|
| 1 | + |
| 2 | + |
| 3 | +# Binance Pay API for PHP and Laravel using Curl |
| 4 | +Binance Pay API for PHP and Laravel - This is a simple and quick repo on how to initiate crypto payments using the Official Binance API. You can use this to initiate ecommerce payments or any other payments of your choise from your website. |
| 5 | + |
| 6 | +## Binance Pay API: Authentication |
| 7 | +The Binance Pay API uses API keys to authenticate requests. You can view and manage your API keys in the Binance Merchant Admin Portal. |
| 8 | + |
| 9 | +Your API keys carry many privileges, so be sure to keep them secure! Do not share your secret API keys in publicly accessible areas such as GitHub, client-side code, and so forth. |
| 10 | + |
| 11 | +All API requests must be made over HTTPS. Calls made over plain HTTP will fail. API requests without authentication will also fail. |
| 12 | + |
| 13 | +## Apply API identity key and API secret key# |
| 14 | +Register Merchant Account at Binance |
| 15 | +Login merchant account and create new API identity key/API secret key [Binance Merchant Admin Portal](https://merchant.binance.com/). |
| 16 | + |
| 17 | + |
| 18 | +## Create Order |
| 19 | +Create order API Version 2 used for merchant/partner to initiate acquiring order. |
| 20 | + |
| 21 | +### Base Url |
| 22 | + |
| 23 | +``` |
| 24 | +https://bpay.binanceapi.com |
| 25 | +``` |
| 26 | + |
| 27 | +### Endpoint |
| 28 | +``` |
| 29 | +POST /binancepay/openapi/v2/order |
| 30 | +``` |
| 31 | + |
| 32 | +### Request Parameters |
| 33 | +Check here for all the request parameters |
| 34 | +[-> Check here](https://developers.binance.com/docs/binance-pay/api-order-create-v2#request-parameters) |
| 35 | + |
| 36 | +## USAGE IN PHP |
| 37 | + |
| 38 | +``` |
| 39 | +<?php |
| 40 | + // Generate nonce string |
| 41 | + $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 42 | + $nonce = ''; |
| 43 | + for($i=1; $i <= 32; $i++) |
| 44 | + { |
| 45 | + $pos = mt_rand(0, strlen($chars) - 1); |
| 46 | + $char = $chars[$pos]; |
| 47 | + $nonce .= $char; |
| 48 | + } |
| 49 | + $ch = curl_init(); |
| 50 | + $timestamp = round(microtime(true) * 1000); |
| 51 | + // Request body |
| 52 | + $request = array( |
| 53 | + "env" => array( |
| 54 | + "terminalType" => "APP" |
| 55 | + ), |
| 56 | + "merchantTradeNo" => mt_rand(982538,9825382937292), |
| 57 | + "orderAmount" => 25.17, |
| 58 | + "currency" => "BUSD", |
| 59 | + "goods" => array( |
| 60 | + "goodsType" => "01", |
| 61 | + "goodsCategory" => "D000", |
| 62 | + "referenceGoodsId" => "7876763A3B", |
| 63 | + "goodsName" => "Ice Cream", |
| 64 | + "goodsDetail" => "Greentea ice cream cone" |
| 65 | + ) |
| 66 | + ); |
| 67 | + |
| 68 | + $json_request = json_encode($request); |
| 69 | + $payload = $timestamp."\n".$nonce."\n".$json_request."\n"; |
| 70 | + $binance_pay_key = "REPLACE-WITH-YOUR-BINANCE-MERCHANT-API-KEY"; |
| 71 | + $binance_pay_secret = "REPLACE-WITH-YOUR-BINANCE-MERCHANT-SCRETE-KEY"; |
| 72 | + $signature = strtoupper(hash_hmac('SHA512',$payload,$binance_pay_secret)); |
| 73 | + $headers = array(); |
| 74 | + $headers[] = "Content-Type: application/json"; |
| 75 | + $headers[] = "BinancePay-Timestamp: $timestamp"; |
| 76 | + $headers[] = "BinancePay-Nonce: $nonce"; |
| 77 | + $headers[] = "BinancePay-Certificate-SN: $binance_pay_key"; |
| 78 | + $headers[] = "BinancePay-Signature: $signature"; |
| 79 | +
|
| 80 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 81 | + curl_setopt($ch, CURLOPT_URL, "https://bpay.binanceapi.com/binancepay/openapi/v2/order"); |
| 82 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 83 | + curl_setopt($ch, CURLOPT_POST, 1); |
| 84 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $json_request); |
| 85 | +
|
| 86 | + $result = curl_exec($ch); |
| 87 | + if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } |
| 88 | + curl_close ($ch); |
| 89 | +
|
| 90 | + var_dump($result); |
| 91 | + |
| 92 | + //Redirect user to the payment page |
| 93 | +
|
| 94 | +?> |
| 95 | +``` |
| 96 | + |
| 97 | +## USAGE IN LARAVEL |
| 98 | + |
| 99 | +Use this in your targeted controller file. |
| 100 | + |
| 101 | +``` |
| 102 | +public function initiateBinancePay(){ |
| 103 | + // Generate nonce string |
| 104 | + $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 105 | + $nonce = ''; |
| 106 | + for($i=1; $i <= 32; $i++) |
| 107 | + { |
| 108 | + $pos = mt_rand(0, strlen($chars) - 1); |
| 109 | + $char = $chars[$pos]; |
| 110 | + $nonce .= $char; |
| 111 | + } |
| 112 | + $ch = curl_init(); |
| 113 | + $timestamp = round(microtime(true) * 1000); |
| 114 | + // Request body |
| 115 | + $request = array( |
| 116 | + "env" => array( |
| 117 | + "terminalType" => "APP" |
| 118 | + ), |
| 119 | + "merchantTradeNo" => mt_rand(982538,9825382937292), |
| 120 | + "orderAmount" => 25.17, |
| 121 | + "currency" => "BUSD", |
| 122 | + "goods" => array( |
| 123 | + "goodsType" => "01", |
| 124 | + "goodsCategory" => "D000", |
| 125 | + "referenceGoodsId" => "7876763A3B", |
| 126 | + "goodsName" => "Ice Cream", |
| 127 | + "goodsDetail" => "Greentea ice cream cone" |
| 128 | + ) |
| 129 | + ); |
| 130 | + |
| 131 | + $json_request = json_encode($request); |
| 132 | + $payload = $timestamp."\n".$nonce."\n".$json_request."\n"; |
| 133 | + $binance_pay_key = "REPLACE-WITH-YOUR-BINANCE-MERCHANT-API-KEY"; |
| 134 | + $binance_pay_secret = "REPLACE-WITH-YOUR-BINANCE-MERCHANT-SCRETE-KEY"; |
| 135 | + $signature = strtoupper(hash_hmac('SHA512',$payload,$binance_pay_secret)); |
| 136 | + $headers = array(); |
| 137 | + $headers[] = "Content-Type: application/json"; |
| 138 | + $headers[] = "BinancePay-Timestamp: $timestamp"; |
| 139 | + $headers[] = "BinancePay-Nonce: $nonce"; |
| 140 | + $headers[] = "BinancePay-Certificate-SN: $binance_pay_key"; |
| 141 | + $headers[] = "BinancePay-Signature: $signature"; |
| 142 | +
|
| 143 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 144 | + curl_setopt($ch, CURLOPT_URL, "https://bpay.binanceapi.com/binancepay/openapi/v2/order"); |
| 145 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 146 | + curl_setopt($ch, CURLOPT_POST, 1); |
| 147 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $json_request); |
| 148 | +
|
| 149 | + $result = curl_exec($ch); |
| 150 | + if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } |
| 151 | + curl_close ($ch); |
| 152 | +
|
| 153 | + var_dump($result); |
| 154 | +
|
| 155 | + //Redirect user to the payment page |
| 156 | +
|
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +## Sample SUCCESS JSON Response |
| 161 | + |
| 162 | +``` |
| 163 | +{ |
| 164 | + "status": "SUCCESS", |
| 165 | + "code": "000000", |
| 166 | + "data": { |
| 167 | + "prepayId": "29383937493038367292", |
| 168 | + "terminalType": "APP", |
| 169 | + "expireTime": 121123232223, |
| 170 | + "qrcodeLink": "https://qrservice.dev.com/en/qr/dplkb005181944f84b84aba2430e1177012b.jpg", |
| 171 | + "qrContent": "https://qrservice.dev.com/en/qr/dplk12121112b", |
| 172 | + "checkoutUrl": "https://pay.binance.com/checkout/dplk12121112b", |
| 173 | + "deeplink": "bnc://app.binance.com/payment/secpay/xxxxxx", |
| 174 | + "universalUrl": "https://app.binance.com/payment/secpay?_dp=xxx=&linkToken=xxx" |
| 175 | + }, |
| 176 | + "errorMessage": "" |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +## Binance Pay: Order Webhook Notification |
| 181 | +###Callback Webhook Endpoints |
| 182 | +Binance Pay will send order events with final status to partner for notification. You will be able to configure webhook endpoints via the Binance Merchant Admin Portal Result of the orders that are close will be notified to you through this webhook with bizStatus = "PAY_CLOSED" |
| 183 | + |
| 184 | +> I'll update a webhook callback usage for this soon! You'll be able to receive the JSON responses and validate payments even storing them to your database |
| 185 | +
|
| 186 | + |
| 187 | +## Upcoming Updates |
| 188 | + |
| 189 | +- [x] #C2B - Create Order/Initiate binance payment :tada: |
| 190 | +- [ ] #Receiving webhook responses through callback url for validating and storing response to database |
| 191 | +- [ ] #C2C - Customer to Customer crypto funds transfer using binance API |
| 192 | +- [ ] #B2C - Business to Customer crypto funds transfer using binance API (Bulk/Batch Payments) |
| 193 | +- [ ] #Refunds - Making refunds using the Binance API |
| 194 | +- [ ] #Create Sub-merchant API used for merchant/partner. |
| 195 | +- [ ] #Wallet Balance Query API used to query wallet balance. |
| 196 | +- [ ] #Integrate Binance Pay with your App SDK: Android & IOS |
| 197 | + |
| 198 | +## Binance Pay Official Documentation |
| 199 | +https://developers.binance.com/docs/binance-pay/introduction |
| 200 | + |
| 201 | +## Contribution |
| 202 | +- Pull requests are welcome. |
| 203 | +- Give us a star ⭐ |
| 204 | +- Fork and Clone! Awesome |
| 205 | +- Select existing issues or create a new issue and give us a PR with your bugfix or improvement after. We love it ❤️ |
| 206 | + |
| 207 | +> For any inquiries Email: enocmonyancha@gmail.com or Whatsapp +254799117020 |
| 208 | +
|
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
0 commit comments