Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
185 lines (163 sloc) 7.53 KB
<?php
require('includes/application_top.php');
if (!defined('MODULE_PAYMENT_GOCOIN_STATUS') || (MODULE_PAYMENT_GOCOIN_STATUS != 'True')) {
exit;
}
function callback() {
global $db;
_paymentStandard();
}
function postData() {
//get webhook content
$response = new stdClass();
$post_data = file_get_contents("php://input");
if (!$post_data) {
$response->error = 'Request body is empty';
}
$post_as_json = json_decode($post_data);
if (is_null($post_as_json)){
$response->error = 'Request body was not valid json';
} else {
$response = $post_as_json;
}
return $response;
}
function _paymentStandard() {
global $db;
$sts_default = MODULE_PAYMENT_GOCOIN_DEFAULT_ORDER_STATUS_ID; // Default
$sts_processing = MODULE_PAYMENT_GOCOIN_ORDER_STATUS_ID; // Processing
$merchant_id = MODULE_PAYMENT_GOCOIN_MERCHANT_ID;
$access_token = MODULE_PAYMENT_GOCOIN_ACCESS_KEY;
if(isset($access_token) && empty($access_token)){
$error_msg[] = 'API Key is Blank';
zcLog('GoCoin Callback', 'API Key is Blank');
}
$module_display = 'gocoin';
$data = postData();
if (isset($data->error)){
zcLog('GoCoin Callback',$data->error);
}
else {
$key = $access_token;
$event_id = $data -> id;
$event = $data -> event;
$invoice = $data -> payload;
$payload_arr = get_object_vars($invoice) ;
ksort($payload_arr);
$signature = $invoice -> user_defined_8;
$sig_comp = sign($payload_arr, $key);
$status = $invoice -> status;
$order_id = (int) $invoice -> order_id;
$order_query = $db->Execute("select orders_status, currency, currency_value from " . TABLE_ORDERS . " where orders_id = '" .(int) $order_id . "'");
if (isset($signature) && ($signature != $sig_comp)) {
$msg = "Signature : " . $signature . "does not match for Order: " . $order_id ."$sig_comp | $signature ";
$msg .= ' Event ID: '. $event_id;
zcLog('GoCoin Callback', $msg);
}
elseif (empty($signature) || empty($sig_comp) ) {
$msg = "Signature is blank for Order: " . $order_id;
$msg .= ' Event ID: '. $event_id;
zcLog('GoCoin Callback', $msg);
}
elseif ($order_query->RecordCount() > 0) {
switch($event) {
case 'invoice_created':
break;
case 'invoice_payment_received':
switch ($status) {
case 'ready_to_ship':
$msg = 'Order ' . $order_id .' is paid and awaiting payment confirmation on blockchain.';
$msg .=" Price (Currency) : ". $invoice->price."(". $invoice->price_currency.")";
$msg .= ' Event ID: '. $event_id;
updateOrderstatus($order_id,$sts_default,$msg);
break;
case 'paid':
$msg = 'Order ' . $order_id .' is paid and awaiting payment confirmation on blockchain.';
$msg .=" Price (Currency) : ". $invoice->price."(". $invoice->price_currency.")";
$msg .= ' Event ID: '. $event_id;
updateOrderstatus($order_id,$sts_default,$msg);
break;
case 'underpaid':
$msg = 'Order ' . $order_id .' is underpaid.';
$msg .=" Price (Currency) : ". $invoice->price."(". $invoice->price_currency.")";
$msg .= ' Event ID: '. $event_id;
updateOrderstatus($order_id,$sts_default,$msg);
break;
}
break;
case 'invoice_merchant_review':
$msg = 'Order ' . $order_id .' is under review. Action must be taken from the GoCoin Dashboard.';
$msg .=" Price (Currency) : ". $invoice->price."(". $invoice->price_currency.")";
$msg .= ' Event ID: '. $event_id;
updateOrderstatus($order_id,$sts_default,$msg);
break;
case 'invoice_ready_to_ship':
$msg = 'Order ' . $order_id .' has been paid in full and confirmed on the blockchain.';
$msg .=" Price (Currency) : ". $invoice->price."(". $invoice->price_currency.")";
$msg .= ' Event ID: '. $event_id;
updateOrderstatus($order_id,$sts_processing,$msg);
break;
case 'invoice_invalid':
$msg = 'Order ' . $order_id . ' is invalid and will not be confirmed on the blockchain.';
$msg .=" Price (Currency) : ". $invoice->price."(". $invoice->price_currency.")";
$msg .= ' Event ID: '. $event_id;
updateOrderstatus($order_id,$sts_default,$msg);
break;
default:
$msg = "Unrecognized event type: ". $event;
}
}
}
}
function updateOrderstatus($order_id,$sts,$comment){
zcLog('GoCoin Callback', $comment);
global $db;
$db->Execute("update " . TABLE_ORDERS . " set orders_status = '" . $db->prepare_input($sts) . "', last_modified = now() where orders_id = '" . (int) $order_id . "'");
$db->Execute("insert into " . TABLE_ORDERS_STATUS_HISTORY . "(orders_id,orders_status_id,date_added,customer_notified,comments)values('" . $db->prepare_input($order_id) . "','" . $db->prepare_input($sts) . "',now(),'1','" . $db->prepare_input($comment) . "' )");
}
function updateTransaction($type = 'payment', $details) {
global $db;
return $db->Execute("
update gocoin_ipn set
status = '" . $db->prepare_input($details['status']) . "',
updated_time = '" . $db->prepare_input($details['updated_time']) . "' where
invoice_id = '" . $db->prepare_input($details['invoice_id']) . "' and
order_id = '" . $db->prepare_input($details['order_id']) . "'
");
}
function zcLog($stage, $message) {
$_logDir = defined('DIR_FS_LOGS') ? DIR_FS_LOGS : DIR_FS_SQL_CACHE;
$file = $_logDir . '/' . 'GoCoin.log';
$fp = @fopen($file, 'a');
@fwrite($fp, date('M-d-Y H:i:s') . ' (' . time() . ')' . "\n" . $stage . "\n" . $message . "\n=================================\n\n");
@fclose($fp);
}
function sign($data, $key) {
// $include = array('price_currency','base_price','base_price_currency','order_id','customer_name');
$include = array('base_price', 'base_price_currency', 'order_id', 'customer_name');
// $data must be an array
if (is_array($data)) {
$querystring = "";
while (count($include) > 0) {
$k = $include[0];
if (isset($data[$k])) {
$querystring .= $k . "=" . $data[$k] . "&";
array_shift($include);
} else {
return false;
}
}
//Strip trailing '&' and lowercase
$msg = substr($querystring, 0, strlen($querystring) - 1);
$msg = strtolower($msg);
// hash with key
$hash = hash_hmac("sha256", $msg, $key, true);
$encoded = base64_encode($hash);
return $encoded;
} else {
return false;
}
}
callback();
require('includes/application_bottom.php');
?>