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