11<?php
22/**
3- * BitPay Checkout IPN 4.0.1
3+ * BitPay Checkout IPN 4.0.2
44 *
55 * This file demonstrates how a payment gateway callback should be
66 * handled within WHMCS.
@@ -36,18 +36,17 @@ function checkInvoiceStatus($url){
3636 return $ result ;
3737}
3838
39- $ all_data = json_decode (file_get_contents ("php://input " ), true );
39+ $ event = json_decode (file_get_contents ("php://input " ), true );
4040$ file = 'bitpay.txt ' ;
4141$ err = "bitpay_err.txt " ;
4242
4343file_put_contents ($ file ,"===========INCOMING IPN========================= " ,FILE_APPEND );
4444file_put_contents ($ file ,date ('d.m.Y H:i:s ' ),FILE_APPEND );
45- file_put_contents ($ file ,print_r ($ all_data , true ),FILE_APPEND );
45+ file_put_contents ($ file ,print_r ($ event , true ),FILE_APPEND );
4646file_put_contents ($ file ,"===========END OF IPN=========================== " ,FILE_APPEND );
4747
48- $ data = $ all_data ['data ' ];
49- $ order_status = $ data ['status ' ];
50- $ order_invoice = $ data ['id ' ];
48+ $ order_status = $ event ['status ' ];
49+ $ order_invoice = $ event ['id ' ];
5150$ endpoint = $ gatewayParams ['bitpay_checkout_endpoint ' ];
5251if ($ endpoint == "Test " ):
5352 $ url_check = 'https://test.bitpay.com/invoices/ ' .$ order_invoice ;
@@ -56,10 +55,8 @@ function checkInvoiceStatus($url){
5655endif ;
5756$ invoiceStatus = json_decode (checkInvoiceStatus ($ url_check ));
5857
59- $ event = $ all_data ['event ' ];
6058$ orderid = $ invoiceStatus ->data ->orderId ;
6159$ price = $ invoiceStatus ->data ->price ;
62-
6360#first see if the ipn matches
6461#get the user id first
6562$ table = "_bitpay_checkout_transactions " ;
@@ -71,10 +68,9 @@ function checkInvoiceStatus($url){
7168$ btn_id = $ rowdata ['transaction_id ' ];
7269
7370if ($ btn_id ):
74- switch ($ event ['name ' ]) {
71+ switch ($ event ['status ' ]) {
7572 #complete, update invoice table to Paid
76- case 'invoice_confirmed ' :
77-
73+ case 'complete ' :
7874
7975 $ table = "tblinvoices " ;
8076 $ update = array ("status " => 'Paid ' ,'datepaid ' => date ("Y-m-d H:i:s " ));
@@ -88,7 +84,7 @@ function checkInvoiceStatus($url){
8884
8985 #update the bitpay_invoice table
9086 $ table = "_bitpay_checkout_transactions " ;
91- $ update = array ("transaction_status " => $ event [ ' name ' ] );
87+ $ update = array ("transaction_status " => " complete " );
9288 $ where = array ("order_id " => $ orderid , "transaction_id " => $ order_invoice );
9389 try {
9490 update_query ($ table , $ update , $ where );
@@ -106,7 +102,7 @@ function checkInvoiceStatus($url){
106102 break ;
107103
108104 #processing - put in Payment Pending
109- case 'invoice_paidInFull ' :
105+ case 'paid ' :
110106 $ table = "tblinvoices " ;
111107 $ update = array ("status " => 'Payment Pending ' ,'datepaid ' => date ("Y-m-d H:i:s " ));
112108 $ where = array ("id " => $ orderid , "paymentmethod " => "bitpaycheckout " );
@@ -127,33 +123,8 @@ function checkInvoiceStatus($url){
127123 }
128124 break ;
129125
130- #confirmation error - put in Unpaid
131- case 'invoice_failedToConfirm ' :
132- case 'invoice_declined ' :
133-
134- $ table = "tblinvoices " ;
135- $ update = array ("status " => 'Unpaid ' );
136- $ where = array ("id " => $ orderid , "paymentmethod " => "bitpaycheckout " );
137- try {
138- update_query ($ table , $ update , $ where );
139- }catch (Exception $ e ){
140- file_put_contents ($ file ,$ e ,FILE_APPEND );
141- }
142-
143- #update the bitpay_invoice table
144- $ table = "_bitpay_checkout_transactions " ;
145- $ update = array ("transaction_status " => $ event ['name ' ]);
146- $ where = array ("order_id " => $ orderid , "transaction_id " => $ order_invoice );
147- try {
148- update_query ($ table , $ update , $ where );
149- }catch (Exception $ e ){
150- file_put_contents ($ file ,$ e ,FILE_APPEND );
151- }
152-
153- break ;
154-
155126 #expired, remove from transaction table, wont be in invoice table
156- case 'invoice_expired ' :
127+ case 'expired ' :
157128 #delete any orphans
158129 $ table = "_bitpay_checkout_transactions " ;
159130 $ delete = 'DELETE FROM _bitpay_checkout_transactions WHERE transaction_id = " ' . $ order_invoice .'" ' ;
@@ -163,37 +134,6 @@ function checkInvoiceStatus($url){
163134 file_put_contents ($ file ,$ e ,FILE_APPEND );
164135 }
165136 break ;
166-
167- #update both table to refunded
168- case 'invoice_refundComplete ' :
169-
170- #get the user id first
171- $ table = "tblaccounts " ;
172- $ fields = "id,userid " ;
173- $ where = array ("transid " => $ order_invoice );
174- $ result = select_query ($ table , $ fields , $ where );
175- $ rowdata = mysql_fetch_array ($ result );
176- $ id = $ rowdata ['id ' ];
177- $ userid = $ rowdata ['userid ' ];
178-
179-
180- #do an insert on tblaccounts
181- $ 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 " ));
182- $ newid = insert_query ($ table , $ values );
183-
184- #update the tblinvoices to show Refunded
185- $ table = "tblinvoices " ;
186- $ update = array ("status " => 'Refunded ' ,'datepaid ' => date ("Y-m-d H:i:s " ));
187- $ where = array ("id " => $ orderid , "paymentmethod " => "bitpaycheckout " );
188- update_query ($ table , $ update , $ where );
189-
190- #update the bitpay_invoice table
191- $ table = "_bitpay_checkout_transactions " ;
192- $ update = array ("transaction_status " => $ event ['name ' ]);
193- $ where = array ("order_id " => $ orderid , "transaction_id " => $ order_invoice );
194- update_query ($ table , $ update , $ where );
195-
196- break ;
197137}
198138http_response_code (200 );
199139endif ;#end of the table lookup
0 commit comments