-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStripePaymentGateway.php
124 lines (108 loc) · 3.75 KB
/
StripePaymentGateway.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
<?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 Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;
class StripePaymentGateway extends AbstractPaymentGateway
{
/**
* @var UrlGeneratorInterface
*/
private $router;
public function __construct(
Environment $templating,
EventDispatcherInterface $dispatcher,
UrlGeneratorInterface $router
) {
parent::__construct($templating, $dispatcher);
$this->router = $router;
}
/**
* {@inheritdoc}
*/
public function initialize(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction,
array $options = []
): array {
return [
'callbackUrl' => $paymentGatewayConfiguration->get('callback_url'),
'cancelUrl' => $paymentGatewayConfiguration->get('return_url'),
'publicKey' => $paymentGatewayConfiguration->get('public_key'),
'proxyUrl' => $this->router->generate(
'idci_payment_stripe_payment_gateway_proxy',
['configuration_alias' => $paymentGatewayConfiguration->getAlias()],
UrlGeneratorInterface::ABSOLUTE_URL
),
'returnUrl' => $paymentGatewayConfiguration->get('return_url'),
'transaction' => $transaction,
];
}
/**
* {@inheritdoc}
*/
public function buildHTMLView(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction,
array $options = []
): string {
$initializationData = $this->initialize($paymentGatewayConfiguration, $transaction);
return $this->templating->render('@IDCIPayment/Gateway/stripe.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('POST')) {
throw new \UnexpectedValueException('Stripe : Payment Gateway error (Request method should be POST)');
}
$gatewayResponse = (new GatewayResponse())
->setDate(new \DateTime())
->setStatus(PaymentStatus::STATUS_FAILED)
;
$gatewayResponse
->setTransactionUuid($request->get('transactionId'))
->setAmount($request->get('amount'))
->setCurrencyCode($request->get('currencyCode'))
;
if (null !== $request->get('error')) {
return $gatewayResponse->setMessage($request->get('error')['message']);
}
$gatewayResponse->setRaw($request->get('raw'));
return $gatewayResponse->setStatus(PaymentStatus::STATUS_APPROVED);
}
/**
* {@inheritdoc}
*/
public static function getParameterNames(): ?array
{
return array_merge(
parent::getParameterNames(),
[
'public_key',
'secret_key',
]
);
}
}