10
10
use Magento \Sales \Model \Order ;
11
11
use Xendit \M2Invoice \Enum \LogDNALevel ;
12
12
13
+ /**
14
+ * This callback is only for order in multishipping flow. For order
15
+ * created in onepage checkout is handled in ProcessHosted.php
16
+ */
13
17
class CCCallback extends ProcessHosted implements CsrfAwareActionInterface
14
18
{
15
19
public function execute ()
16
20
{
17
21
try {
18
- $ orderIds = explode ('- ' , $ this ->getRequest ()->getParam ('order_ids ' ));
22
+ $ post = $ this ->getRequest ()->getContent ();
23
+ $ callbackPayload = json_decode ($ post , true );
24
+
25
+ if (
26
+ !isset ($ callbackPayload ['id ' ]) ||
27
+ !isset ($ callbackPayload ['hp_token ' ]) ||
28
+ !isset ($ callbackPayload ['order_number ' ])
29
+ ) {
30
+ $ result = $ this ->getJsonResultFactory ()->create ();
31
+ $ result ->setHttpResponseCode (\Magento \Framework \Webapi \Exception::HTTP_BAD_REQUEST );
32
+ $ result ->setData ([
33
+ 'status ' => __ ('ERROR ' ),
34
+ 'message ' => 'Callback body is invalid '
35
+ ]);
36
+
37
+ return $ result ;
38
+ }
39
+ $ orderIds = explode ('- ' , $ callbackPayload ['order_number ' ]);
40
+ $ hostedPaymentId = $ callbackPayload ['id ' ];
41
+ $ hostedPaymentToken = $ callbackPayload ['hp_token ' ];
19
42
20
43
$ shouldRedirect = false ;
21
44
$ isError = false ;
@@ -27,40 +50,49 @@ public function execute()
27
50
28
51
$ payment = $ order ->getPayment ();
29
52
30
- if ($ payment ->getAdditionalInformation ('xendit_hosted_payment_id ' ) !== null ) {
31
- $ requestData = [
32
- 'id ' => $ payment ->getAdditionalInformation ('xendit_hosted_payment_id ' ),
33
- 'hp_token ' => $ payment ->getAdditionalInformation ('xendit_hosted_payment_token ' )
34
- ];
35
-
36
- if ($ flag ) { // complete hosted payment only once as status will be changed to USED
37
- $ hostedPayment = $ this ->getCompletedHostedPayment ($ requestData );
38
- $ flag = false ;
39
- }
40
-
41
- if (isset ($ hostedPayment ['error_code ' ])) {
42
- $ isError = true ;
53
+ $ requestData = [
54
+ 'id ' => $ hostedPaymentId ,
55
+ 'hp_token ' => $ hostedPaymentToken
56
+ ];
57
+
58
+ if ($ flag ) { // complete hosted payment only once as status will be changed to USED
59
+ $ hostedPayment = $ this ->getCompletedHostedPayment ($ requestData );
60
+ $ flag = false ;
61
+ }
62
+
63
+ if (isset ($ hostedPayment ['error_code ' ])) {
64
+ $ isError = true ;
65
+ }
66
+ else {
67
+ if ($ hostedPayment ['order_number ' ] !== $ callbackPayload ['order_number ' ]) {
68
+ $ result = $ this ->getJsonResultFactory ()->create ();
69
+ $ result ->setHttpResponseCode (\Magento \Framework \Webapi \Exception::HTTP_BAD_REQUEST );
70
+ $ result ->setData ([
71
+ 'status ' => __ ('ERROR ' ),
72
+ 'message ' => 'Hosted payment is not for this order '
73
+ ]);
74
+
75
+ return $ result ;
43
76
}
44
- else {
45
- if ($ hostedPayment ['paid_amount ' ] != $ hostedPayment ['amount ' ]) {
46
- $ order ->setBaseDiscountAmount ($ hostedPayment ['paid_amount ' ] - $ hostedPayment ['amount ' ]);
47
- $ order ->setDiscountAmount ($ hostedPayment ['paid_amount ' ] - $ hostedPayment ['amount ' ]);
48
- $ order ->save ();
49
-
50
- $ order ->setBaseGrandTotal ($ order ->getBaseGrandTotal () + $ order ->getBaseDiscountAmount ());
51
- $ order ->setGrandTotal ($ order ->getGrandTotal () + $ order ->getDiscountAmount ());
52
- $ order ->save ();
53
- }
54
- $ payment ->setAdditionalInformation ('token_id ' , $ hostedPayment ['token_id ' ]);
55
- $ payment ->setAdditionalInformation ('xendit_installment ' , $ hostedPayment ['installment ' ]);
77
+
78
+ if ($ hostedPayment ['paid_amount ' ] != $ hostedPayment ['amount ' ]) {
79
+ $ order ->setBaseDiscountAmount ($ hostedPayment ['paid_amount ' ] - $ hostedPayment ['amount ' ]);
80
+ $ order ->setDiscountAmount ($ hostedPayment ['paid_amount ' ] - $ hostedPayment ['amount ' ]);
81
+ $ order ->save ();
56
82
57
- $ this ->processSuccessfulTransaction (
58
- $ order ,
59
- $ payment ,
60
- 'Xendit Credit Card payment completed. Transaction ID: ' ,
61
- $ hostedPayment ['charge_id ' ]
62
- );
83
+ $ order ->setBaseGrandTotal ($ order ->getBaseGrandTotal () + $ order ->getBaseDiscountAmount ());
84
+ $ order ->setGrandTotal ($ order ->getGrandTotal () + $ order ->getDiscountAmount ());
85
+ $ order ->save ();
63
86
}
87
+ $ payment ->setAdditionalInformation ('token_id ' , $ hostedPayment ['token_id ' ]);
88
+ $ payment ->setAdditionalInformation ('xendit_installment ' , $ hostedPayment ['installment ' ]);
89
+
90
+ $ this ->processSuccessfulTransaction (
91
+ $ order ,
92
+ $ payment ,
93
+ 'Xendit Credit Card payment completed. Transaction ID: ' ,
94
+ $ hostedPayment ['charge_id ' ]
95
+ );
64
96
}
65
97
}
66
98
0 commit comments