Skip to content

Commit c7503ba

Browse files
authored
Merge pull request #14 from IDCI-Consulting/feat/fix-paypal-gateway
Fix: PayPal payment gateway
2 parents 98575eb + 64b8cc6 commit c7503ba

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

Gateway/PaypalPaymentGateway.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
class PaypalPaymentGateway extends AbstractPaymentGateway
1616
{
17+
const PAYPAL_CHECKOUT_FLOW_TEMPLATE_MAPPING = [
18+
'PAY_NOW' => 'paypal_pay_now.html.twig',
19+
// 'SMART_BUTTON' => 'paypal_smart_button.html.twig',
20+
];
21+
1722
public function initialize(
1823
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
1924
Transaction $transaction
@@ -23,7 +28,7 @@ public function initialize(
2328
'transaction' => $transaction,
2429
'callbackUrl' => $paymentGatewayConfiguration->get('callback_url'),
2530
'returnUrl' => $paymentGatewayConfiguration->get('return_url'),
26-
'environment' => $paymentGatewayConfiguration->get('environment'),
31+
'mode' => $paymentGatewayConfiguration->get('mode'),
2732
];
2833
}
2934

@@ -33,9 +38,25 @@ public function buildHTMLView(
3338
): string {
3439
$initializationData = $this->initialize($paymentGatewayConfiguration, $transaction);
3540

36-
return $this->templating->render('@IDCIPaymentBundle/Resources/views/Gateway/paypal.html.twig', [
37-
'initializationData' => $initializationData,
38-
]);
41+
if (!array_key_exists($paymentGatewayConfiguration->get('checkout_flow'), self::PAYPAL_CHECKOUT_FLOW_TEMPLATE_MAPPING)) {
42+
throw new \UnexpectedValueException(
43+
sprintf(
44+
'The checkout flow "%s" is not yet implemented in %s',
45+
$paymentGatewayConfiguration->get('checkout_flow'),
46+
self::class
47+
)
48+
);
49+
}
50+
51+
return $this->templating->render(
52+
sprintf(
53+
'@IDCIPaymentBundle/Resources/views/Gateway/%s',
54+
self::PAYPAL_CHECKOUT_FLOW_TEMPLATE_MAPPING[$paymentGatewayConfiguration->get('checkout_flow')]
55+
),
56+
[
57+
'initializationData' => $initializationData,
58+
]
59+
);
3960
}
4061

4162
public function getResponse(
@@ -57,6 +78,10 @@ public function getResponse(
5778
$paymentGatewayConfiguration->get('client_secret')
5879
));
5980

81+
$apiContext->setConfig([
82+
'mode' => $paymentGatewayConfiguration->get('mode'),
83+
]);
84+
6085
$paypalPayment = PaypalPayment::get($request->get('paymentID'), $apiContext);
6186

6287
$amount = $paypalPayment->getTransactions()[0]->getAmount();
@@ -88,7 +113,8 @@ public static function getParameterNames(): ?array
88113
[
89114
'client_id',
90115
'client_secret',
91-
'environment',
116+
'mode',
117+
'checkout_flow',
92118
]
93119
);
94120
}

Resources/views/Gateway/paypal.html.twig renamed to Resources/views/Gateway/paypal_pay_now.html.twig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
paypal.Button.render({
1010
11-
env: '{{ initializationData.environment == 'live' ? 'production' : initializationData.environment }}',
11+
env: '{{ initializationData.mode == 'live' ? 'production' : initializationData.mode }}',
1212
1313
client: {
1414
sandbox: '{{ initializationData.clientId }}',
@@ -39,5 +39,9 @@
3939
});
4040
}
4141
42+
onError: function (err) {
43+
console.error(err);
44+
}
45+
4246
}, '#paypal-button');
4347
</script>

0 commit comments

Comments
 (0)