Skip to content

Commit e057d55

Browse files
authored
Merge pull request #68 from xendit/TPI-3034/migrate-ovo
Migrate OVO to using XenInvoice
2 parents 70be897 + 8d0c7c8 commit e057d55

File tree

9 files changed

+15
-365
lines changed

9 files changed

+15
-365
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 2.5.0 (2020-12-16)
4+
5+
Improvements:
6+
7+
- Migrate OVO to using XenInvoice
8+
39
## 2.4.3 (2020-12-10)
410

511
Improvements:

Xendit/M2Invoice/Controller/Checkout/Redirect.php

-38
Original file line numberDiff line numberDiff line change
@@ -47,44 +47,6 @@ public function execute()
4747
return $this->_redirect('checkout/onepage/success', [ '_secure'=> false ]);
4848
}
4949

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-
}
86-
}
87-
8850
if ($payment->getAdditionalInformation('xendit_failure_reason') !== null) {
8951
$failureReason = $payment->getAdditionalInformation('xendit_failure_reason');
9052

Xendit/M2Invoice/Helper/ApiRequest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function getHeaders($isPublicRequest, $preferredMethod = null, $customHe
8585
'Content-Type' => 'application/json',
8686
'x-plugin-name' => 'MAGENTO2',
8787
'user-agent' => 'Magento 2 Module',
88-
'x-plugin-version' => '2.4.3'
88+
'x-plugin-version' => '2.5.0'
8989
];
9090

9191
if ($preferredMethod !== null) {

Xendit/M2Invoice/Model/Payment/OVO.php

-172
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,8 @@
22

33
namespace Xendit\M2Invoice\Model\Payment;
44

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-
185
class OVO extends AbstractInvoice
196
{
20-
const DEFAULT_EWALLET_TYPE = 'OVO';
217
/**
228
* Payment code
239
*
@@ -27,162 +13,4 @@ class OVO extends AbstractInvoice
2713
protected $_minAmount = 10000;
2814
protected $_maxAmount = 10000000;
2915
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-
}
18816
}

Xendit/M2Invoice/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "xendit/m2invoice",
33
"description": "Xendit Payment Gateway Module",
44
"type": "magento2-module",
5-
"version": "2.4.3",
5+
"version": "2.5.0",
66
"license": [
77
"GPL-3.0"
88
],

Xendit/M2Invoice/etc/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="Xendit_M2Invoice" setup_version="2.4.3" active="true"></module>
3+
<module name="Xendit_M2Invoice" setup_version="2.5.0" active="true"></module>
44
</config>

Xendit/M2Invoice/view/frontend/web/js/view/payment/method-renderer/ovo.js

+6-89
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@ define(
22
[
33
'Magento_Checkout/js/view/payment/default',
44
'mage/url',
5-
'Magento_Checkout/js/model/quote',
6-
'underscore',
7-
'jquery',
8-
'Magento_Checkout/js/action/place-order'
5+
'Magento_Checkout/js/model/quote'
96
],
107
function (
118
Component,
129
url,
13-
quote,
14-
_,
15-
$,
16-
placeOrderAction
17-
) {
10+
quote
11+
) {
1812
'use strict';
1913

2014
var self;
2115

2216
return Component.extend({
2317
defaults: {
24-
template: 'Xendit_M2Invoice/payment/ovo',
25-
redirectAfterPlaceOrder: false
18+
template: 'Xendit_M2Invoice/payment/invoiceva',
19+
redirectAfterPlaceOrder: false,
2620
},
2721

2822
initialize: function() {
@@ -59,23 +53,8 @@ define(
5953
};
6054
},
6155

62-
isActive: function() {
63-
return true;
64-
},
65-
6656
afterPlaceOrder: function () {
67-
if ($("[class='xendit-overlay-box']").length === 0) {
68-
var overlayDiv = $( "<div class='xendit-overlay-box'>" +
69-
"<div id='xendit-overlay-content'>\n" +
70-
" <span class='xendit-overlay-text' style='margin-top: 80px;'>Periksa kembali telepon selular Anda, buka aplikasi Ovo anda dan</span>\n" +
71-
" <span class='xendit-overlay-text'>konfirmasikan transaksi anda dengan memasukkan PIN</span>" +
72-
"</div>" +
73-
"</div>" );
74-
$( 'body' ).append(overlayDiv);
75-
}
76-
77-
$( "[class='xendit-overlay-box']" ).css("display", "flex");
78-
window.location.replace(url.build('xendit/checkout/redirect'));
57+
window.location.replace(url.build(`xendit/checkout/invoice?preferred_method=${self.getMethod()}`));
7958
},
8059

8160
validate: function() {
@@ -95,68 +74,6 @@ define(
9574
}
9675

9776
return true;
98-
},
99-
100-
placeOrder: function (data, event) {
101-
this.isPlaceOrderActionAllowed(false);
102-
var self = this;
103-
104-
try {
105-
var ovoPhoneNumber = $('#ovo_ovo_number').val();
106-
107-
if (!self.isPhoneNumber(ovoPhoneNumber)) {
108-
alert('Invalid OVO phone number, please check again');
109-
self.isPlaceOrderActionAllowed(true);
110-
self.unblock();
111-
return;
112-
}
113-
114-
var paymentData = self.getData();
115-
paymentData.additional_data = {
116-
phone_number: ovoPhoneNumber
117-
};
118-
119-
var placeOrder = placeOrderAction(paymentData, false);
120-
121-
$.when(placeOrder)
122-
.fail(function () {
123-
self.isPlaceOrderActionAllowed(true);
124-
self.unblock();
125-
})
126-
.done(function () {
127-
self.afterPlaceOrder();
128-
});
129-
return false;
130-
} catch (e) {
131-
alert(e);
132-
self.isPlaceOrderActionAllowed(true);
133-
self.unblock();
134-
}
135-
},
136-
137-
isPhoneNumber: function (string) {
138-
var pattern = /^\d+$/;
139-
140-
return pattern.test(string);
141-
},
142-
143-
block: function() {
144-
if ($("[class='xendit-overlay-box']").length === 0) {
145-
var overlayDiv = $( "<div class='xendit-overlay-box'>" +
146-
"<div id='xendit-overlay-content'>\n" +
147-
" <span class='xendit-overlay-text' style='margin-top: 80px;'>Periksa kembali telepon selular Anda, buka aplikasi Ovo anda dan</span>\n" +
148-
" <span class='xendit-overlay-text'>konfirmasikan transaksi anda dengan memasukkan PIN</span>" +
149-
"</div>" +
150-
"</div>" );
151-
$( 'body' ).append(overlayDiv);
152-
}
153-
154-
$( "[class='xendit-overlay-box']" ).css("display", "flex");
155-
// return;
156-
},
157-
158-
unblock: function() {
159-
$('.xendit-overlay-box').css("display", "none");
16077
}
16178
});
16279
}

0 commit comments

Comments
 (0)