-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPaypalPaymentGateway.php
141 lines (121 loc) · 4.66 KB
/
PaypalPaymentGateway.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
<?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 PayPal\Api\Payment as PaypalPayment;
use PayPal\Api\PaymentExecution;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use Symfony\Component\HttpFoundation\Request;
class PaypalPaymentGateway extends AbstractPaymentGateway
{
const PAYPAL_CHECKOUT_FLOW_TEMPLATE_MAPPING = [
'PAY_NOW' => 'paypal_pay_now.html.twig',
// 'SMART_BUTTON' => 'paypal_smart_button.html.twig',
];
/**
* {@inheritdoc}
*/
public function initialize(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
return [
'clientId' => $paymentGatewayConfiguration->get('client_id'),
'transaction' => $transaction,
'callbackUrl' => $paymentGatewayConfiguration->get('callback_url'),
'returnUrl' => $paymentGatewayConfiguration->get('return_url'),
'mode' => $paymentGatewayConfiguration->get('mode'),
];
}
/**
* {@inheritdoc}
*
* @throws \UnexpectedValueException If the payment gateway configuration use a non authorized checkout flow
*/
public function buildHTMLView(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): string {
$initializationData = $this->initialize($paymentGatewayConfiguration, $transaction);
if (!array_key_exists($paymentGatewayConfiguration->get('checkout_flow'), self::PAYPAL_CHECKOUT_FLOW_TEMPLATE_MAPPING)) {
throw new \UnexpectedValueException(sprintf('The checkout flow "%s" is not yet implemented in %s', $paymentGatewayConfiguration->get('checkout_flow'), self::class));
}
return $this->templating->render(
sprintf(
'@IDCIPayment/Gateway/%s',
self::PAYPAL_CHECKOUT_FLOW_TEMPLATE_MAPPING[$paymentGatewayConfiguration->get('checkout_flow')]
),
[
'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('Paypal : Payment Gateway error (Request method should be POST)');
}
$gatewayResponse = (new GatewayResponse())
->setRaw($request->request->all())
->setDate(new \DateTime())
->setStatus(PaymentStatus::STATUS_FAILED)
;
$apiContext = new ApiContext(new OAuthTokenCredential(
$paymentGatewayConfiguration->get('client_id'),
$paymentGatewayConfiguration->get('client_secret')
));
$apiContext->setConfig([
'mode' => $paymentGatewayConfiguration->get('mode'),
]);
$paypalPayment = PaypalPayment::get($request->get('paymentID'), $apiContext);
$amount = $paypalPayment->getTransactions()[0]->getAmount();
$gatewayResponse
->setPaymentMethod($paypalPayment->getPayer()->getPaymentMethod())
->setTransactionUuid($request->get('transactionID'))
->setAmount($amount->total * 100)
->setCurrencyCode($amount->currency)
->setRaw($paypalPayment->toArray())
;
$execution = new PaymentExecution();
$execution->setPayerId($request->get('payerID'));
$result = $paypalPayment->execute($execution, $apiContext);
if ('approved' !== $result->getState()) {
return $gatewayResponse->setMessage('Transaction unauthorized');
}
return $gatewayResponse->setStatus(PaymentStatus::STATUS_APPROVED);
}
/**
* {@inheritdoc}
*/
public static function getParameterNames(): ?array
{
return array_merge(
parent::getParameterNames(),
[
'client_id',
'client_secret',
'mode',
'checkout_flow',
]
);
}
}