Skip to content

Commit d5dd77a

Browse files
Merge pull request #58 from PaystackHQ/patch/magento-2
Magento-2 compatibility patch
2 parents 3db73bd + bbf5dad commit d5dd77a

8 files changed

Lines changed: 101 additions & 31 deletions

File tree

Controller/Payment/Setup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function processAuthorization(\Magento\Sales\Model\Order $order) {
5555
'email' => $order->getCustomerEmail(), // unique to customers
5656
'reference' => $order->getIncrementId(), // unique to transactions
5757
'currency' => $order->getCurrency(),
58-
'callback_url' => $this->configProvider->store->getBaseUrl() . "paystack/payment/callback",
58+
'callback_url' => $this->configProvider->getStore()->getBaseUrl() . "paystack/payment/callback",
5959
'metadata' => array('custom_fields' => array(
6060
array(
6161
"display_name"=>"Plugin",

Model/PaymentManagement.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(
6464

6565
/**
6666
* @param string $reference
67-
* @return bool
67+
* @return string
6868
*/
6969
public function verifyPayment($reference)
7070
{
@@ -80,25 +80,29 @@ public function verifyPayment($reference)
8080
]);
8181

8282
$order = $this->getOrder();
83-
//return json_encode($transaction_details);
8483
if ($order && $order->getQuoteId() === $quoteId && $transaction_details->data->metadata->quoteId === $quoteId) {
8584

8685
// dispatch the `paystack_payment_verify_after` event to update the order status
8786
$this->eventManager->dispatch('paystack_payment_verify_after', [
8887
"paystack_order" => $order,
8988
]);
9089

91-
return json_encode($transaction_details);
90+
// Return consistent response format
91+
return json_encode([
92+
'status' => true,
93+
'message' => 'Verification successful',
94+
'data' => $transaction_details->data
95+
]);
9296
}
9397
} catch (Exception $e) {
9498
return json_encode([
95-
'status'=>0,
96-
'message'=>$e->getMessage()
99+
'status' => false,
100+
'message' => $e->getMessage()
97101
]);
98102
}
99103
return json_encode([
100-
'status'=>0,
101-
'message'=>"quoteId doesn't match transaction"
104+
'status' => false,
105+
'message' => "quoteId doesn't match transaction"
102106
]);
103107
}
104108

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pstk/paystack-magento2-module",
33
"description": "Paystack Magento2 Module using \\Magento\\Payment\\Model\\Method\\AbstractMethod",
4-
"version": "2.4.1",
4+
"version": "2.5.0",
55
"require": {
66
"yabacon/paystack-php": "2.*"
77
},

composer.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

etc/frontend/csp_whitelist.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0"?>
2+
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Csp/etc/csp_whitelist.xsd">
4+
5+
<policy id="script-src">
6+
<values>
7+
<value id="paystack-js" type="host">js.paystack.co</value>
8+
<value id="paystack-api" type="host">api.paystack.co</value>
9+
</values>
10+
</policy>
11+
12+
<policy id="connect-src">
13+
<values>
14+
<value id="paystack-api-connect" type="host">api.paystack.co</value>
15+
<value id="paystack-js-connect" type="host">js.paystack.co</value>
16+
<value id="paystack-tracker" type="host">plugin-tracker.paystackintegrations.com</value>
17+
</values>
18+
</policy>
19+
20+
<policy id="frame-src">
21+
<values>
22+
<value id="paystack-standard" type="host">standard.paystack.co</value>
23+
</values>
24+
</policy>
25+
26+
</csp_whitelist>

view/frontend/requirejs-config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var config = {
2+
paths: {
3+
'paystack': 'https://js.paystack.co/v2/inline'
4+
},
5+
shim: {
6+
'paystack': {
7+
exports: 'PaystackPop'
8+
}
9+
}
10+
};

view/frontend/templates/page/js/require_js.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222

2323
?>
24-
<script src="https://js.paystack.co/v1/inline.js"></script>
24+
<script src="https://js.paystack.co/v2/inline.js"></script>
2525
<script>
2626
var BASE_URL = '<?= $block->escapeUrl($block->getBaseUrl()) ?>';
2727
var require = {

view/frontend/web/js/view/payment/method-renderer/pstk_paystack-method.js

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ define(
88
"Magento_Checkout/js/model/quote",
99
"Magento_Checkout/js/model/full-screen-loader",
1010
"Magento_Checkout/js/action/redirect-on-success",
11+
"paystack"
1112
],
1213
function (
1314
$,
@@ -17,7 +18,8 @@ define(
1718
additionalValidators,
1819
quote,
1920
fullScreenLoader,
20-
redirectOnSuccessAction
21+
redirectOnSuccessAction,
22+
PaystackPop
2123
) {
2224
'use strict';
2325

@@ -44,7 +46,6 @@ define(
4446
* @override
4547
*/
4648
afterPlaceOrder: function () {
47-
4849
var checkoutConfig = window.checkoutConfig;
4950
var paymentData = quote.billingAddress();
5051
var paystackConfiguration = checkoutConfig.payment.pstk_paystack;
@@ -63,7 +64,9 @@ define(
6364

6465
var _this = this;
6566
_this.isPlaceOrderActionAllowed(false);
66-
var handler = PaystackPop.setup({
67+
68+
var popup = new PaystackPop();
69+
popup.newTransaction({
6770
key: paystackConfiguration.public_key,
6871
email: paymentData.email,
6972
amount: Math.ceil(quote.totals().grand_total * 100), // get order total from quote for an accurate... quote
@@ -99,14 +102,31 @@ define(
99102
}
100103
]
101104
},
102-
callback: function (response) {
105+
onSuccess: function (response) {
103106
fullScreenLoader.startLoader();
104107
$.ajax({
105108
method: "GET",
106-
url: paystackConfiguration.api_url + "V1/paystack/verify/" + response.reference + "_-~-_" + quoteId
107-
}).success(function (data) {
108-
data = JSON.parse(data);
109-
//JS PSTK-logger
109+
url: paystackConfiguration.api_url + "V1/paystack/verify/" + response.reference + "_-~-_" + quoteId,
110+
dataType: 'text'
111+
}).done(function (data) {
112+
// Parse JSON response (may be double-encoded)
113+
try {
114+
data = JSON.parse(data);
115+
// Check if it's still a string (double-encoded)
116+
if (typeof data === 'string') {
117+
data = JSON.parse(data);
118+
}
119+
} catch (e) {
120+
console.error('Payment verification JSON parse error:', e);
121+
fullScreenLoader.stopLoader();
122+
_this.isPlaceOrderActionAllowed(true);
123+
_this.messageContainer.addErrorMessage({
124+
message: "Payment verification error."
125+
});
126+
return;
127+
}
128+
129+
// Log payment success
110130
$.ajax({
111131
method: 'POST',
112132
url: "https://plugin-tracker.paystackintegrations.com/log/charge_success",
@@ -115,28 +135,31 @@ define(
115135
transaction_reference: response.reference,
116136
public_key: paystackConfiguration.public_key
117137
}
118-
})
119-
if (data.status) {
120-
if (data.data.status === "success") {
121-
// redirect to success page after
122-
redirectOnSuccessAction.execute();
123-
return;
124-
}
138+
});
139+
140+
// Check if payment was successful
141+
if (data.status && data.data && data.data.status === "success") {
142+
redirectOnSuccessAction.execute();
143+
return;
125144
}
126-
145+
// Payment verification failed
146+
fullScreenLoader.stopLoader();
147+
_this.isPlaceOrderActionAllowed(true);
148+
_this.messageContainer.addErrorMessage({
149+
message: "Payment verification failed. Status: " + (data.data ? data.data.status : 'unknown')
150+
});
151+
}).fail(function () {
127152
fullScreenLoader.stopLoader();
128-
129153
_this.isPlaceOrderActionAllowed(true);
130154
_this.messageContainer.addErrorMessage({
131-
message: "Error, please try again"
155+
message: "Payment verification failed."
132156
});
133157
});
134158
},
135-
onClose: function(){
159+
onCancel: function(){
136160
_this.redirectToCustomAction(paystackConfiguration.recreate_quote_url);
137161
}
138162
});
139-
handler.openIframe();
140163
}
141164
},
142165

0 commit comments

Comments
 (0)