-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlmaPaymentGateway.php
308 lines (274 loc) · 14.4 KB
/
AlmaPaymentGateway.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
namespace IDCI\Bundle\PaymentBundle\Gateway;
use Alma\API\Client;
use Alma\API\RequestError;
use IDCI\Bundle\PaymentBundle\Gateway\StatusCode\AlmaStatusCode;
use IDCI\Bundle\PaymentBundle\Model\GatewayResponse;
use IDCI\Bundle\PaymentBundle\Model\PaymentGatewayConfigurationInterface;
use IDCI\Bundle\PaymentBundle\Model\Transaction;
use IDCI\Bundle\PaymentBundle\Payment\PaymentStatus;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;
class AlmaPaymentGateway extends AbstractPaymentGateway
{
private $logger;
public function __construct(
Environment $templating,
EventDispatcherInterface $dispatcher,
LoggerInterface $logger
) {
parent::__construct($templating, $dispatcher);
$this->logger = $logger;
}
private function getClient(PaymentGatewayConfigurationInterface $paymentGatewayConfiguration)
{
if (!class_exists(Client::class)) {
throw new \RuntimeException('AlmaPaymentGateway cache requires "alma/alma-php-client" package');
}
return new Client($paymentGatewayConfiguration->get('api_key'), [
'mode' => $paymentGatewayConfiguration->get('mode'),
]);
}
private function createPayment(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
) {
try {
return $this->getClient($paymentGatewayConfiguration)->payments->create(
$this->resolveCreatePaymentOptions(
$paymentGatewayConfiguration,
$transaction,
$transaction->getMetadata('payment_options') ?? []
)
);
} catch (RequestError $e) {
$this->logger->error(sprintf('Error: %s. Context: %s', $e->getMessage(), json_encode($e->response->json)));
}
}
/**
* {@inheritdoc}
*/
public function initialize(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction,
array $options = []
): array {
$payment = $this->createPayment($paymentGatewayConfiguration, $transaction);
return [
'url' => $payment->url,
];
}
/**
* {@inheritdoc}
*/
public function buildHTMLView(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction,
array $options = []
): string {
$initializationData = $this->initialize($paymentGatewayConfiguration, $transaction);
return $this->templating->render('@IDCIPayment/Gateway/alma.html.twig', [
'url' => $initializationData['url'],
]);
}
/**
* {@inheritdoc}
*/
public function getReturnResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
$gatewayResponse = new GatewayResponse();
if (!$request->query->has('pid')) {
return $gatewayResponse;
}
try {
$payment = $this->getClient($paymentGatewayConfiguration)->payments->fetch($request->query->get('pid'));
$gatewayResponse
->setTransactionUuid($payment->orders[0]->merchant_reference)
->setAmount($payment->purchase_amount)
->setRaw(get_object_vars($payment))
;
if (!in_array($payment->state, [AlmaStatusCode::STATUS_IN_PROGRESS, AlmaStatusCode::STATUS_PAID])) {
return $gatewayResponse->setStatus(PaymentStatus::STATUS_FAILED);
}
return $gatewayResponse->setStatus(PaymentStatus::STATUS_APPROVED);
} catch (\Exception $e) {
$this->logger->error(sprintf('Error: %s. Context: %s', $e->getMessage(), json_encode($e->response->json)));
}
return $gatewayResponse;
}
/**
* {@inheritdoc}
*
* @throws \UnexpectedValueException If the request method is not POST
*/
public function getCallbackResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
if (!$request->isMethod('GET')) {
throw new \UnexpectedValueException('Alma : Payment Gateway error (Request method should be POST)');
}
$gatewayResponse = (new GatewayResponse())
->setDate(new \DateTime())
->setStatus(PaymentStatus::STATUS_FAILED)
->setRaw($request->query->all())
;
if (!$request->query->has('pid')) {
throw new \UnexpectedValueException('Alma : Payment Gateway error (missing pid query parameter)');
}
$payment = $this->getClient($paymentGatewayConfiguration)->payments->fetch($request->query->get('pid'));
$gatewayResponse
->setTransactionUuid($payment->orders[0]->merchant_reference)
->setAmount($payment->purchase_amount)
->setRaw(get_object_vars($payment))
;
if (!in_array($payment->state, [AlmaStatusCode::STATUS_IN_PROGRESS, AlmaStatusCode::STATUS_PAID])) {
$gatewayResponse->setMessage(AlmaStatusCode::getStatusMessage($payment->state));
return $gatewayResponse;
}
return $gatewayResponse->setStatus(PaymentStatus::STATUS_APPROVED);
}
/**
* {@inheritdoc}
*/
public static function getParameterNames(): ?array
{
return array_merge(
parent::getParameterNames(),
[
'api_key',
'mode',
]
);
}
/**
* Resolve createPayment options.
*
* @method resolveCreatePaymentOptions
*/
private function resolveCreatePaymentOptions(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction,
array $options
): array {
$resolver = (new OptionsResolver())
->setDefault('payment', function (OptionsResolver $paymentResolver) use ($paymentGatewayConfiguration, $transaction) {
$paymentResolver
->setDefault('purchase_amount', $transaction->getAmount())->setAllowedTypes('purchase_amount', ['int'])
->setDefined('installments_count')->setAllowedTypes('installments_count', ['int'])
->setDefault('billing_address', function (OptionsResolver $billingAddressResolver) {
$billingAddressResolver
->setDefined('company')->setAllowedTypes('company', ['string'])
->setDefined('first_name')->setAllowedTypes('first_name', ['string'])
->setDefined('last_name')->setAllowedTypes('last_name', ['string'])
->setDefined('email')->setAllowedTypes('email', ['string'])
->setDefined('phone')->setAllowedTypes('phone', ['string'])
->setDefined('line1')->setAllowedTypes('line1', ['string'])
->setDefined('line2')->setAllowedTypes('line2', ['string'])
->setDefined('postal_code')->setAllowedTypes('postal_code', ['string', 'int'])
->setDefined('city')->setAllowedTypes('city', ['string'])
->setDefined('country')->setAllowedTypes('country', ['string'])
;
})
->setDefault('customer_cancel_url', $paymentGatewayConfiguration->get('return_url'))->setAllowedTypes('customer_cancel_url', ['string'])
->setDefined('custom_data')->setAllowedTypes('custom_data', ['string', 'array'])
->setNormalizer('custom_data', function (Options $options, $value) {
if (is_array($value)) {
$value = json_encode($value);
}
return $value;
})
->setDefined('deferred_months')->setAllowedTypes('deferred_months', ['int'])
->setDefined('deferred_days')->setAllowedTypes('deferred_days', ['int'])
->setDefault('ipn_callback_url', $paymentGatewayConfiguration->get('callback_url'))->setAllowedTypes('ipn_callback_url', ['string'])
->setDefined('origin')->setAllowedTypes('origin', ['string'])
->setDefault('return_url', $paymentGatewayConfiguration->get('return_url'))->setAllowedTypes('return_url', ['string'])
->setDefault('shipping_address', function (OptionsResolver $shippingAddressResolver) {
$shippingAddressResolver
->setDefined('company')->setAllowedTypes('company', ['string'])
->setDefined('first_name')->setAllowedTypes('first_name', ['string'])
->setDefined('last_name')->setAllowedTypes('last_name', ['string'])
->setDefined('email')->setAllowedTypes('email', ['string'])
->setDefined('phone')->setAllowedTypes('phone', ['string'])
->setDefined('line1')->setAllowedTypes('line1', ['string'])
->setDefined('line2')->setAllowedTypes('line2', ['string'])
->setDefined('postal_code')->setAllowedTypes('postal_code', ['string', 'int'])
->setDefined('city')->setAllowedTypes('city', ['string'])
->setDefined('country')->setAllowedTypes('country', ['string'])
;
})
->setDefined('locale')->setAllowedTypes('locale', ['string'])
->setDefined('deferred')->setAllowedValues('deferred', ['trigger'])
->setDefined('deferred_description')->setAllowedTypes('deferred_description', ['string'])
->setDefined('expires_after')->setAllowedTypes('expires_after', ['int'])
;
})
->setDefault('customer', function (OptionsResolver $customerResolver) use ($transaction) {
$customerResolver
->setDefined('id')->setAllowedTypes('id', ['string'])
->setDefault('identifier', $transaction->getCustomerId())->setAllowedTypes('identifier', ['string'])
->setDefined('created')->setAllowedTypes('created', [\DateTimeInterface::class, 'string'])
->setNormalizer('created', function (Options $options, $value) {
if (is_string($value)) {
$value = new \DateTime($value);
}
return $value->format('U');
})
->setDefined('first_name')->setAllowedTypes('first_name', ['string'])
->setDefined('last_name')->setAllowedTypes('last_name', ['string'])
->setDefined('addresses')->setAllowedTypes('addresses', ['array'])
->setDefault('email', $transaction->getCustomerEmail())->setAllowedTypes('email', ['string'])
->setDefined('phone')->setAllowedTypes('phone', ['string'])
->setDefined('birth_date')->setAllowedTypes('birth_date', ['string'])
->setDefined('birth_place')->setAllowedTypes('birth_place', ['string'])
->setDefault('card', function (OptionsResolver $cardResolver) {
$cardResolver
->setDefined('id')->setAllowedTypes('id', ['string'])
->setDefined('created')->setAllowedTypes('created', [\DateTimeInterface::class, 'string'])
->setNormalizer('created', function (Options $options, $value) {
if (is_string($value)) {
$value = new \DateTime($value);
}
return $value->format('U');
})
->setDefined('exp_month')->setAllowedTypes('exp_month', ['int'])
->setDefined('exp_year')->setAllowedTypes('exp_year', ['int'])
->setDefined('last4')->setAllowedTypes('last4', ['string'])
->setDefined('country')->setAllowedTypes('country', ['string'])
->setDefined('funding')->setAllowedValues('funding', ['debit', 'credit', 'unknown'])
->setDefined('brand')->setAllowedValues('brand', ['visa', 'mastercard', 'american express'])
->setDefined('three_d_secure_possible')->setAllowedTypes('three_d_secure_possible', ['bool'])
->setDefined('verified')->setAllowedTypes('verified', ['bool'])
;
})
->setDefined('banking_data_collected')->setAllowedTypes('banking_data_collected', ['bool'])
->setDefined('is_business')->setAllowedTypes('is_business', ['bool'])
->setDefined('business_id_number')->setAllowedTypes('business_id_number', ['string'])
->setDefined('business_name')->setAllowedTypes('business_name', ['string'])
;
})
->setDefault('order', function (OptionsResolver $orderResolver) use ($transaction) {
$orderResolver
->setDefault('merchant_reference', $transaction->getId())->setAllowedTypes('merchant_reference', ['string'])
->setDefined('merchant_url')->setAllowedTypes('merchant_url', ['string'])
->setDefined('data')->setAllowedTypes('data', ['string', 'array'])
->setNormalizer('data', function (Options $options, $value) {
if (is_string($value)) {
$value = json_decode($value, true);
}
return $value;
})
->setDefined('customer_url')->setAllowedTypes('customer_url', ['string'])
->setDefined('comment')->setAllowedTypes('comment', ['string'])
;
})
;
return $resolver->resolve($options);
}
}