|
1 |
| -# webirr-api-php-client |
2 | 1 | Official PHP Client Library for WeBirr Payment Gateway APIs
|
| 2 | + |
| 3 | +This Client Library provides convenient access to WeBirr Payment Gateway APIs from PHP Applications. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```bash |
| 8 | +$ composer require webirr/webirr |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +The library needs to be configured with a *merchant Id* & *API key*. You can get it by contacting [webirr.com](https://webirr.com) |
| 14 | + |
| 15 | +> You can use this library for production or test environments. you will need to set isTestEnv=true for test, and false for production apps when creating objects of class WeBirrClient |
| 16 | +
|
| 17 | +## Example |
| 18 | + |
| 19 | +### Creating a new Bill / Updating an existing Bill on WeBirr Servers |
| 20 | + |
| 21 | +```php |
| 22 | +<?php |
| 23 | + |
| 24 | +require 'vendor/autoload.php'; |
| 25 | + |
| 26 | +use WeBirr\Bill; |
| 27 | +use WeBirr\WeBirrClient; |
| 28 | + |
| 29 | +// Create & Update Bill |
| 30 | +function main() |
| 31 | +{ |
| 32 | + $apiKey = 'YOUR_API_KEY'; |
| 33 | + $merchantId = 'YOUR_MERCHANT_ID'; |
| 34 | + |
| 35 | + //$apiKey = getenv('wb_apikey_1'); |
| 36 | + //$merchantId = getenv('wb_merchid_1'); |
| 37 | + |
| 38 | + $api = new WeBirrClient($apiKey, true); |
| 39 | + |
| 40 | + $bill = new Bill(); |
| 41 | + |
| 42 | + $bill->amount = '270.90'; |
| 43 | + $bill->customerCode = 'cc01'; // it can be email address or phone number if you dont have customer code |
| 44 | + $bill->customerName = 'Elias Haileselassie'; |
| 45 | + $bill->time = '2021-07-22 22:14'; // your bill time, always in this format |
| 46 | + $bill->description = 'hotel booking'; |
| 47 | + $bill->billReference = 'php/2021/127'; // your unique reference number |
| 48 | + $bill->merchantID = $merchantId; |
| 49 | + |
| 50 | + echo "\nCreating Bill..."; |
| 51 | + |
| 52 | + $res = $api->createBill($bill); |
| 53 | + |
| 54 | + if (!$res->error) { |
| 55 | + // success |
| 56 | + $paymentCode = $res->res; // returns paymentcode such as 429 723 975 |
| 57 | + echo "\nPayment Code = $paymentCode"; // we may want to save payment code in local db. |
| 58 | + |
| 59 | + } else { |
| 60 | + // fail |
| 61 | + echo "\nerror: $res->error"; |
| 62 | + echo "\nerrorCode: $res->errorCode"; // can be used to handle specific busines error such as ERROR_INVLAID_INPUT_DUP_REF |
| 63 | + } |
| 64 | + |
| 65 | + //var_dump($res); |
| 66 | + |
| 67 | + // Update existing bill if it is not paid |
| 68 | + $bill->amount = "278.00"; |
| 69 | + $bill->customerName = 'Elias php'; |
| 70 | + //$bill->billReference = "WE CAN NOT CHANGE THIS"; |
| 71 | + |
| 72 | + echo "\nUpdating Bill..."; |
| 73 | + |
| 74 | + $res = $api->updateBill($bill); |
| 75 | + |
| 76 | + if (!$res->error) { |
| 77 | + // success |
| 78 | + echo "\nbill is updated succesfully"; //res.res will be 'OK' no need to check here! |
| 79 | + } else { |
| 80 | + // fail |
| 81 | + echo "\nerror: $res->error"; |
| 82 | + echo "\nerrorCode: $res->errorCode"; // can be used to handle specific busines error such as ERROR_INVLAID_INPUT |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +main(); |
| 87 | + |
| 88 | +``` |
| 89 | + |
| 90 | +### Getting Payment status of an existing Bill from WeBirr Servers |
| 91 | + |
| 92 | +```php |
| 93 | +<?php |
| 94 | + |
| 95 | +require 'vendor/autoload.php'; |
| 96 | + |
| 97 | +use WeBirr\Bill; |
| 98 | +use WeBirr\WeBirrClient; |
| 99 | + |
| 100 | +// Get Payment Status of Bill |
| 101 | +function main() |
| 102 | +{ |
| 103 | + $apiKey = 'YOUR_API_KEY'; |
| 104 | + $merchantId = 'YOUR_MERCHANT_ID'; |
| 105 | + |
| 106 | + //$apiKey = getenv('wb_apikey_1'); |
| 107 | + //$merchantId = getenv('wb_merchid_1'); |
| 108 | + |
| 109 | + $api = new WeBirrClient($apiKey, true); |
| 110 | + |
| 111 | + $paymentCode = 'PAYMENT_CODE_YOU_SAVED_AFTER_CREATING_A_NEW_BILL'; // suchas as '141 263 782'; |
| 112 | + |
| 113 | + echo "\nGetting Payment Status..."; |
| 114 | + |
| 115 | + $res = $api->getPaymentStatus($paymentCode); |
| 116 | + |
| 117 | + if (!$res->error) { |
| 118 | + // success |
| 119 | + if ($res->res->status == 2) { |
| 120 | + $data = $res->res->data; |
| 121 | + echo "\nbill is paid"; |
| 122 | + echo "\nbill payment detail"; |
| 123 | + echo "\nBank: $data->bankID"; |
| 124 | + echo "\nBank Reference Number: $data->paymentReference"; |
| 125 | + echo "\nAmount Paid: $data->amount"; |
| 126 | + } else |
| 127 | + echo "\nbill is pending payment"; |
| 128 | + } else { |
| 129 | + // fail |
| 130 | + echo "\nerror: $res->error"; |
| 131 | + echo "\nerrorCode: $res->errorCode"; // can be used to handle specific busines error such as ERROR_INVLAID_INPUT |
| 132 | + } |
| 133 | + |
| 134 | + //var_dump($res); |
| 135 | +} |
| 136 | + |
| 137 | +main(); |
| 138 | + |
| 139 | +``` |
| 140 | + |
| 141 | +*Sample object returned from getPaymentStatus()* |
| 142 | + |
| 143 | +```javascript |
| 144 | +{ |
| 145 | + error: null, |
| 146 | + res: { |
| 147 | + status: 2, |
| 148 | + data: { |
| 149 | + id: 111112347, |
| 150 | + paymentReference: '8G3303GHJN', |
| 151 | + confirmed: true, |
| 152 | + confirmedTime: '2021-07-03 10:25:35', |
| 153 | + bankID: 'cbe_birr', |
| 154 | + time: '2021-07-03 10:25:33', |
| 155 | + amount: '4.60', |
| 156 | + wbcCode: '624 549 955' |
| 157 | + } |
| 158 | + }, |
| 159 | + errorCode: null |
| 160 | +} |
| 161 | + |
| 162 | +``` |
| 163 | + |
| 164 | +### Deleting an existing Bill from WeBirr Servers (if it is not paid) |
| 165 | + |
| 166 | +```php |
| 167 | +<?php |
| 168 | + |
| 169 | +require 'vendor/autoload.php'; |
| 170 | + |
| 171 | +use WeBirr\WeBirrClient; |
| 172 | + |
| 173 | +// Delete an existing Bill |
| 174 | +function main() |
| 175 | +{ |
| 176 | + $apiKey = 'YOUR_API_KEY'; |
| 177 | + $merchantId = 'YOUR_MERCHANT_ID'; |
| 178 | + |
| 179 | + //$apiKey = getenv('wb_apikey_1'); |
| 180 | + //$merchantId = getenv('wb_merchid_1'); |
| 181 | + |
| 182 | + $api = new WeBirrClient($apiKey, true); |
| 183 | + |
| 184 | + $paymentCode = 'PAYMENT_CODE_YOU_SAVED_AFTER_CREATING_A_NEW_BILL'; // suchas as '141 263 782'; |
| 185 | + |
| 186 | + echo "\nDeleting Bill..."; |
| 187 | + |
| 188 | + $res = $api->deleteBill($paymentCode); |
| 189 | + |
| 190 | + if (!$res->error) { |
| 191 | + // success |
| 192 | + echo "\nbill is deleted succesfully"; //res.res will be 'OK' no need to check here! |
| 193 | + } else { |
| 194 | + // fail |
| 195 | + echo "\nerror: $res->error"; |
| 196 | + echo "\nerrorCode: $res->errorCode"; // can be used to handle specific bussines error such as ERROR_INVLAID_INPUT |
| 197 | + } |
| 198 | + //var_dump($res); |
| 199 | +} |
| 200 | + |
| 201 | +main(); |
| 202 | + |
| 203 | +``` |
0 commit comments