2
2
3
3
namespace Xendit \M2Invoice \Model \Payment ;
4
4
5
- use Magento \Framework \Api \ExtensionAttributesFactory ;
6
- use Magento \Framework \Api \AttributeValueFactory ;
7
- use Magento \Framework \App \Config \ScopeConfigInterface ;
8
- use Magento \Framework \Model \Context ;
9
- use Magento \Framework \Phrase ;
10
- use Magento \Framework \Registry ;
11
- use Magento \Sales \Model \Order ;
12
- use Magento \Payment \Helper \Data ;
13
- use Magento \Payment \Model \Method \Logger ;
14
- use Xendit \M2Invoice \Helper \ApiRequest ;
15
- use Xendit \M2Invoice \Helper \LogDNA ;
16
- use Xendit \M2Invoice \Enum \LogDNALevel ;
17
-
18
5
class OVO extends AbstractInvoice
19
6
{
20
- const DEFAULT_EWALLET_TYPE = 'OVO ' ;
21
7
/**
22
8
* Payment code
23
9
*
@@ -27,162 +13,4 @@ class OVO extends AbstractInvoice
27
13
protected $ _minAmount = 10000 ;
28
14
protected $ _maxAmount = 10000000 ;
29
15
protected $ methodCode = 'OVO ' ;
30
-
31
- public function authorize (\Magento \Payment \Model \InfoInterface $ payment , $ amount )
32
- {
33
- $ payment ->setIsTransactionPending (true );
34
- $ additionalData = $ this ->getAdditionalData ();
35
-
36
- $ order = $ payment ->getOrder ();
37
- $ quoteId = $ order ->getQuoteId ();
38
- $ quote = $ this ->quoteRepository ->get ($ quoteId );
39
-
40
- if ($ quote ->getIsMultiShipping ()) {
41
- return $ this ;
42
- }
43
-
44
- $ orderId = $ order ->getRealOrderId ();
45
- $ billing = $ order ->getBillingAddress ();
46
- $ phone = trim (!empty ($ additionalData ['phone_number ' ]) ? $ additionalData ['phone_number ' ] : $ billing ->getTelephone ());
47
-
48
- try {
49
- // validate phone number
50
- if (!$ phone || strpos ($ phone , '08 ' ) !== 0 ) {
51
- $ message = $ this ->mapOvoErrorCode ('INVALID_PHONE_NUMBER ' );
52
- throw new \Magento \Framework \Exception \LocalizedException (
53
- new Phrase ($ message )
54
- );
55
- }
56
-
57
- $ args = [
58
- 'external_id ' => $ this ->dataHelper ->getExternalId ($ orderId ),
59
- 'amount ' => $ amount ,
60
- 'phone ' => $ phone ,
61
- 'ewallet_type ' => self ::DEFAULT_EWALLET_TYPE ,
62
- 'platform_callback_url ' => $ this ->getXenditCallbackUrl ()
63
- ];
64
-
65
- $ ewalletPayment = $ this ->requestEwalletPayment ($ args );
66
-
67
- if (isset ($ ewalletPayment ['error_code ' ])) {
68
- if ($ ewalletPayment ['error_code ' ] == 'DUPLICATE_PAYMENT_REQUEST_ERROR ' ) {
69
- $ args = array_replace ($ args , array (
70
- 'external_id ' => $ this ->dataHelper ->getExternalId ($ orderId , true )
71
- ));
72
- $ ewalletPayment = $ this ->requestEwalletPayment ($ args );
73
- }
74
-
75
- if (isset ($ ewalletPayment ['error_code ' ])) {
76
- $ message = $ this ->mapOvoErrorCode ($ ewalletPayment ['error_code ' ]);
77
- $ this ->processFailedPayment ($ payment , $ message );
78
-
79
- throw new \Magento \Framework \Exception \LocalizedException (
80
- new Phrase ($ message )
81
- );
82
- }
83
- }
84
-
85
- $ payment ->setAdditionalInformation ('xendit_ovo_external_id ' , $ ewalletPayment ['external_id ' ]);
86
- } catch (\Exception $ e ) {
87
- $ errorMsg = $ e ->getMessage ();
88
- throw new \Magento \Framework \Exception \LocalizedException (
89
- new Phrase ($ errorMsg )
90
- );
91
- }
92
-
93
- return $ this ;
94
- }
95
-
96
- private function requestEwalletPayment ($ requestData , $ isRetried = true )
97
- {
98
- $ ewalletUrl = $ this ->dataHelper ->getCheckoutUrl () . "/payment/xendit/ewallets " ;
99
- $ ewalletMethod = \Zend \Http \Request::METHOD_POST ;
100
- $ options = [
101
- 'timeout ' => 60
102
- ];
103
-
104
- try {
105
- $ ewalletPayment = $ this ->apiHelper ->request (
106
- $ ewalletUrl ,
107
- $ ewalletMethod ,
108
- $ requestData ,
109
- null ,
110
- null ,
111
- $ options ,
112
- [
113
- 'x-api-version ' => '2020-02-01 '
114
- ]
115
- );
116
- } catch (\Exception $ e ) {
117
- throw $ e ;
118
- }
119
-
120
- return $ ewalletPayment ;
121
- }
122
-
123
- private function getAdditionalData ()
124
- {
125
- static $ data = [];
126
- if (count ($ data ) < 1 ) {
127
- $ data = (array ) $ this ->getPaymentMethod ();
128
- }
129
-
130
- return $ this ->elementFromArray ($ data , 'additional_data ' );
131
- }
132
-
133
- private function getPaymentMethod ()
134
- {
135
- /**
136
- * @var array $data
137
- * Holds submitted JSOn data in a PHP associative array
138
- */
139
- static $ data = [];
140
- if (count ($ data ) < 1 ) {
141
- $ data = (array ) $ this ->dataHelper ->jsonData ();
142
- }
143
- return $ this ->elementFromArray ($ data , 'paymentMethod ' );
144
- }
145
-
146
- private function elementFromArray ($ data , $ element )
147
- {
148
- $ r = [];
149
- if (key_exists ($ element , $ data )) {
150
- $ r = (array ) $ data [$ element ];
151
- }
152
-
153
- return $ r ;
154
- }
155
-
156
- private function processFailedPayment ($ payment , $ message )
157
- {
158
- $ payment ->setAdditionalInformation ('xendit_failure_reason ' , $ message );
159
- }
160
-
161
- private function mapOvoErrorCode ($ errorCode , $ message = '' )
162
- {
163
- switch ($ errorCode ) {
164
- case 'USER_DID_NOT_AUTHORIZE_THE_PAYMENT ' :
165
- return 'Please complete the payment request within 60 seconds. ' ;
166
- case 'USER_DECLINED_THE_TRANSACTION ' :
167
- return 'You rejected the payment request, please try again when needed. ' ;
168
- case 'PHONE_NUMBER_NOT_REGISTERED ' :
169
- return 'Your number is not registered in OVO, please register first or contact OVO Customer Service. ' ;
170
- case 'EXTERNAL_ERROR ' :
171
- return 'There is a technical issue happens on OVO, please contact the merchant to solve this issue. ' ;
172
- case 'SENDING_TRANSACTION_ERROR ' :
173
- return 'Your transaction is not sent to OVO, please try again. ' ;
174
- case 'EWALLET_APP_UNREACHABLE ' :
175
- return 'Do you have OVO app on your phone? Please check your OVO app on your phone and try again. ' ;
176
- case 'REQUEST_FORBIDDEN_ERROR ' :
177
- return 'Your merchant disable OVO payment from his side, please contact your merchant to re-enable it
178
- before trying it again. ' ;
179
- case 'DEVELOPMENT_MODE_PAYMENT_ACKNOWLEDGED ' :
180
- return 'Development mode detected. Please refer to our documentations for successful payment
181
- simulation ' ;
182
- case 'INVALID_PHONE_NUMBER ' :
183
- return 'Phone number is either missing or incorrect. Format should be 08XXXXXXXXX (not using "+62"). ' ;
184
- default :
185
- return "Failed to pay with eWallet. Error code: $ errorCode " ;
186
- }
187
- }
188
16
}
0 commit comments