Skip to content

Commit

Permalink
Merge pull request #8 from xendit/TPI-1044/error-handling
Browse files Browse the repository at this point in the history
OC 3.0 FO error handling
  • Loading branch information
hakiemaul authored Jan 15, 2020
2 parents cc27a08 + f8cd951 commit c3b9e23
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public function process_payment()
try {
$response = Xendit::request($request_url, Xendit::METHOD_POST, $request_payload, $request_options);

if(array_key_exists("error_code", $response)) {
$json = $response;
} else {
if (isset($response['error_code'])) {
$json['error'] = $response['message'];
}
else {
$this->model_payment_xendit->addOrder($order, $response, $this->config->get('xendit_environment'));
$message = 'Invoice ID: ' . $response['id'] . '. Redirecting..';
$this->model_checkout_order->addOrderHistory(
Expand Down
23 changes: 12 additions & 11 deletions opencart2.0.x-2.2.x/upload/catalog/controller/payment/xenditcc.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ public function process_payment() {
$response = Xendit::request($request_url, Xendit::METHOD_POST, $request_payload, $request_options);

if (isset($response['error_code'])) {
throw new Exception('Failed to authenticate, please try again');
$json['error'] = 'Failed to authenticate, please try again.';
}
else {
$message = 'Authentication ID: ' . $response['id'] . '. Authenticating..';
$this->model_checkout_order->addOrderHistory(
$order_id,
1,
$message,
false
);

$json['redirect'] = $response['redirect']['url'];
}

$message = 'Authentication ID: ' . $response['id'] . '. Authenticating..';
$this->model_checkout_order->addOrderHistory(
$order_id,
1,
$message,
false
);

$json['redirect'] = $response['redirect']['url'];

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@
$('#button-confirm').button('reset');
},
success: function(json) {
if(json['error_code']) {
alert(json['message']);
} else {
if (json['redirect']) {
location = json['redirect'] + '#' + $('#invoice-hash').val();
}
if (json['error']) {
alert(json['error']);
}
if (json['redirect']) {
location = json['redirect'] + '#' + $('#invoice-hash').val();
}
},
error: function(xhr, ajaxOptions, thrownError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</div>

<script src="https://js.xendit.co/v1/xendit.min.js"></script>
<script type="text/javascript"><!--
<script type="text/javascript">
var buttonConfirm = $('#button-confirm');
buttonConfirm.on('click', function() {
buttonConfirm.button('loading');
Expand Down Expand Up @@ -129,6 +129,9 @@
$('#button-confirm').button('reset');
},
success: function(json) {
if (json['error']) {
alert(json['error']);
}
if (json['redirect']) {
location = json['redirect'];
}
Expand All @@ -139,7 +142,7 @@
});
});
});
//--></script>
</script>

<style>
.test_instructions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,21 @@ public function process_payment() {
try {
$response = Xendit::request($request_url, Xendit::METHOD_POST, $request_payload, $request_options);

$this->model_extension_payment_xendit->addOrder($order, $response, $this->config->get('xendit_environment'));
$message = 'Invoice ID: ' . $response['id'] . '. Redirecting..';
$this->model_checkout_order->addOrderHistory(
$order_id,
1,
$message,
false
);

$json['redirect'] = $response['invoice_url'];
if (isset($response['error_code'])) {
$json['error'] = $response['message'];
}
else {
$this->model_extension_payment_xendit->addOrder($order, $response, $this->config->get('xendit_environment'));
$message = 'Invoice ID: ' . $response['id'] . '. Redirecting..';
$this->model_checkout_order->addOrderHistory(
$order_id,
1,
$message,
false
);

$json['redirect'] = $response['invoice_url'];
}

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@ public function process_payment() {
$response = Xendit::request($request_url, Xendit::METHOD_POST, $request_payload, $request_options);

if (isset($response['error_code'])) {
throw new Exception('Failed to authenticate, please try again');
$json['error'] = 'Failed to authenticate, please try again.';
}
else {
$message = 'Authentication ID: ' . $response['id'] . '. Authenticating..';
$this->model_checkout_order->addOrderHistory(
$order_id,
1,
$message,
false
);

$json['redirect'] = $response['redirect']['url'];
}

$message = 'Authentication ID: ' . $response['id'] . '. Authenticating..';
$this->model_checkout_order->addOrderHistory(
$order_id,
1,
$message,
false
);

$json['redirect'] = $response['redirect']['url'];

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/>
</div>
</div>
<script type="text/javascript"><!--
<script type="text/javascript">
$('#button-confirm').on('click', function() {
$.ajax({
url: 'index.php?route=extension/payment/xendit/process_payment',
Expand All @@ -39,6 +39,9 @@
$('#button-confirm').button('reset');
},
success: function(json) {
if (json['error']) {
alert(json['error']);
}
if (json['redirect']) {
location = json['redirect'] + '#' + $('#invoice-hash').val();
}
Expand All @@ -48,7 +51,7 @@
}
});
});
//--></script>
</script>

<style>
.test_instructions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
buttonConfirm.button('reset');
alert('Tokenization error. Error code:' + err.error_code);
return;
}
Expand All @@ -129,6 +128,9 @@
$('#button-confirm').button('reset');
},
success: function(json) {
if (json['error']) {
alert(json['error']);
}
if (json['redirect']) {
location = json['redirect'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,21 @@ public function process_payment() {
try {
$response = Xendit::request($request_url, Xendit::METHOD_POST, $request_payload, $request_options);

$this->model_extension_payment_xendit->addOrder($order, $response, $this->config->get('payment_xendit_environment'));
$message = 'Invoice ID: ' . $response['id'] . '. Redirecting..';
$this->model_checkout_order->addOrderHistory(
$order_id,
1,
$message,
false
);

$json['redirect'] = $response['invoice_url'];
if (isset($response['error_code'])) {
$json['error'] = $response['message'];
}
else {
$this->model_extension_payment_xendit->addOrder($order, $response, $this->config->get('payment_xendit_environment'));
$message = 'Invoice ID: ' . $response['id'] . '. Redirecting..';
$this->model_checkout_order->addOrderHistory(
$order_id,
1,
$message,
false
);

$json['redirect'] = $response['invoice_url'];
}

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,20 @@ public function process_payment() {
$response = Xendit::request($request_url, Xendit::METHOD_POST, $request_payload, $request_options);

if (isset($response['error_code'])) {
throw new Exception('Failed to authenticate, please try again');
$json['error'] = 'Failed to authenticate, please try again.';
}

$message = 'Authentication ID: ' . $response['id'] . '. Authenticating..';
$this->model_checkout_order->addOrderHistory(
$order_id,
1,
$message,
false
);

$json['redirect'] = $response['redirect']['url'];

else {
$message = 'Authentication ID: ' . $response['id'] . '. Authenticating..';
$this->model_checkout_order->addOrderHistory(
$order_id,
1,
$message,
false
);

$json['redirect'] = $response['redirect']['url'];
}

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
} catch (Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/>
</div>
</div>
<script type="text/javascript"><!--
<script type="text/javascript">
$('#button-confirm').on('click', function() {
$.ajax({
url: 'index.php?route=extension/payment/xendit/process_payment',
Expand All @@ -39,6 +39,9 @@
$('#button-confirm').button('reset');
},
success: function(json) {
if (json['error']) {
alert(json['error']);
}
if (json['redirect']) {
location = json['redirect'] + '#' + $('#invoice-hash').val();
}
Expand All @@ -48,7 +51,7 @@
}
});
});
//--></script>
</script>

<style>
.test_instructions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</div>

<script src="https://js.xendit.co/v1/xendit.min.js"></script>
<script type="text/javascript"><!--
<script type="text/javascript">
var buttonConfirm = $('#button-confirm');
buttonConfirm.on('click', function() {
buttonConfirm.button('loading');
Expand Down Expand Up @@ -129,6 +129,9 @@
$('#button-confirm').button('reset');
},
success: function(json) {
if (json['error']) {
alert(json['error']);
}
if (json['redirect']) {
location = json['redirect'];
}
Expand All @@ -139,7 +142,7 @@
});
});
});
//--></script>
</script>

<style>
.test_instructions {
Expand Down

0 comments on commit c3b9e23

Please sign in to comment.