11<?php
22/**
33 * The MIT License (MIT)
4- *
4+ *
55 * Copyright (c) 2011-2015 BitPay
6- *
6+ *
77 * Permission is hereby granted, free of charge, to any person obtaining a copy
88 * of this software and associated documentation files (the "Software"), to deal
99 * in the Software without restriction, including without limitation the rights
1010 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1111 * copies of the Software, and to permit persons to whom the Software is
1212 * furnished to do so, subject to the following conditions:
13- *
13+ *
1414 * The above copyright notice and this permission notice shall be included in
1515 * all copies or substantial portions of the Software.
16- *
16+ *
1717 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1818 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1919 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2626require_once 'bp_options.php ' ;
2727
2828function bpCurl ($ url , $ apiKey , $ post = false ) {
29- global $ bpOptions ;
30-
29+ global $ bpOptions ;
30+
3131 $ curl = curl_init ($ url );
3232 $ length = 0 ;
3333 if ($ post )
34- {
34+ {
3535 curl_setopt ($ curl , CURLOPT_POST , 1 );
3636 curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ post );
3737 $ length = strlen ($ post );
3838 }
39-
39+
4040 $ uname = base64_encode ($ apiKey );
4141 $ header = array (
4242 'Content-Type: application/json ' ,
@@ -54,9 +54,9 @@ function bpCurl($url, $apiKey, $post = false) {
5454 curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , 1 );
5555 curl_setopt ($ curl , CURLOPT_FORBID_REUSE , 1 );
5656 curl_setopt ($ curl , CURLOPT_FRESH_CONNECT , 1 );
57-
57+
5858 $ responseString = curl_exec ($ curl );
59-
59+
6060 if ($ responseString == false ) {
6161 $ response = curl_error ($ curl );
6262 } else {
@@ -65,43 +65,43 @@ function bpCurl($url, $apiKey, $post = false) {
6565 curl_close ($ curl );
6666 return $ response ;
6767}
68- // $orderId: Used to display an orderID to the buyer. In the account summary view, this value is used to
68+ // $orderId: Used to display an orderID to the buyer. In the account summary view, this value is used to
6969// identify a ledger entry if present.
7070//
71- // $price: by default, $price is expressed in the currency you set in bp_options.php. The currency can be
71+ // $price: by default, $price is expressed in the currency you set in bp_options.php. The currency can be
7272// changed in $options.
7373//
7474// $posData: this field is included in status updates or requests to get an invoice. It is intended to be used by
7575// the merchant to uniquely identify an order associated with an invoice in their system. Aside from that, Bit-Pay does
7676// not use the data in this field. The data in this field can be anything that is meaningful to the merchant.
7777//
78- // $options keys can include any of:
78+ // $options keys can include any of:
7979// ('itemDesc', 'itemCode', 'notificationEmail', 'notificationURL', 'redirectURL', 'apiKey'
80- // 'currency', 'physical', 'fullNotifications', 'transactionSpeed', 'buyerName',
80+ // 'currency', 'physical', 'fullNotifications', 'transactionSpeed', 'buyerName',
8181// 'buyerAddress1', 'buyerAddress2', 'buyerCity', 'buyerState', 'buyerZip', 'buyerEmail', 'buyerPhone')
8282// If a given option is not provided here, the value of that option will default to what is found in bp_options.php
8383// (see api documentation for information on these options).
84- function bpCreateInvoice ($ orderId , $ price , $ posData , $ options = array ()) {
85- global $ bpOptions ;
86-
84+ function bpCreateInvoice ($ orderId , $ price , $ posData , $ options = array ()) {
85+ global $ bpOptions ;
86+
8787 $ options = array_merge ($ bpOptions , $ options ); // $options override any options found in bp_options.php
88-
88+
8989 $ options ['posData ' ] = '{"posData": " ' . $ posData . '" ' ;
9090 if ($ bpOptions ['verifyPos ' ]) // if desired, a hash of the POS data is included to verify source in the callback
9191 $ options ['posData ' ].= ', "hash": " ' . crypt ($ posData , $ options ['apiKey ' ]).'" ' ;
92- $ options ['posData ' ].= '} ' ;
93-
92+ $ options ['posData ' ].= '} ' ;
93+
9494 $ options ['orderID ' ] = $ orderId ;
9595 $ options ['price ' ] = $ price ;
96-
97- $ postOptions = array ('orderID ' , 'itemDesc ' , 'itemCode ' , 'notificationEmail ' , 'notificationURL ' , 'redirectURL ' ,
98- 'posData ' , 'price ' , 'currency ' , 'physical ' , 'fullNotifications ' , 'transactionSpeed ' , 'buyerName ' ,
96+
97+ $ postOptions = array ('orderID ' , 'itemDesc ' , 'itemCode ' , 'notificationEmail ' , 'notificationURL ' , 'redirectURL ' ,
98+ 'posData ' , 'price ' , 'currency ' , 'physical ' , 'fullNotifications ' , 'transactionSpeed ' , 'buyerName ' ,
9999 'buyerAddress1 ' , 'buyerAddress2 ' , 'buyerCity ' , 'buyerState ' , 'buyerZip ' , 'buyerEmail ' , 'buyerPhone ' );
100100 foreach ($ postOptions as $ o )
101101 if (array_key_exists ($ o , $ options ))
102102 $ post [$ o ] = $ options [$ o ];
103103 $ post = json_encode ($ post );
104-
104+
105105 $ response = bpCurl ('https://bitpay.com/api/invoice/ ' , $ options ['apiKey ' ], $ post );
106106
107107 return $ response ;
@@ -111,25 +111,25 @@ function bpCreateInvoice($orderId, $price, $posData, $options = array()) {
111111function bpVerifyNotification ($ apiKey = false ) {
112112 global $ bpOptions ;
113113 if (!$ apiKey )
114- $ apiKey = $ bpOptions ['apiKey ' ];
115-
114+ $ apiKey = $ bpOptions ['apiKey ' ];
115+
116116 $ post = file_get_contents ("php://input " );
117117 if (!$ post )
118118 return 'No post data ' ;
119-
119+
120120 $ json = json_decode ($ post , true );
121-
121+
122122 if (is_string ($ json ))
123123 return $ json ; // error
124124
125- if (!array_key_exists ('posData ' , $ json ))
125+ if (!array_key_exists ('posData ' , $ json ))
126126 return 'no posData ' ;
127-
127+
128128 $ posData = json_decode ($ json ['posData ' ], true );
129- if ($ bpOptions ['verifyPos ' ] and $ posData ['hash ' ] != crypt ($ posData ['posData ' ], $ apiKey ))
129+ if ($ bpOptions ['verifyPos ' ] and $ posData ['hash ' ] != crypt ($ posData ['posData ' ], $ apiKey ))
130130 return 'authentication failed (bad hash) ' ;
131131 $ json ['posData ' ] = $ posData ['posData ' ];
132-
132+
133133 if (!array_key_exists ('id ' , $ json ))
134134 {
135135 return 'Cannot find invoice ID ' ;
@@ -142,17 +142,13 @@ function bpVerifyNotification($apiKey = false) {
142142function bpGetInvoice ($ invoiceId , $ apiKey =false ) {
143143 global $ bpOptions ;
144144 if (!$ apiKey )
145- $ apiKey = $ bpOptions ['apiKey ' ];
145+ $ apiKey = $ bpOptions ['apiKey ' ];
146146
147147 $ response = bpCurl ('https://bitpay.com/api/invoice/ ' .$ invoiceId , $ apiKey );
148148 if (is_string ($ response ))
149149 return $ response ; // error
150150 $ response ['posData ' ] = json_decode ($ response ['posData ' ], true );
151- if ($ bpOptions ['verifyPos ' ])
152- {
153- $ response ['posData ' ] = $ response ['posData ' ]['posData ' ];
154- }
155- return $ response ;
151+ return $ response ;
156152}
157153
158154?>
0 commit comments