$this->load->language('payment/pp_express');
/**
* used to capture authorised payments
*
* capture can be full or partial amounts
*/
if(isset($this->request->post['order_id']) && $this->request->post['amount'] > 0 && isset($this->request->post['order_id']) && isset($this->request->post['complete'])) {
$this->load->model('payment/pp_express');
$paypal_order = $this->model_payment_pp_express->getOrder($this->request->post['order_id']);
if($this->request->post['complete'] == 1) {
$complete = 'Complete';
} else {
$complete = 'NotComplete';
}
$call_data = array();
$call_data['METHOD'] = 'DoCapture';
$call_data['AUTHORIZATIONID'] = $paypal_order['authorization_id'];
$call_data['AMT'] = number_format($this->request->post['amount'], 2);
$call_data['CURRENCYCODE'] = $paypal_order['currency_code'];
$call_data['COMPLETETYPE'] = $complete;
$call_data['MSGSUBID'] = uniqid(mt_rand(), true);
$result = $this->model_payment_pp_express->call($call_data);
$transaction = array(
'paypal_order_id' => $paypal_order['paypal_order_id'],
'transaction_id' => '',
'parent_transaction_id' => $paypal_order['authorization_id'],
'note' => '',
'msgsubid' => $call_data['MSGSUBID'],
'receipt_id' => '',
'payment_type' => '',
'payment_status' => '',
'pending_reason' => '',
'transaction_entity' => 'payment',
'amount' => '',
'debug_data' => json_encode($result),
);
if ($result == false) {
$transaction['amount'] = number_format($this->request->post['amount'], 2);
$paypal_order_transaction_id = $this->model_payment_pp_express->addTransaction($transaction, $call_data);
$json['error'] = true;
$json['failed_transaction']['paypal_order_transaction_id'] = $paypal_order_transaction_id;
$json['failed_transaction']['amount'] = $transaction['amount'];
$json['failed_transaction']['created'] = date("Y-m-d H:i:s");
$json['msg'] = $this->language->get('error_timeout');
} else if(isset($result['ACK']) && $result['ACK'] != 'Failure' && $result['ACK'] != 'FailureWithWarning') {
$transaction['transaction_id'] = $result['TRANSACTIONID'];
$transaction['payment_type'] = $result['PAYMENTTYPE'];
$transaction['payment_status'] = $result['PAYMENTSTATUS'];
$transaction['pending_reason'] = (isset($result['PENDINGREASON']) ? $result['PENDINGREASON'] : '');
$transaction['amount'] = $result['AMT'];
$this->model_payment_pp_express->addTransaction($transaction);
unset($transaction['debug_data']);
$transaction['created'] = date("Y-m-d H:i:s");
$captured = number_format($this->model_payment_pp_express->totalCaptured($paypal_order['paypal_order_id']), 2);
$refunded = number_format($this->model_payment_pp_express->totalRefundedOrder($paypal_order['paypal_order_id']), 2);
$transaction['captured'] = $captured;
$transaction['refunded'] = $refunded;
$transaction['remaining'] = number_format($paypal_order['total'] - $captured, 2);
$transaction['status'] = 0;
if($transaction['remaining'] == 0.00) {
$transaction['status'] = 1;
$this->model_payment_pp_express->updateOrder('Complete', $this->request->post['order_id']);
}
$transaction['void'] = '';
if($this->request->post['complete'] == 1 && $transaction['remaining'] > 0) {
$transaction['void'] = array(
'paypal_order_id' => $paypal_order['paypal_order_id'],
'transaction_id' => '',
'parent_transaction_id' => $paypal_order['authorization_id'],
'note' => '',
'msgsubid' => '',
'receipt_id' => '',
'payment_type' => '',
'payment_status' => 'Void',
'pending_reason' => '',
'amount' => '',
'debug_data' => 'Voided after capture',
'transaction_entity' => 'auth',
);
$this->model_payment_pp_express->addTransaction($transaction['void']);
$this->model_payment_pp_express->updateOrder('Complete', $this->request->post['order_id']);
$transaction['void']['created'] = date("Y-m-d H:i:s");
$transaction['status'] = 1;
}
$json['data'] = $transaction;
$json['error'] = false;
$json['msg'] = 'Ok';
} else {
$json['error'] = true;
$json['msg'] = (isset($result['L_SHORTMESSAGE0']) ? $result['L_SHORTMESSAGE0'] : 'There was an error');
}
} else {
$json['error'] = true;
$json['msg'] = 'Missing data';
}
$this->response->setOutput(json_encode($json));