-
Notifications
You must be signed in to change notification settings - Fork 1
/
PayPlugPaymentGateway.php
118 lines (103 loc) · 3.56 KB
/
PayPlugPaymentGateway.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
<?php
namespace IDCI\Bundle\PaymentBundle\Gateway;
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 Payplug;
use Symfony\Component\HttpFoundation\Request;
class PayPlugPaymentGateway extends AbstractPaymentGateway
{
/**
* {@inheritdoc}
*/
public function initialize(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
Payplug\Payplug::setSecretKey($paymentGatewayConfiguration->get('secret_key'));
$payment = Payplug\Payment::create([
'amount' => $transaction->getAmount(),
'currency' => $transaction->getCurrencyCode(),
'customer' => [
'email' => null,
'first_name' => null,
'last_name' => null,
],
'hosted_payment' => [
'return_url' => $paymentGatewayConfiguration->get('return_url'),
'cancel_url' => $paymentGatewayConfiguration->get('return_url'),
],
'notification_url' => $paymentGatewayConfiguration->get('callback_url'),
'metadata' => [
'transaction_id' => $transaction->getId(),
],
]);
return [
'payment' => $payment,
];
}
/**
* {@inheritdoc}
*/
public function buildHTMLView(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): string {
$initializationData = $this->initialize($paymentGatewayConfiguration, $transaction);
return $this->templating->render('@IDCIPayment/Gateway/payplug.html.twig', [
'initializationData' => $initializationData,
]);
}
/**
* {@inheritdoc}
*/
public function getReturnResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
return new GatewayResponse();
}
/**
* {@inheritdoc}
*
* @throws \UnexpectedValueException If the request method is not POST
*/
public function getCallbackResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
if (!$request->isMethod(Request::METHOD_POST)) {
throw new \UnexpectedValueException('PayPlug : Payment Gateway error (Request method should be POST)');
}
$gatewayResponse = (new GatewayResponse())
->setDate(new \DateTime())
->setStatus(PaymentStatus::STATUS_FAILED)
;
if (empty($request->request->all())) {
return $gatewayResponse->setMessage('The request do not contains required post data');
}
$params = $request->request->all();
$gatewayResponse
->setTransactionUuid($params['metadata']['transaction_id'])
->setAmount($params['amount'])
->setCurrencyCode($params['currency'])
;
if (!$params['is_paid']) {
return $gatewayResponse->setMessage('Transaction unauthorized');
}
return $gatewayResponse->setStatus(PaymentStatus::STATUS_APPROVED);
}
/**
* {@inheritdoc}
*/
public static function getParameterNames(): ?array
{
return array_merge(
parent::getParameterNames(),
[
'secret_key',
]
);
}
}