· 7 years ago · Oct 30, 2018, 08:14 PM
1<?php
2/*
3 Polls the Bluepay reporting API to discover transactions that have been rejected
4 due to insufficient funds or other common reasons.
5
6 Grabs the output and organizes into an associative 'rejected' array that is easy
7 to parse. From here, you can easily find and update the failed transaction
8 to a 'collection' status.
9*/
10
11$accountID = "Merchant's Account ID Here";
12$secretKey = "Merchant's Secret Key Here";
13$mode = "TEST";
14
15$report = new BluePay(
16 $accountID,
17 $secretKey,
18 $mode
19);
20
21// report on all settled transactions from yesterday
22// to parse for rejection status
23$reportStart = date('Y-m-d', strtotime('-1 Days');
24$reportEnd = date("Y-m-d");
25
26$report->getSettledTransactionReport(array(
27 'reportStart' => $reportStart,
28 'reportEnd' => $reportEnd,
29 'subaccountsSearched' => '0',
30 'doNotEscape' => '1',
31 'responseVersion' => '2',
32 'errors'=> '1'
33));
34
35//makes the API request with BluePay
36$report->process();
37
38//get the response
39$response = $report->getResponse();
40
41//init the array that will hold each header title
42$headers = array();
43
44//init the array that will hold each line
45$lines = array();
46
47//create the array by delineating on line breaks
48$lines = explode("\n", $response);
49
50//check if there is anything in the response
51if (count($lines) == 0) {
52
53 //nothing to do...
54 die();
55}
56
57//get the first line and load it as the header
58$header = $lines[0];
59
60// strip quotes and breaks
61$header = str_replace('"', '', $header);
62$header = str_replace("\n", '', $header);
63$header = str_replace("\r", '', $header);
64
65//create the header array with all the header titles
66$headers = explode(",", $header);
67
68//this will hold all the completed entries for this batch
69$results = array();
70
71//loop the lines and manipulate the values to parse later
72for ($i=1; $i < count($lines); $i++) {
73
74 $this_line = $lines[$i];
75 $this_line = str_replace('"', '', $this_line);
76 $this_line = str_replace("\n", '', $this_line);
77 $this_line = str_replace("\r", '', $this_line);
78
79 //add to the lines array by delineating on commas
80 //between field values
81 $this_line = explode(",", $this_line);
82
83 //init an array to add the associative key/values for this item
84 //based on the field names in the header
85 $this_item = array();
86
87 /*
88 //using this sample data
89 "id","payment_type","trans_type","amount","card_type","payment_account","order_id","invoice_id","custom_id","custom_id2","master_id","status","f_void","message","origin","issue_date","settle_date","rebilling_id","settlement_id","card_expire","bank_name","addr1","addr2","city","state","zip","phone","email","auth_code","name1","name2","company_name","memo","backend_id","doc_type","f_captured","avs_result","cvv_result","card_present","merchdata","level_3_data","remote_ip","connected_ip","level_2_data"
90 100634281145,CREDIT,SALE,229.00,VISA,xxxxxxxxxxxx8151,1006222736,10223572736,session_5bd2749b,customer_5ba,100633572736,1,,AUTH/TKT,bp10emu,2018-10-27 17:40:58,2018-10-28 02:17:20,,1006369,0519,Chase Bank USA National Association,37 Kanes Lane,,Middletown,NJ,7748,,,01774G,Customer,Name,,,10010-468300816586387-V,,,N,P,0,shpf-form-id=auth,,22.222.64.155,111.222.11.222,
91 100632482657,ACH,VOID,199.00,ACH,C:122244676:xxxxxx3585,60247eea5bc7829b0ef46,100630025896,,customer_c7a3e,10061112354,1,,R01 ,REJECT,2018-10-23 00:00:00,2018-10-23 00:00:00,,100632482655,,VALLEY BANK,1119 South 4th St,,CITY,CA,91113,,,R01,Another Customer,Name,,,10063111265,WEB,,,,,,,,,
92
93 //this is how the item array will look
94
95 $this_item['id'] = '100632482657'
96 $this_item['payment_type'] = 'ACH'
97 $this_item['trans_type'] = 'VOID'
98 $this_item['amount'] = '199.00'
99 $this_item['card_type'] = 'ACH'
100 ...
101 */
102
103 //loop each item in the line and load up the associative array
104 //with the header key and line value (seen above)
105 for ($x=0; $x < count($this_line); $x++) {
106 $this_item[$headers[$x]] = $this_line[$x];
107 }
108
109 //add this to the results array
110 array_push($results, $this_item);
111}
112
113//create a reject array
114$rejects = array();
115
116//loop through the results and add the lines where the 'origin' value eq REJECT into the new array
117foreach ($results as $key => $value) {
118 if ($value["origin"] == "REJECT") {
119 //add this item
120 array_push($rejects, $value);
121 }
122}
123
124//loop through the lines and dump the lines where position XX value eq REJECT into the new array
125foreach ($rejects as $key => $value) {
126
127 //grab data to update the failing transaction in the DB
128 $invoice_id = $value["invoice_id"];
129 $amount = $value["amount"];
130
131 //update the transaction however you choose...
132 updateBillingLineItemAsCollections($invoice_id, $amount, $value);
133}