Skip to content

Commit 663e675

Browse files
authored
Merge pull request #1 from wychoong/wychoong-patch-1
fix
2 parents 8311f78 + 9c218a3 commit 663e675

File tree

5 files changed

+69
-18
lines changed

5 files changed

+69
-18
lines changed

resources/views/components/payment-form.blade.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,23 @@
1010
.then(resp => {
1111
this.initializing = false
1212
13+
if(resp.error){
14+
console.log('error')
15+
16+
return
17+
}
18+
1319
if(resp.session){
1420
this.processing = true
1521
22+
sessionStorage.removeItem('HostedCheckout_sessionId')
23+
24+
session = resp.session
25+
1626
Checkout.configure({
1727
session:{
18-
id: resp.session
28+
id: session.id,
29+
version: session.version,
1930
}
2031
});
2132
@@ -25,7 +36,6 @@
2536
}
2637
}"
2738
@checkout='checkout'
28-
@mpgs-complete.window='$wire.checkoutSuccess()'
2939
>
3040
<x-lunar-mpgs::embed-container />
3141
<x-lunar-mpgs::checkout-button />

src/Components/PaymentForm.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,48 @@ class PaymentForm extends Component
2121
'cardDetailsSubmitted',
2222
];
2323

24+
protected $queryString = [
25+
'resultIndicator',
26+
];
27+
28+
public ?string $resultIndicator = null;
29+
30+
public function mount()
31+
{
32+
if ($this->resultIndicator) {
33+
return $this->checkoutSuccess();
34+
}
35+
}
36+
2437
public function checkout()
2538
{
2639
$this->cart->calculate();
2740
$intent = MpgsFacade::createIntent($this->cart);
2841

42+
if ($intent === false) {
43+
return false;
44+
}
45+
2946
return [
3047
'session' => $intent,
3148
];
3249
}
3350

34-
public function checkoutSuccess()
51+
protected function checkoutSuccess()
3552
{
3653
$mpgs = (new MpgsPaymentType());
37-
$payment = $mpgs->cart($this->cart)->authorize();
54+
55+
$payment = $mpgs->cart($this->cart)->withData([
56+
'resultIndicator' => $this->resultIndicator,
57+
])->authorize();
3858

3959
if ($payment->success) {
4060
$this->emit('mpgsPaymentSuccess');
4161

4262
if (config('lunar-mpgs.route.payment-success')) {
43-
return redirect()->route(config('lunar-mpgs.route.payment-success'));
63+
$this->shouldSkipRender = false; //# workaround with an issue with livewire
64+
65+
return $this->redirectRoute(config('lunar-mpgs.route.payment-success'));
4466
}
4567

4668
return;
@@ -49,8 +71,11 @@ public function checkoutSuccess()
4971
$this->emit('mpgsPaymentFailed');
5072

5173
if (config('lunar-mpgs.route.payment-failed')) {
52-
return redirect()->route(config('lunar-mpgs.route.payment-failed'));
74+
$this->shouldSkipRender = false; //# workaround with an issue with livewire
75+
76+
return $this->redirectRoute(config('lunar-mpgs.route.payment-failed'));
5377
}
78+
5479
}
5580

5681
/**

src/Managers/MpgsManager.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static function notify($message, $error = false, $data = null)
4545
]);
4646
}
4747

48-
public function createIntent(Cart $cart): string
48+
public function createIntent(Cart $cart)
4949
{
5050
$meta = (array) $cart->meta;
5151

@@ -55,24 +55,32 @@ public function createIntent(Cart $cart): string
5555
$cart
5656
);
5757

58-
$paymentIntent = $result['intent'];
58+
$response = $result['response'];
5959
$orderId = $result['order_id'];
6060

61+
if ($response->result !== 'SUCCESS') {
62+
return [
63+
'error' => true,
64+
];
65+
}
66+
6167
if (! $meta) {
6268
$cart->update([
6369
'meta' => [
64-
'payment_intent' => $paymentIntent->session->id,
70+
'checkout_session' => $response->session->id,
71+
'success_indicator' => $response->successIndicator,
6572
'order_id' => $orderId,
6673
],
6774
]);
6875
} else {
69-
$meta['payment_intent'] = $paymentIntent->session->id;
76+
$meta['checkout_session'] = $response->session->id;
77+
$meta['success_indicator'] = $response->successIndicator;
7078
$meta['order_id'] = $orderId;
7179
$cart->meta = $meta;
7280
$cart->save();
7381
}
7482

75-
return $paymentIntent->session->id;
83+
return $response->session;
7684
}
7785

7886
public static function initiateCheckoutUsing(Closure $initiateCheckoutUsing)
@@ -90,7 +98,7 @@ protected function buildIntent($value, $currencyCode, $cart)
9098

9199
return [
92100
'order_id' => $data['order']['id'] ?? null,
93-
'intent' => Mpgs::initiateCheckout($data),
101+
'response' => Mpgs::initiateCheckout($data),
94102
];
95103
}
96104
}

src/MpgsPaymentServiceProvider.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,15 @@ public function packageBooted()
4141
Blade::directive('mpgsScripts', function () {
4242
$jsUrl = config('lunar-mpgs.checkout_js');
4343

44-
return <<<EOT
45-
<script src="$jsUrl"
46-
data-error="errorCallback"
47-
data-cancel="cancelCallback"
48-
data-complete="completeCallback">
44+
return <<<EOT
45+
<script type="text/javascript" src="$jsUrl"
46+
data-error="errorCallback" data-cancel="cancelCallback" data-complete="completeCallback" >
4947
</script>
5048
<script type="text/javascript">
5149
const et = new EventTarget();
5250
function errorCallback(error) {
53-
et.dispatchEvent(new Event('mpgs-error'));
51+
et.dispatchEvent(new Event('mpgs-error', error));
52+
console.log('error', error)
5453
}
5554
function cancelCallback() {
5655
et.dispatchEvent(new Event('mpgs-cancel'));

src/MpgsPaymentType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ public function authorize(): PaymentAuthorize
3737

3838
$meta = (array) $this->cart->meta;
3939

40+
$resultIndicator = $this->data['resultIndicator'];
41+
42+
if ($resultIndicator != $meta['success_indicator']) {
43+
return new PaymentAuthorize(
44+
success: false,
45+
message: 'Payment failed',
46+
);
47+
}
48+
4049
$orderId = $meta['order_id'];
4150
$this->orderResponse = Mpgs::retrieveOrder($orderId);
4251

0 commit comments

Comments
 (0)