-
Notifications
You must be signed in to change notification settings - Fork 1
/
SofincoPaymentGateway.php
177 lines (152 loc) · 5.23 KB
/
SofincoPaymentGateway.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
<?php
namespace IDCI\Bundle\PaymentBundle\Gateway;
use GuzzleHttp\Client;
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 Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;
class SofincoPaymentGateway extends AbstractPaymentGateway
{
const OFFER_MODULE = 'WSACCROCHE';
const PRODUCT_MODULE = 'PRODUCT';
const CART_MODULE = 'PANIER';
/**
* @var string
*/
private $serverUrl;
public function __construct(
Environment $templating,
EventDispatcherInterface $dispatcher,
string $serverUrl
) {
parent::__construct($templating, $dispatcher);
$this->serverUrl = $serverUrl;
}
/**
* Build options for WSACCROCHE Sofinco module.
*
* @method buildOfferVerifyOptions
*/
private function buildOfferVerifyOptions(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
return [
'q6' => $paymentGatewayConfiguration->get('site_id'),
'p0' => self::OFFER_MODULE,
'p4' => $transaction->getAmount() / 100,
];
}
/**
* Build gateway form options.
*
* @method buildOptions
*/
private function buildOptions(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
return [
'q6' => $paymentGatewayConfiguration->get('site_id'),
'p0' => self::CART_MODULE,
'ti' => $transaction->getId(),
's3' => $transaction->getAmount() / 100,
'uret' => $paymentGatewayConfiguration->get('return_url'),
'p5' => $paymentGatewayConfiguration->get('callback_url'),
];
}
/**
* Check if the sofinco offer exists according to transaction amount.
*
* @method verifyIfOfferExist
*
* @throws \UnexpectedValueException If the offer doesn't exists
*/
private function verifyIfOfferExist(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): bool {
$options = $this->buildOfferVerifyOptions($paymentGatewayConfiguration, $transaction);
$response = (new Client())->request('GET', $this->serverUrl, [
'query' => $options,
]);
$data = json_decode(json_encode(new \SimpleXMLElement($response->getBody()->getContents())), true);
if ('00' !== $data['C_RETOUR']) {
throw new \UnexpectedValueException(sprintf('Error code %s: No offer exists for the contract code "%s" and the amount "%s". Result of the request: %s', $data['C_RETOUR'], $options['q6'], $options['p4'], json_encode($data)));
}
return true;
}
/**
* {@inheritdoc}
*/
public function initialize(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
$options = $this->buildOptions($paymentGatewayConfiguration, $transaction);
return [
'url' => $this->serverUrl,
'options' => $options,
];
}
/**
* {@inheritdoc}
*/
public function buildHTMLView(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): string {
$initializationData = $this->initialize($paymentGatewayConfiguration, $transaction);
return $this->templating->render('@IDCIPayment/Gateway/sofinco.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 GET
*/
public function getCallbackResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
if (!$request->isMethod(Request::METHOD_GET)) {
throw new \UnexpectedValueException('Sofinco : Payment Gateway error (Request method should be GET)');
}
$gatewayResponse = (new GatewayResponse())
->setTransactionUuid($request->query->get('ti'))
->setAmount($request->query->get('s3'))
->setDate(new \DateTime())
->setStatus(PaymentStatus::STATUS_FAILED)
->setRaw($request->query->all())
;
if (1 == $request->query->get('c3')) {
return $gatewayResponse->setMessage('Transaction unauthorized');
}
return $gatewayResponse->setStatus(PaymentStatus::STATUS_UNVERIFIED);
}
/**
* {@inheritdoc}
*/
public static function getParameterNames(): ?array
{
return array_merge(
parent::getParameterNames(),
[
'site_id',
]
);
}
}