Skip to content

Commit

Permalink
Merge pull request #6 from xendit/TPI-1044/cancel-fix
Browse files Browse the repository at this point in the history
Use callback for order cancellation and add new header
  • Loading branch information
hakiemaul authored Jan 14, 2020
2 parents 453bc88 + d330910 commit cc27a08
Show file tree
Hide file tree
Showing 26 changed files with 138 additions and 43 deletions.
7 changes: 0 additions & 7 deletions opencart1.5.x/upload/admin/controller/payment/xendit.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,12 @@ public function install()
{
$this->load->model('payment/xendit');
$this->model_payment_xendit->install();

//$this->load->model('setting/event');
//$this->model_setting_event->addEvent('xendit', 'admin/view/common/column_left/before', 'payment/xendit/cancelExpiredOrder');
//$this->model_setting_event->addEvent('xendit', 'admin/view/common/header', 'payment/xendit/cancelExpiredOrder');
}

public function uninstall()
{
$this->load->model('payment/xendit');
$this->model_payment_xendit->uninstall();

//$this->load->model('setting/event');
//$this->model_setting_event->deleteEventByCode('xendit');
}

public function validate()
Expand Down
6 changes: 6 additions & 0 deletions opencart1.5.x/upload/admin/controller/payment/xenditbniva.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public function index() {
$this->data['xendit_status'] = false;
}

if (isset($this->request->post['xendit' . self::XENDIT_CODE . '_sort_order'])) {
$this->data['xendit_sort_order'] = $this->request->post['xendit' . self::XENDIT_CODE . '_sort_order'];
} else {
$this->data['xendit_sort_order'] = $this->config->get('xendit' . self::XENDIT_CODE . '_sort_order');
}

$this->data['xendit_code'] = self::XENDIT_CODE;

$this->data['token'] = $this->session->data['token'];
Expand Down
6 changes: 6 additions & 0 deletions opencart1.5.x/upload/admin/controller/payment/xenditbriva.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public function index() {
$this->data['xendit_status'] = false;
}

if (isset($this->request->post['xendit' . self::XENDIT_CODE . '_sort_order'])) {
$this->data['xendit_sort_order'] = $this->request->post['xendit' . self::XENDIT_CODE . '_sort_order'];
} else {
$this->data['xendit_sort_order'] = $this->config->get('xendit' . self::XENDIT_CODE . '_sort_order');
}

$this->data['xendit_code'] = self::XENDIT_CODE;

$this->data['token'] = $this->session->data['token'];
Expand Down
6 changes: 6 additions & 0 deletions opencart1.5.x/upload/admin/controller/payment/xenditcc.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public function index() {
$this->data['xendit_debug'] = 'failover';
}

if (isset($this->request->post['xenditcc_sort_order'])) {
$this->data['xenditcc_sort_order'] = $this->request->post['xenditcc_sort_order'];
} else {
$this->data['xenditcc_sort_order'] = $this->config->get('xenditcc_sort_order');
}

$this->data['token'] = $this->session->data['token'];

//Bootstrap 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public function index() {
$this->data['xendit_status'] = false;
}

if (isset($this->request->post['xendit' . self::XENDIT_CODE . '_sort_order'])) {
$this->data['xendit_sort_order'] = $this->request->post['xendit' . self::XENDIT_CODE . '_sort_order'];
} else {
$this->data['xendit_sort_order'] = $this->config->get('xendit' . self::XENDIT_CODE . '_sort_order');
}

$this->data['xendit_code'] = self::XENDIT_CODE;

$this->data['token'] = $this->session->data['token'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public function index() {
$this->data['xendit_status'] = false;
}

if (isset($this->request->post['xendit' . self::XENDIT_CODE . '_sort_order'])) {
$this->data['xendit_sort_order'] = $this->request->post['xendit' . self::XENDIT_CODE . '_sort_order'];
} else {
$this->data['xendit_sort_order'] = $this->config->get('xendit' . self::XENDIT_CODE . '_sort_order');
}

$this->data['xendit_code'] = self::XENDIT_CODE;

$this->data['token'] = $this->session->data['token'];
Expand Down
6 changes: 6 additions & 0 deletions opencart1.5.x/upload/admin/view/template/payment/xenditcc.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"> Sort Order </label>
<div class="col-sm-10">
<input type="number" class="form-control" name="xenditcc_sort_order" value="<?php echo $xenditcc_sort_order; ?>" />
</div>
</div>
</form>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"> Sort Order </label>
<div class="col-sm-10">
<input type="number" class="form-control" name="xendit<?php echo $xendit_code; ?>_sort_order" value="<?php echo $xendit_sort_order; ?>" />
</div>
</div>
</form>
</div>
</div>
Expand Down
20 changes: 19 additions & 1 deletion opencart1.5.x/upload/catalog/controller/payment/xendit.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ public function process_notification() {
$invoice_id = $response['id'];
$external_id = $response['external_id'];
$order_id = str_replace(self::EXT_ID_PREFIX, "", $external_id);
$order_info = $this->model_checkout_order->getOrder($order_id);

if (empty($order_info)) {
$message = 'Order not found. Order id: ' . $order_id . '.';
$this->response->addHeader('HTTP/1.1 404 Not Found');
$this->response->setOutput($message);
return;
}

$order_status_id = $order_info['order_status_id'];

$api_key = $this->get_api_key();
Xendit::set_secret_key($api_key['secret_key']);
Expand All @@ -92,6 +102,14 @@ public function process_notification() {
'store_name' => $store_name
);

// if status is not pending
if ($order_status_id != 1) {
$message = 'Order status is not pending. Order id: ' . $order_id . '.';
$this->response->addHeader('HTTP/1.1 422 Unprocessable Entity');
$this->response->setOutput($message);
return;
}

try {
$response = Xendit::request($request_url, Xendit::METHOD_GET, array(), $request_options);

Expand Down Expand Up @@ -154,7 +172,7 @@ private function cancel_order($order_id, $message) {
false
);

$message = 'Successfully completed order ' . $order_id;
$message = 'Successfully cancelled order ' . $order_id;
$this->response->setOutput($message);
}
}
2 changes: 1 addition & 1 deletion opencart1.5.x/upload/catalog/model/payment/xenditbniva.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getMethod($address, $total) {
'code' => 'xenditbniva',
'title' => 'Bank Transfer BNI',
'terms' => '',
'sort_order' => $this->config->get('payment_' . $code . '_sort_order')
'sort_order' => $this->config->get($code . '_sort_order')
);
}

Expand Down
2 changes: 1 addition & 1 deletion opencart1.5.x/upload/catalog/model/payment/xenditbriva.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getMethod($address, $total) {
'code' => $code,
'title' => 'Bank Transfer BRI',
'terms' => '',
'sort_order' => $this->config->get('payment_' . $code . '_sort_order')
'sort_order' => $this->config->get($code . '_sort_order')
);
}

Expand Down
2 changes: 1 addition & 1 deletion opencart1.5.x/upload/catalog/model/payment/xenditcc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getMethod($address, $total) {
'code' => 'xenditcc',
'title' => $this->language->get('text_title'),
'terms' => '',
'sort_order' => $this->config->get('payment_' . $code . '_sort_order')
'sort_order' => $this->config->get($code . '_sort_order')
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getMethod($address, $total) {
'code' => $code,
'title' => 'Bank Transfer Mandiri',
'terms' => '',
'sort_order' => $this->config->get('payment_' . $code . '_sort_order')
'sort_order' => $this->config->get($code . '_sort_order')
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getMethod($address, $total) {
'code' => $code,
'title' => 'Bank Transfer Permata',
'terms' => '',
'sort_order' => $this->config->get('payment_' . $code . '_sort_order')
'sort_order' => $this->config->get($code . '_sort_order')
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
type="button"
value="<?php echo $button_confirm; ?>"
id="button-confirm"
data-loading-text="<?php echo $text_loading; ?>"
class="button"
/>
</div>
Expand All @@ -28,13 +27,14 @@
url: 'index.php?route=payment/xendit/process_payment',
dataType: 'json',
beforeSend: function() {
$('#button-confirm').button('loading');
$('#button-confirm').attr('disabled', true);
},
complete: function() {
$('#button-confirm').button('reset');
$('#button-confirm').attr('disabled', false);
},
success: function(json) {
if (json['error']) {
$('#button-confirm').attr('disabled', false);
alert('Error: ' + json['error']);
}
Expand All @@ -43,6 +43,7 @@
}
},
error: function(xhr, ajaxOptions, thrownError) {
$('#button-confirm').attr('disabled', false);
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
type="button"
value="<?php echo $button_confirm; ?>"
id="button-confirm"
data-loading-text="<?php echo $text_loading; ?>"
class="button"
/>
</div>
Expand All @@ -75,8 +74,6 @@
<script type="text/javascript">
var buttonConfirm = $('#button-confirm');
buttonConfirm.on('click', function() {
buttonConfirm.button('loading');
Xendit.setPublishableKey($('#xendit-public-key').val());
var expDate = $('#card-expiry-date').val().split('/');
Expand Down Expand Up @@ -104,9 +101,11 @@
return;
}
buttonConfirm.attr('disabled', true);
Xendit.card.createToken(data, function (err, response) {
if (err) {
buttonConfirm.button('reset');
buttonConfirm.attr('disabled', false);
alert('Tokenization error. Error code:' + err.error_code);
return;
Expand All @@ -122,13 +121,14 @@
token_id: token
},
beforeSend: function() {
$('#button-confirm').button('loading');
buttonConfirm.attr('disabled', true);
},
complete: function() {
$('#button-confirm').button('reset');
buttonConfirm.attr('disabled', false);
},
success: function(json) {
if (json['error']) {
buttonConfirm.attr('disabled', false);
alert('Error: ' + json['error']);
}
Expand All @@ -137,6 +137,7 @@
}
},
error: function(xhr, ajaxOptions, thrownError) {
buttonConfirm.attr('disabled', false);
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
Expand Down
1 change: 1 addition & 0 deletions opencart1.5.x/upload/system/library/xendit.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static function get_headers($options)
{
$headers = array();
$headers[] = 'x-plugin-name: ' . self::PLUGIN_NAME;
$headers[] = 'x-plugin-version: ' . VERSION;
$headers[] = 'x-plugin-store-name: ' . isset($options['store_name']) ? $options['store_name'] : self::DEFAULT_STORE_NAME;
$headers[] = 'Content-Type: application/json';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,12 @@ public function install()
{
$this->load->model('payment/xendit');
$this->model_payment_xendit->install();

$this->load->model('extension/event');
$this->model_extension_event->addEvent('xendit', 'admin/view/common/column_left/before', 'payment/xendit/cancelExpiredOrder');
}

public function uninstall()
{
$this->load->model('payment/xendit');
$this->model_payment_xendit->uninstall();

$this->load->model('extension/event');
$this->model_extension_event->deleteEvent('xendit');
}

public function validate()
Expand Down
20 changes: 19 additions & 1 deletion opencart2.0.x-2.2.x/upload/catalog/controller/payment/xendit.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public function process_notification()
$invoice_id = $response['id'];
$external_id = $response['external_id'];
$order_id = str_replace(self::EXT_ID_PREFIX, "", $external_id);
$order_info = $this->model_checkout_order->getOrder($order_id);

if (empty($order_info)) {
$message = 'Order not found. Order id: ' . $order_id . '.';
$this->response->addHeader('HTTP/1.1 404 Not Found');
$this->response->setOutput($message);
return;
}

$order_status_id = $order_info['order_status_id'];

$api_key = $this->get_api_key();
Xendit::set_secret_key($api_key['secret_key']);
Expand All @@ -100,6 +110,14 @@ public function process_notification()
'store_name' => $store_name
);

// if status is not pending
if ($order_status_id != 1) {
$message = 'Order status is not pending. Order id: ' . $order_id . '.';
$this->response->addHeader('HTTP/1.1 422 Unprocessable Entity');
$this->response->setOutput($message);
return;
}

try {
$response = Xendit::request($request_url, Xendit::METHOD_GET, array(), $request_options);

Expand Down Expand Up @@ -163,7 +181,7 @@ private function cancel_order($order_id, $message)
false
);

$message = 'Successfully completed order ' . $order_id;
$message = 'Successfully cancelled order ' . $order_id;
$this->response->setOutput($message);
}
}
1 change: 1 addition & 0 deletions opencart2.0.x-2.2.x/upload/system/library/xendit.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static function get_headers($options)
{
$headers = array();
$headers[] = 'x-plugin-name: ' . self::PLUGIN_NAME;
$headers[] = 'x-plugin-version: ' . VERSION;
$headers[] = 'x-plugin-store-name: ' . isset($options['store_name']) ? $options['store_name'] : self::DEFAULT_STORE_NAME;
$headers[] = 'Content-Type: application/json';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,12 @@ public function install()
{
$this->load->model('extension/payment/xendit');
$this->model_extension_payment_xendit->install();

$this->load->model('extension/event');
$this->model_extension_event->addEvent('xendit', 'admin/view/common/column_left/before', 'extension/payment/xendit/cancelExpiredOrder');
}

public function uninstall()
{
$this->load->model('extension/payment/xendit');
$this->model_extension_payment_xendit->uninstall();

$this->load->model('extension/event');
$this->model_extension_event->deleteEventByCode('xendit');
}

public function validate()
Expand Down
Loading

0 comments on commit cc27a08

Please sign in to comment.