|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alcalyn\PayplugBundle\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Routing\Router; |
|
6
|
|
|
use Alcalyn\PayplugBundle\Model\Payment; |
|
7
|
|
|
use Alcalyn\PayplugBundle\Exceptions\PayplugUndefinedAccountParameterException; |
|
8
|
|
|
|
|
9
|
|
|
class PayplugPaymentService |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Payplug url from account configuration |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $baseUrl; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Your private key from account configuration |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $privateKey; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Payplug url from sandbox account configuration |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $testBaseUrl; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Your private key from sandbox account configuration |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $testPrivateKey; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Is sandbox enabled |
|
41
|
|
|
* |
|
42
|
|
|
* @var boolean |
|
43
|
|
|
*/ |
|
44
|
|
|
private $testEnabled; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* IPN url |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $ipnUrl; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $baseUrl |
|
55
|
|
|
* @param string $privateKey |
|
56
|
|
|
* @param string $testBaseUrl |
|
57
|
|
|
* @param string $testPrivateKey |
|
58
|
|
|
* @param boolean $testEnabled |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct($baseUrl, $privateKey, $testBaseUrl, $testPrivateKey, $testEnabled) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->baseUrl = $baseUrl; |
|
63
|
|
|
$this->privateKey = $privateKey; |
|
64
|
|
|
$this->testBaseUrl = $testBaseUrl; |
|
65
|
|
|
$this->testPrivateKey = $testPrivateKey; |
|
66
|
|
|
$this->testEnabled = $testEnabled; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Generate payment url for $payment |
|
71
|
|
|
* |
|
72
|
|
|
* @param Payment $payment |
|
73
|
|
|
* @param boolean $sandbox set it to true or false to force test payment or real payment |
|
74
|
|
|
* |
|
75
|
|
|
* @return string payment url |
|
76
|
|
|
* |
|
77
|
|
|
* @throws PayplugUndefinedAccountParameterException if a parameter is missing |
|
78
|
|
|
*/ |
|
79
|
|
|
public function generateUrl(Payment $payment, $sandbox = null) |
|
80
|
|
|
{ |
|
81
|
|
|
if (null === $sandbox ? $this->testEnabled : $sandbox) { |
|
82
|
|
|
return $this->generateUrlFrom($payment, $this->testBaseUrl, $this->testPrivateKey, true); |
|
83
|
|
|
} else { |
|
84
|
|
|
return $this->generateUrlFrom($payment, $this->baseUrl, $this->privateKey, false); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Generate payment url for $payment from given parameters |
|
90
|
|
|
* |
|
91
|
|
|
* @param Payment $payment |
|
92
|
|
|
* @param string $baseUrl |
|
93
|
|
|
* @param string $privateKey |
|
94
|
|
|
* @param boolean $isTest |
|
95
|
|
|
* |
|
96
|
|
|
* @return string payment url |
|
97
|
|
|
* |
|
98
|
|
|
* @throws PayplugUndefinedAccountParameterException if a parameter is missing |
|
99
|
|
|
*/ |
|
100
|
|
|
private function generateUrlFrom(Payment $payment, $baseUrl, $privateKey, $isTest) |
|
101
|
|
|
{ |
|
102
|
|
|
$testParam = $isTest ? 'sandbox_' : '' ; |
|
103
|
|
|
|
|
104
|
|
|
if (null === $privateKey) { |
|
105
|
|
|
throw new PayplugUndefinedAccountParameterException('payplug_'.$testParam.'account_yourPrivateKey'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (null === $baseUrl) { |
|
109
|
|
|
throw new PayplugUndefinedAccountParameterException('payplug_'.$testParam.'account_url'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// Prepare payment parameters |
|
113
|
|
|
$this->affectDefaultIpnUrlToPayment($payment); |
|
114
|
|
|
$params = $this->convertPaymentToArray($payment); |
|
115
|
|
|
|
|
116
|
|
|
// Create data parameter |
|
117
|
|
|
$url_params = http_build_query($params); |
|
118
|
|
|
$data = urlencode(base64_encode($url_params)); |
|
119
|
|
|
|
|
120
|
|
|
// Create signature parameter |
|
121
|
|
|
$signature = null; |
|
122
|
|
|
$privatekey = openssl_pkey_get_private($privateKey); |
|
123
|
|
|
openssl_sign($url_params, $signature, $privatekey, OPENSSL_ALGO_SHA1); |
|
124
|
|
|
$signatureBase64 = urlencode(base64_encode($signature)); |
|
125
|
|
|
|
|
126
|
|
|
return $baseUrl . '?data=' . $data . '&sign=' . $signatureBase64; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Return default ipn url used by the bundle (Something like "http://yoursite.com/payplug_ipn"). |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getIpnUrl() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->ipnUrl; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set default ipn url used by the bundle (Something like "http://yoursite.com/payplug_ipn"). |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $ipnUrl |
|
143
|
|
|
* |
|
144
|
|
|
* @return PayplugPaymentService |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setIpnUrl($ipnUrl) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->ipnUrl = $ipnUrl; |
|
149
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Set default ipn url used by the bundle |
|
155
|
|
|
* based on the ipn route payplug_ipn. |
|
156
|
|
|
* |
|
157
|
|
|
* @param Router $router |
|
158
|
|
|
* |
|
159
|
|
|
* @return PayplugPaymentService |
|
160
|
|
|
*/ |
|
161
|
|
|
public function setIpnUrlFromRouter(Router $router) |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->setIpnUrl($router->generate('payplug_ipn', array(), true)); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Set automatically ipn url to the default used by this bundle if not set by user |
|
168
|
|
|
* |
|
169
|
|
|
* @param Payment $payment |
|
170
|
|
|
* |
|
171
|
|
|
* @return PayplugPaymentService |
|
172
|
|
|
*/ |
|
173
|
|
|
private function affectDefaultIpnUrlToPayment(Payment $payment) |
|
174
|
|
|
{ |
|
175
|
|
|
if (null === $payment->getIpnUrl()) { |
|
176
|
|
|
$payment->setIpnUrl($this->getIpnUrl()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param Payment $payment |
|
184
|
|
|
* |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
|
|
private function convertPaymentToArray(Payment $payment) |
|
188
|
|
|
{ |
|
189
|
|
|
return array( |
|
190
|
|
|
'amount' => $payment->getAmount(), |
|
191
|
|
|
'currency' => $payment->getCurrency(), |
|
192
|
|
|
'ipn_url' => $payment->getIpnUrl(), |
|
193
|
|
|
'return_url' => $payment->getReturnUrl(), |
|
194
|
|
|
'cancel_url' => $payment->getCancelUrl(), |
|
195
|
|
|
'email' => $payment->getEmail(), |
|
196
|
|
|
'first_name' => $payment->getFirstName(), |
|
197
|
|
|
'last_name' => $payment->getLastName(), |
|
198
|
|
|
'customer' => $payment->getCustomer(), |
|
199
|
|
|
'order' => $payment->getOrder(), |
|
200
|
|
|
'custom_data' => $payment->getCustomData(), |
|
201
|
|
|
'origin' => $payment->getOrigin(), |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: