Skip to content

Commit 5e471f3

Browse files
authored
Merge pull request #2 from magebitcom/feature/google-pay
Google Pay implementation
2 parents a2c3af9 + 5f6fea6 commit 5e471f3

File tree

15 files changed

+697
-34
lines changed

15 files changed

+697
-34
lines changed

Helper/Config.php

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace Magebit\CheckoutComPayment\Helper;
1111

12+
use CheckoutCom\Magento2\Model\Config\Backend\Source\ConfigGooglePayButton;
13+
use CheckoutCom\Magento2\Model\Config\Backend\Source\ConfigGooglePayEnvironment;
14+
use CheckoutCom\Magento2\Model\Config\Backend\Source\ConfigGooglePayNetworks;
1215
use JsonException;
1316
use Magento\Customer\Model\Session;
1417
use Magento\Framework\App\Helper\AbstractHelper;
@@ -19,6 +22,14 @@
1922

2023
class Config extends AbstractHelper implements ArgumentInterface
2124
{
25+
/**
26+
* Default allowed GooglePay networks if not configured
27+
*/
28+
private const DEFAULT_CARD_NETWORKS = [
29+
ConfigGooglePayNetworks::CARD_VISA,
30+
ConfigGooglePayNetworks::CARD_MASTERCARD
31+
];
32+
2233
/**
2334
* @param Session $session
2435
* @param Repository $assetRepository
@@ -239,8 +250,105 @@ public function getImagesPath(): string
239250
public function isPhoneValidationEnabled(): bool
240251
{
241252
return (bool) $this->scopeConfig->getValue(
242-
'settings/checkoutcom_configuration/mb_pay_phone_validation',
253+
'payment/checkoutcom_apm/mb_pay_phone_validation',
254+
ScopeInterface::SCOPE_STORE,
255+
);
256+
}
257+
258+
/**
259+
* Get the store name
260+
*
261+
* @return string
262+
*/
263+
public function getStoreName(): string
264+
{
265+
return $this->scopeConfig->getValue(
266+
'general/store_information/name',
243267
ScopeInterface::SCOPE_STORE,
268+
) ?? '';
269+
}
270+
271+
/**
272+
* Get Google Pay gateway name
273+
*
274+
* @return string
275+
*/
276+
public function getGatewayName(): string
277+
{
278+
return $this->scopeConfig->getValue(
279+
'payment/checkoutcom_google_pay/gateway_name',
280+
ScopeInterface::SCOPE_STORE
281+
) ?? 'checkoutltd';
282+
}
283+
284+
/**
285+
* Get Google Pay merchant ID
286+
*
287+
* @return string|null
288+
*/
289+
public function getMerchantId(): ?string
290+
{
291+
return $this->scopeConfig->getValue(
292+
'payment/checkoutcom_google_pay/merchant_id',
293+
ScopeInterface::SCOPE_STORE
294+
);
295+
}
296+
297+
/**
298+
* Get Google Pay environment
299+
*
300+
* @return string
301+
*/
302+
public function getEnvironment(): string
303+
{
304+
return $this->scopeConfig->getValue(
305+
'payment/checkoutcom_google_pay/environment',
306+
ScopeInterface::SCOPE_STORE
307+
) ?? ConfigGooglePayEnvironment::ENVIRONMENT_TEST;
308+
}
309+
310+
/**
311+
* Get allowed card networks
312+
*
313+
* @return array
314+
*/
315+
public function getAllowedCardNetworks(): array
316+
{
317+
$networks = $this->scopeConfig->getValue(
318+
'payment/checkoutcom_google_pay/allowed_card_networks',
319+
ScopeInterface::SCOPE_STORE
244320
);
321+
322+
if (empty($networks)) {
323+
return self::DEFAULT_CARD_NETWORKS;
324+
}
325+
326+
return explode(',', $networks);
327+
}
328+
329+
/**
330+
* Google Pay button corner radius
331+
*
332+
* @return int
333+
*/
334+
public function getButtonRadius(): int
335+
{
336+
return (int)$this->scopeConfig->getValue(
337+
'payment/checkoutcom_google_pay/button_radius',
338+
ScopeInterface::SCOPE_STORE,
339+
) ?? 8;
340+
}
341+
342+
/**
343+
* Get Google Pay button style
344+
*
345+
* @return string
346+
*/
347+
public function getButtonStyle(): string
348+
{
349+
return $this->scopeConfig->getValue(
350+
'payment/checkoutcom_google_pay/button_style',
351+
ScopeInterface::SCOPE_STORE
352+
) ?? ConfigGooglePayButton::BUTTON_BLACK;
245353
}
246354
}

Magewire/Payment/Method/CheckoutComApm.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,6 @@ private function updateApmData(string $method, string $key, $value): void
135135
$this->checkoutSession->setData(self::APM_DATA, $apmData);
136136
}
137137

138-
/**
139-
* Get payment method code
140-
*
141-
* @return string
142-
*/
143-
public function getCode(): string
144-
{
145-
return 'checkoutcom_apm';
146-
}
147-
148-
/**
149-
* Get payment method name
150-
*
151-
* @return string
152-
*/
153-
public function getName(): string
154-
{
155-
return 'checkoutcom_apm';
156-
}
157-
158138
/**
159139
* Get available APM methods
160140
*
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2025 Magebit, Ltd. (https://magebit.com/)
4+
* @author Magebit <info@magebit.com>
5+
* @license MIT
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magebit\CheckoutComPayment\Magewire\Payment\Method;
11+
12+
use Magebit\CheckoutComPayment\Model\Magewire\Payment\CheckoutComPlaceOrderService;
13+
use Magebit\CheckoutComPayment\ViewModel\Data;
14+
use Magento\Checkout\Model\Session as CheckoutSession;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magewirephp\Magewire\Component;
18+
19+
class CheckoutComGooglePay extends Component
20+
{
21+
/**
22+
* Define component's listeners for checkout events
23+
*
24+
* @var array
25+
*/
26+
protected $listeners = [
27+
'shipping_method_selected' => 'refresh',
28+
'coupon_code_applied' => 'refresh',
29+
'coupon_code_revoked' => 'refresh',
30+
];
31+
32+
/**
33+
* @var float
34+
*/
35+
public float $amount = 0.0;
36+
37+
/**
38+
* Google Pay session storage constants
39+
*/
40+
public const PAYMENT_TOKEN = 'checkout_com_google_pay_token';
41+
public const PAYMENT_SOURCE = 'checkout_com_google_pay_source';
42+
43+
/**
44+
* @param CheckoutSession $checkoutSession
45+
* @param CheckoutComPlaceOrderService $placeOrderService
46+
* @param Data $checkoutViewModel
47+
*/
48+
public function __construct(
49+
private readonly CheckoutSession $checkoutSession,
50+
private readonly CheckoutComPlaceOrderService $placeOrderService,
51+
private readonly Data $checkoutViewModel
52+
) {
53+
}
54+
55+
/**
56+
* Initialize component with cart details
57+
*
58+
* @return void
59+
*/
60+
public function boot(): void
61+
{
62+
$this->setCartDetails();
63+
}
64+
65+
/**
66+
* Set all cart details needed for placing order
67+
*
68+
* @return void
69+
*/
70+
protected function setCartDetails(): void
71+
{
72+
$this->amount = $this->checkoutViewModel->getTotal();
73+
}
74+
75+
/**
76+
* Set the Google Pay token with related data
77+
*
78+
* @param array $tokenData
79+
* @return void
80+
*/
81+
public function setPaymentToken(array $tokenData): void
82+
{
83+
$this->checkoutSession->setData(self::PAYMENT_TOKEN, $tokenData);
84+
$this->checkoutSession->setData(self::PAYMENT_SOURCE, 'checkoutcom_google_pay');
85+
}
86+
87+
/**
88+
* Place order and handle redirection
89+
*
90+
* @throws NoSuchEntityException
91+
* @throws LocalizedException
92+
* @return void
93+
*/
94+
public function placeOrder(): void
95+
{
96+
$quote = $this->checkoutSession->getQuote();
97+
$orderId = $this->placeOrderService->placeOrder($quote);
98+
99+
if ($orderId) {
100+
$redirectUrl = $this->placeOrderService->getRedirectUrl($quote, $orderId);
101+
$this->redirect($redirectUrl, null, true);
102+
}
103+
}
104+
}

Model/Magewire/Payment/CheckoutComPlaceOrderService.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Exception;
2222
use Magebit\CheckoutComPayment\Magewire\Payment\Method\CheckoutComApm;
2323
use Magebit\CheckoutComPayment\Magewire\Payment\Method\CheckoutComCard;
24+
use Magebit\CheckoutComPayment\Magewire\Payment\Method\CheckoutComGooglePay;
2425
use Magebit\CheckoutComPayment\Magewire\Payment\Method\CheckoutComVault;
2526
use Hyva\Checkout\Model\Magewire\Payment\AbstractOrderData;
2627
use Hyva\Checkout\Model\Magewire\Payment\AbstractPlaceOrderService;
@@ -113,6 +114,11 @@ public function placeOrder(Quote $quote): int
113114
'checkoutcom_vault' => [
114115
'publicHash' => $this->session->getData(CheckoutComVault::PUBLIC_HASH)
115116
],
117+
'checkoutcom_google_pay' => [
118+
'methodId' => 'checkoutcom_google_pay',
119+
'cardToken' => $this->session->getData(CheckoutComGooglePay::PAYMENT_TOKEN),
120+
'source' => $this->session->getData(CheckoutComGooglePay::PAYMENT_SOURCE)
121+
],
116122
'checkoutcom_apm' => $this->getApmData(),
117123
default => []
118124
};
@@ -163,7 +169,10 @@ public function placeOrder(Quote $quote): int
163169

164170
$isValidResponse = $api->isValidResponse($response);
165171

166-
if ($isValidResponse) {
172+
// Check if Google Pay response is declined
173+
$isDeclined = isset($response['status']) && $response['status'] === 'Declined';
174+
175+
if ($isValidResponse && !$isDeclined) {
167176
// Create an order if processing is payment first
168177
$order = $order === null ? $this->orderHandler->setMethodId($data['methodId'])->handleOrder($quote) : $order;
169178

@@ -182,10 +191,17 @@ public function placeOrder(Quote $quote): int
182191
// Save the order
183192
$this->orderRepository->save($order);
184193
// Update the response parameters
185-
$success = $isValidResponse;
194+
$success = true;
186195
} else {
187-
// Payment failed
188-
if (isset($response['response_code'])) {
196+
// Payment failed or declined
197+
if ($isDeclined) {
198+
// Handle declined payment specifically
199+
if (isset($response['response_summary'])) {
200+
$message = __('Your payment was declined. Reason: %1', $response['response_summary']);
201+
} else {
202+
$message = __('Your payment was declined.');
203+
}
204+
} elseif (isset($response['response_code'])) {
189205
$message = $this->paymentErrorHandler->getErrorMessage($response['response_code']);
190206
} else {
191207
$message = __('The transaction could not be processed.');
@@ -232,7 +248,7 @@ public function placeOrder(Quote $quote): int
232248
$this->logger->write($debugMessage);
233249
}
234250

235-
$this->setUrlRedirect('checkout/cart');
251+
$this->setUrlRedirect('hyva_checkout/index');
236252

237253
return 1;
238254
}

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ composer require magebitcom/magento2-hyva-checkout-checkoutcom-payment
1212
bin/magento module:enable Magebit_CheckoutComPayment && bin/magento setup:upgrade
1313
```
1414

15-
## Features
15+
## Feature Coverage
16+
17+
- [x] Card Payments (Multiple iframes)
18+
- [X] Vault
19+
- [x] Google Pay
20+
- [x] MB PAY
1621

1722
### Functionality that is currently not supported:
1823
* Adding a new Stored Card from My Account -> Stored Payment Methods
@@ -23,10 +28,22 @@ bin/magento module:enable Magebit_CheckoutComPayment && bin/magento setup:upgrad
2328

2429
### Payment methods the currently are not supported:
2530
* Apple Pay Payments
26-
* Google Pay Payments
2731
* Klarna (NAS)
2832
* Paypal Payments (NAS)
2933
* MOTO Payments
3034

3135
### Supported alternative payment methods:
3236
* MB PAY
37+
38+
### Google Pay Payments: New Configuration Options
39+
40+
* Button corner radius
41+
42+
This option sets the `border-radius` property of the button and is measured in pixels. There is no need to specify the
43+
CSS `px` unit in this option input field.
44+
45+
### Alternative payments: New Configuration Options
46+
47+
* Enable MB WAY Phone Validation
48+
49+
This option sets strict phone validation for MB WAY phone numbers (must be 9 digits and start with 9).

0 commit comments

Comments
 (0)