1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alcalyn\PayplugBundle\Services; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Alcalyn\PayplugBundle\Model\IPN; |
7
|
|
|
|
8
|
|
|
class PayplugIPNService |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Payplug public key from configuration |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $payplugPublicKey; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* IPN class from configuration used to create ipn instance |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $ipnClass; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $payplugPublicKey |
26
|
|
|
*/ |
27
|
|
|
public function __construct($payplugPublicKey, $ipnClass) |
28
|
|
|
{ |
29
|
|
|
$this->payplugPublicKey = $payplugPublicKey; |
30
|
|
|
$this->ipnClass = $ipnClass; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Verify ipn request validity |
35
|
|
|
* |
36
|
|
|
* @param Request $request |
37
|
|
|
* |
38
|
|
|
* @return boolean |
39
|
|
|
*/ |
40
|
|
|
public function verifyIPNRequest(Request $request) |
41
|
|
|
{ |
42
|
|
|
$signature = base64_decode($request->headers->get('payplug-signature')); |
43
|
|
|
$body = $request->getContent(); |
44
|
|
|
|
45
|
|
|
return $this->verifyIPN($body, $signature); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Verify ipn content |
50
|
|
|
* |
51
|
|
|
* @param string $body |
52
|
|
|
* @param string $signature |
53
|
|
|
* |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
|
|
public function verifyIPN($body, $signature) |
57
|
|
|
{ |
58
|
|
|
$publicKey = openssl_pkey_get_public($this->payplugPublicKey); |
59
|
|
|
return 1 === openssl_verify($body, $signature, $publicKey, OPENSSL_ALGO_SHA1); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create IPN instance from body json data |
64
|
|
|
* |
65
|
|
|
* @param string $body |
66
|
|
|
* |
67
|
|
|
* @return IPN |
68
|
|
|
*/ |
69
|
|
|
public function createIPNFromBody($body) |
70
|
|
|
{ |
71
|
|
|
$data = json_decode($body, true); |
72
|
|
|
|
73
|
|
|
return $this->createIPNFromData($data); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Build IPN instance from array |
78
|
|
|
* |
79
|
|
|
* @param array $data |
80
|
|
|
* |
81
|
|
|
* @return IPN |
82
|
|
|
*/ |
83
|
|
|
public function createIPNFromData(array $data) |
84
|
|
|
{ |
85
|
|
|
$ipn = new $this->ipnClass(); |
86
|
|
|
|
87
|
|
|
return $ipn |
88
|
|
|
->setState($data['state']) |
89
|
|
|
->setIdTransaction($data['id_transaction']) |
90
|
|
|
->setAmount($data['amount']) |
91
|
|
|
->setEmail($data['email']) |
92
|
|
|
->setFirstName($data['first_name']) |
93
|
|
|
->setLastName($data['last_name']) |
94
|
|
|
->setCustomer($data['customer']) |
95
|
|
|
->setOrder($data['order']) |
96
|
|
|
->setCustomData($data['custom_data']) |
97
|
|
|
->setOrigin($data['origin']) |
98
|
|
|
->setIsTest($data['is_test']) |
99
|
|
|
; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.