/**
* used to issue a refund for a captured payment
*
* refund can be full or partial
*/
if(isset($this->request->post['transaction_id']) && isset($this->request->post['refund_full'])) {
$this->load->model('payment/pp_express');
$this->load->language('payment/pp_express_refund');
if($this->request->post['refund_full'] == 0 && $this->request->post['amount'] == 0) {
$this->session->data['error'] = $this->language->get('error_partial_amt');
} else {
$order_id = $this->model_payment_pp_express->getOrderId($this->request->post['transaction_id']);
$paypal_order = $this->model_payment_pp_express->getOrder($order_id);
if ($paypal_order) {
$call_data = array();
$call_data['METHOD'] = 'RefundTransaction';
$call_data['TRANSACTIONID'] = $this->request->post['transaction_id'];
$call_data['NOTE'] = urlencode($this->request->post['refund_message']);
$call_data['MSGSUBID'] = uniqid(mt_rand(), true);
$current_transaction = $this->model_payment_pp_express->getLocalTransaction($this->request->post['transaction_id']);
if ($this->request->post['refund_full'] == 1) {
$call_data['REFUNDTYPE'] = 'Full';
} else {
$call_data['REFUNDTYPE'] = 'Partial';
$call_data['AMT'] = number_format($this->request->post['amount'], 2);
$call_data['CURRENCYCODE'] = $this->request->post['currency_code'];
}
$result = $this->model_payment_pp_express->call($call_data);
$transaction = array(
'paypal_order_id' => $paypal_order['paypal_order_id'],
'transaction_id' => '',
'parent_transaction_id' => $this->request->post['transaction_id'],
'note' => $this->request->post['refund_message'],
'msgsubid' => $call_data['MSGSUBID'],
'receipt_id' => '',
'payment_type' => '',
'payment_status' => 'Refunded',
'transaction_entity' => 'payment',
'pending_reason' => '',
'amount' => '-' . (isset($call_data['AMT']) ? $call_data['AMT'] : $current_transaction['amount']),
'debug_data' => json_encode($result),
);
if ($result == false) {
$transaction['payment_status'] = 'Failed';
$this->model_payment_pp_express->addTransaction($transaction, $call_data);
$this->redirect($this->url->link('sale/order/info', 'token=' . $this->session->data['token'] . '&order_id=' . $paypal_order['order_id'], 'SSL'));
} else if ($result['ACK'] != 'Failure' && $result['ACK'] != 'FailureWithWarning') {
$transaction['transaction_id'] = $result['REFUNDTRANSACTIONID'];
$transaction['payment_type'] = $result['REFUNDSTATUS'];
$transaction['pending_reason'] = $result['PENDINGREASON'];
$transaction['amount'] = '-' . $result['GROSSREFUNDAMT'];
$this->model_payment_pp_express->addTransaction($transaction);
//update transaction to refunded status
if ($result['TOTALREFUNDEDAMOUNT'] == $this->request->post['amount_original']) {
$this->db->query("UPDATE `" . DB_PREFIX . "paypal_order_transaction` SET `payment_status` = 'Refunded' WHERE `transaction_id` = '" . $this->db->escape($this->request->post['transaction_id']) . "' LIMIT 1");
} else {
$this->db->query("UPDATE `" . DB_PREFIX . "paypal_order_transaction` SET `payment_status` = 'Partially-Refunded' WHERE `transaction_id` = '" . $this->db->escape($this->request->post['transaction_id']) . "' LIMIT 1");
}
//redirect back to the order
$this->redirect($this->url->link('sale/order/info', 'token=' . $this->session->data['token'] . '&order_id=' . $paypal_order['order_id'], 'SSL'));
} else {
$this->model_payment_pp_express->log(json_encode($result));
$this->session->data['error'] = (isset($result['L_SHORTMESSAGE0']) ? $result['L_SHORTMESSAGE0'] : 'There was an error') . (isset($result['L_LONGMESSAGE0']) ? '<br />' . $result['L_LONGMESSAGE0'] : '');
$this->redirect($this->url->link('payment/pp_express/refund', 'token=' . $this->session->data['token'] . '&transaction_id=' . $this->request->post['transaction_id'], 'SSL'));
}
} else {
$this->session->data['error'] = $this->language->get('error_data_missing');
$this->redirect($this->url->link('payment/pp_express/refund', 'token=' . $this->session->data['token'] . '&transaction_id=' . $this->request->post['transaction_id'], 'SSL'));
}
}
} else {
$this->session->data['error'] = $this->language->get('error_data');
$this->redirect($this->url->link('payment/pp_express/refund', 'token=' . $this->session->data['token'] . '&transaction_id=' . $this->request->post['transaction_id'], 'SSL'));
}