@@ -12,7 +12,14 @@ public function execute()
12
12
{
13
13
try {
14
14
$ order = $ this ->getOrder ();
15
+ $ orderId = $ order ->getRealOrderId ();
15
16
$ payment = $ order ->getPayment ();
17
+ $ this ->getMessageManager ()->getMessages (true );
18
+
19
+ $ orderState = Order::STATE_PENDING_PAYMENT ;
20
+ $ order ->setState ($ orderState )
21
+ ->setStatus ($ orderState );
22
+ $ order ->save ();
16
23
17
24
if ($ payment ->getAdditionalInformation ('xendit_redirect_url ' ) !== null ) {
18
25
$ redirectUrl = $ payment ->getAdditionalInformation ('xendit_redirect_url ' );
@@ -40,21 +47,50 @@ public function execute()
40
47
return $ this ->_redirect ('checkout/onepage/success ' , [ '_secure ' => false ]);
41
48
}
42
49
43
- if ($ payment ->getAdditionalInformation ('xendit_ewallet_id ' ) !== null ) {
44
- $ this ->getMessageManager ()->addSuccessMessage (__ ("Your payment with Xendit is completed " ));
45
- return $ this ->_redirect ('checkout/onepage/success ' , [ '_secure ' => false ]);
50
+ if ($ payment ->getAdditionalInformation ('xendit_ovo_external_id ' ) !== null ) {
51
+ $ isSuccessful = false ;
52
+ $ loopCondition = true ;
53
+ $ startTime = time ();
54
+ while ($ loopCondition && (time () - $ startTime < 70 )) {
55
+ $ order = $ this ->getOrderById ($ orderId );
56
+
57
+ if ($ order ->getState () !== Order::STATE_PENDING_PAYMENT ) {
58
+ $ loopCondition = false ;
59
+ $ isSuccessful = $ order ->getState () === Order::STATE_PROCESSING ;
60
+ }
61
+ sleep (1 );
62
+ }
63
+
64
+ if ($ order ->getState () === Order::STATE_PENDING_PAYMENT ) {
65
+ $ ewalletStatus = $ this ->getEwalletStatus ('OVO ' , $ payment ->getAdditionalInformation ('xendit_ovo_external_id ' ));
66
+
67
+ if ($ ewalletStatus === 'COMPLETED ' ) {
68
+ $ isSuccessful = true ;
69
+ }
70
+ }
71
+
72
+ if ($ isSuccessful ) {
73
+ $ this ->getMessageManager ()->addSuccessMessage (__ ("Your payment with Xendit is completed " ));
74
+ return $ this ->_redirect ('checkout/onepage/success ' , [ '_secure ' => false ]);
75
+ } else {
76
+ $ payment = $ order ->getPayment ();
77
+ $ failureCode = $ payment ->getAdditionalInformation ('xendit_ewallet_failure_code ' );
78
+
79
+ if ($ failureCode === null ) {
80
+ $ failureCode = 'Payment is ' . $ ewalletStatus ;
81
+ }
82
+
83
+ $ this ->getCheckoutHelper ()->restoreQuote ();
84
+ return $ this ->redirectToCart ($ failureCode );
85
+ }
46
86
}
47
87
48
88
if ($ payment ->getAdditionalInformation ('xendit_failure_reason ' ) !== null ) {
49
89
$ failureReason = $ payment ->getAdditionalInformation ('xendit_failure_reason ' );
50
90
51
91
$ this ->cancelOrder ($ order , $ failureReason );
52
92
53
- $ failureReasonInsight = $ this ->getDataHelper ()->failureReasonInsight ($ failureReason );
54
- $ this ->getMessageManager ()->addErrorMessage (__ (
55
- $ failureReasonInsight
56
- ));
57
- return $ this ->_redirect ('checkout/cart ' , [ '_secure ' => false ]);
93
+ return $ this ->redirectToCart ($ failureReason );
58
94
}
59
95
60
96
if ($ payment ->getAdditionalInformation ('xendit_hosted_payment_id ' ) !== null ) {
@@ -76,20 +112,50 @@ public function execute()
76
112
77
113
$ this ->cancelOrder ($ order , 'No payment recorded ' );
78
114
79
- $ this ->getMessageManager ()->addErrorMessage (__ (
80
- "There was an error in the Xendit payment. Failure reason: No payment recorded "
81
- ));
82
- return $ this ->_redirect ('checkout/cart ' , [ '_secure ' => false ]);
115
+ return $ this ->redirectToCart ("There was an error in the Xendit payment. Failure reason: Unexpected Error " );
83
116
} catch (\Exception $ e ) {
84
117
$ message = 'Exception caught on xendit/checkout/redirect: ' . $ e ->getMessage ();
85
118
$ this ->getLogDNA ()->log (LogDNALevel::ERROR , $ message );
86
119
87
120
$ this ->cancelOrder ($ order , $ e ->getMessage ());
88
121
89
- $ this ->getMessageManager ()->addErrorMessage (__ (
90
- "There was an error in the Xendit payment. Failure reason: Unexpected Error "
91
- ));
92
- return $ this ->_redirect ('checkout/cart ' , [ '_secure ' => false ]);
122
+ return $ this ->redirectToCart ("There was an error in the Xendit payment. Failure reason: Unexpected Error " );
123
+ }
124
+ }
125
+
126
+ private function getEwalletStatus ($ ewalletType , $ externalId )
127
+ {
128
+ $ ewalletUrl = $ this ->getDataHelper ()->getCheckoutUrl () . "/payment/xendit/ewallets?ewallet_type= " .$ ewalletType ."&external_id= " .$ externalId ;
129
+ $ ewalletMethod = \Zend \Http \Request::METHOD_GET ;
130
+
131
+ try {
132
+ $ response = $ this ->getApiHelper ()->request ($ ewalletUrl , $ ewalletMethod );
133
+ } catch (\Magento \Framework \Exception \LocalizedException $ e ) {
134
+ throw new LocalizedException (
135
+ new Phrase ($ e ->getMessage ())
136
+ );
137
+ }
138
+
139
+ if ($ ewalletType == 'DANA ' ) {
140
+ $ response ['status ' ] = $ response ['payment_status ' ];
141
+ }
142
+
143
+ $ statusList = array ("COMPLETED " , "PAID " , "SUCCESS_COMPLETED " ); //OVO, DANA, LINKAJA
144
+ if (in_array ($ response ['status ' ], $ statusList )) {
145
+ return "COMPLETED " ;
93
146
}
147
+
148
+ return $ response ['status ' ];
149
+ }
150
+
151
+ private function redirectToCart ($ failureReason )
152
+ {
153
+ $ failureReasonInsight = $ this ->getDataHelper ()->failureReasonInsight ($ failureReason );
154
+ $ this ->getMessageManager ()->addErrorMessage (__ (
155
+ $ failureReasonInsight
156
+ ));
157
+ $ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
158
+ $ resultRedirect ->setUrl ($ this ->_url ->getUrl ('checkout/cart ' ), [ '_secure ' => false ]);
159
+ return $ resultRedirect ;
94
160
}
95
161
}
0 commit comments