Skip to content

Commit

Permalink
IPN Updates
Browse files Browse the repository at this point in the history
thejoshualewis committed Sep 8, 2021
1 parent a5f8705 commit 2515c01
Showing 4 changed files with 25 additions and 108 deletions.
25 changes: 7 additions & 18 deletions modules/gateways/bitpaycheckout.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* BitPay Checkout 4.0.1
* BitPay Checkout 4.0.2
*
* Within the module itself, all functions must be prefixed with the module
* filename, followed by an underscore, and then the function name. For this
@@ -50,15 +50,6 @@ function ($table) {
* @return array
*/

function bitpaycheckout_MetaData()
{
return array(
'DisplayName' => 'BitPay_Checkout_WHMCS',
'APIVersion' => '4.0.1', // Use API Version 1.1
'DisableLocalCreditCardInput' => false,
'TokenisedStorage' => false,
);
}

/**
* Define gateway configuration options.
@@ -83,6 +74,7 @@ function bitpaycheckout_MetaData()
* @return array
*/

if (!function_exists('bitpaycheckout_config')) {
function bitpaycheckout_config()
{
return array(
@@ -125,6 +117,7 @@ function bitpaycheckout_config()

);
}
}

function BPC_autoloader($class)
{
@@ -220,20 +213,16 @@ function bitpaycheckout_link($config_params)
$protocol = 'https://';

$callback_url = $protocol . $_SERVER['SERVER_NAME'] . $dir . '/modules/gateways/bitpaycheckout/bitpaycheckout_callback.php';
$params->extension_version = bitpaycheckout_MetaData();
$params->extension_version = $params->extension_version['DisplayName'] . '_' . $params->extension_version['APIVersion'];
$params->extension_version = "BitPay_Checkout_WHMCS_4.0.2";
$params->price = $amount;
$params->currency = $currencyCode;
$params->orderId = trim($invoiceId);

$params->notificationURL = $protocol . $_SERVER['SERVER_NAME'] . $dir . '/modules/gateways/bitpaycheckout/bitpaycheckout_ipn.php';
$params->redirectURL = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$params->extendedNotifications = true;

#set the transaction speed in the plugin and override the plugin
$params->fullNotifications = true;
#$params->extendedNotifications = true;

$params->acceptanceWindow = 1200000;
if (!empty($email)):
$buyerInfo = new stdClass();
$buyerInfo->name = $firstname . ' ' . $lastname;
@@ -322,7 +311,7 @@ function showModal(invoiceData) {
data: $encodedData,
dataType: "text",
success: function(resultData) {
location.reload();
window.location.reload();
},
error: function(resultData) {
//console.log('error', resultData)
26 changes: 7 additions & 19 deletions modules/gateways/bitpaycheckout/bitpaycheckout.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* BitPay Checkout 4.0.1
* BitPay Checkout 4.0.2
*
* Within the module itself, all functions must be prefixed with the module
* filename, followed by an underscore, and then the function name. For this
@@ -50,15 +50,6 @@ function ($table) {
* @return array
*/

function bitpaycheckout_MetaData()
{
return array(
'DisplayName' => 'BitPay_Checkout_WHMCS',
'APIVersion' => '4.0.1', // Use API Version 1.1
'DisableLocalCreditCardInput' => false,
'TokenisedStorage' => false,
);
}

/**
* Define gateway configuration options.
@@ -82,7 +73,7 @@ function bitpaycheckout_MetaData()
*
* @return array
*/

if (!function_exists('bitpaycheckout_config')) {
function bitpaycheckout_config()
{
return array(
@@ -125,6 +116,7 @@ function bitpaycheckout_config()

);
}
}

function BPC_autoloader($class)
{
@@ -220,20 +212,16 @@ function bitpaycheckout_link($config_params)
$protocol = 'https://';

$callback_url = $protocol . $_SERVER['SERVER_NAME'] . $dir . '/modules/gateways/bitpaycheckout/bitpaycheckout_callback.php';
$params->extension_version = bitpaycheckout_MetaData();
$params->extension_version = $params->extension_version['DisplayName'] . '_' . $params->extension_version['APIVersion'];
$params->extension_version = "BitPay_Checkout_WHMCS_4.0.2";
$params->price = $amount;
$params->currency = $currencyCode;
$params->orderId = trim($invoiceId);

$params->notificationURL = $protocol . $_SERVER['SERVER_NAME'] . $dir . '/modules/gateways/bitpaycheckout/bitpaycheckout_ipn.php';
$params->redirectURL = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$params->extendedNotifications = true;

#set the transaction speed in the plugin and override the plugin
$params->fullNotifications = true;
#$params->extendedNotifications = true;

$params->acceptanceWindow = 1200000;
if (!empty($email)):
$buyerInfo = new stdClass();
$buyerInfo->name = $firstname . ' ' . $lastname;
@@ -322,7 +310,7 @@ function showModal(invoiceData) {
data: $encodedData,
dataType: "text",
success: function(resultData) {
location.reload();
window.location.reload();
},
error: function(resultData) {
//console.log('error', resultData)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* BitPay Checkout Callback 4.0.1
* BitPay Checkout Callback 4.0.2
*
* This file demonstrates how a payment gateway callback should be
* handled within WHMCS.
80 changes: 10 additions & 70 deletions modules/gateways/bitpaycheckout/bitpaycheckout_ipn.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* BitPay Checkout IPN 4.0.1
* BitPay Checkout IPN 4.0.2
*
* This file demonstrates how a payment gateway callback should be
* handled within WHMCS.
@@ -36,18 +36,17 @@ function checkInvoiceStatus($url){
return $result;
}

$all_data = json_decode(file_get_contents("php://input"), true);
$event = json_decode(file_get_contents("php://input"), true);
$file = 'bitpay.txt';
$err = "bitpay_err.txt";

file_put_contents($file,"===========INCOMING IPN=========================",FILE_APPEND);
file_put_contents($file,date('d.m.Y H:i:s'),FILE_APPEND);
file_put_contents($file,print_r($all_data, true),FILE_APPEND);
file_put_contents($file,print_r($event, true),FILE_APPEND);
file_put_contents($file,"===========END OF IPN===========================",FILE_APPEND);

$data = $all_data['data'];
$order_status = $data['status'];
$order_invoice = $data['id'];
$order_status = $event['status'];
$order_invoice = $event['id'];
$endpoint = $gatewayParams['bitpay_checkout_endpoint'];
if($endpoint == "Test"):
$url_check = 'https://test.bitpay.com/invoices/'.$order_invoice;
@@ -56,10 +55,8 @@ function checkInvoiceStatus($url){
endif;
$invoiceStatus = json_decode(checkInvoiceStatus($url_check));

$event = $all_data['event'];
$orderid = $invoiceStatus->data->orderId;
$price = $invoiceStatus->data->price;

#first see if the ipn matches
#get the user id first
$table = "_bitpay_checkout_transactions";
@@ -71,10 +68,9 @@ function checkInvoiceStatus($url){
$btn_id = $rowdata['transaction_id'];

if($btn_id):
switch ($event['name']) {
switch ($event['status']) {
#complete, update invoice table to Paid
case 'invoice_confirmed':

case 'complete ':

$table = "tblinvoices";
$update = array("status" => 'Paid','datepaid' => date("Y-m-d H:i:s"));
@@ -88,7 +84,7 @@ function checkInvoiceStatus($url){

#update the bitpay_invoice table
$table = "_bitpay_checkout_transactions";
$update = array("transaction_status" => $event['name']);
$update = array("transaction_status" => "complete");
$where = array("order_id" => $orderid, "transaction_id" => $order_invoice);
try{
update_query($table, $update, $where);
@@ -106,7 +102,7 @@ function checkInvoiceStatus($url){
break;

#processing - put in Payment Pending
case 'invoice_paidInFull':
case 'paid':
$table = "tblinvoices";
$update = array("status" => 'Payment Pending','datepaid' => date("Y-m-d H:i:s"));
$where = array("id" => $orderid, "paymentmethod" => "bitpaycheckout");
@@ -127,33 +123,8 @@ function checkInvoiceStatus($url){
}
break;

#confirmation error - put in Unpaid
case 'invoice_failedToConfirm':
case 'invoice_declined':

$table = "tblinvoices";
$update = array("status" => 'Unpaid');
$where = array("id" => $orderid, "paymentmethod" => "bitpaycheckout");
try{
update_query($table, $update, $where);
}catch (Exception $e ){
file_put_contents($file,$e,FILE_APPEND);
}

#update the bitpay_invoice table
$table = "_bitpay_checkout_transactions";
$update = array("transaction_status" => $event['name']);
$where = array("order_id" => $orderid, "transaction_id" => $order_invoice);
try{
update_query($table, $update, $where);
}catch (Exception $e ){
file_put_contents($file,$e,FILE_APPEND);
}

break;

#expired, remove from transaction table, wont be in invoice table
case 'invoice_expired':
case 'expired':
#delete any orphans
$table = "_bitpay_checkout_transactions";
$delete = 'DELETE FROM _bitpay_checkout_transactions WHERE transaction_id = "' . $order_invoice.'"';
@@ -163,37 +134,6 @@ function checkInvoiceStatus($url){
file_put_contents($file,$e,FILE_APPEND);
}
break;

#update both table to refunded
case 'invoice_refundComplete':

#get the user id first
$table = "tblaccounts";
$fields = "id,userid";
$where = array("transid" => $order_invoice);
$result = select_query($table, $fields, $where);
$rowdata = mysql_fetch_array($result);
$id = $rowdata['id'];
$userid = $rowdata['userid'];


#do an insert on tblaccounts
$values = array("userid" => $userid, "description" => "BitPay Refund of Transaction ID: ".$order_invoice, "amountin" => "0","currency"=>"0","amountout" => $price,"invoiceid" =>$orderid,"date"=>date("Y-m-d H:i:s"));
$newid = insert_query($table, $values);

#update the tblinvoices to show Refunded
$table = "tblinvoices";
$update = array("status" => 'Refunded','datepaid' => date("Y-m-d H:i:s"));
$where = array("id" => $orderid, "paymentmethod" => "bitpaycheckout");
update_query($table, $update, $where);

#update the bitpay_invoice table
$table = "_bitpay_checkout_transactions";
$update = array("transaction_status" => $event['name']);
$where = array("order_id" => $orderid, "transaction_id" => $order_invoice);
update_query($table, $update, $where);

break;
}
http_response_code(200);
endif;#end of the table lookup

0 comments on commit 2515c01

Please sign in to comment.