|
1 | | -# webirr-api-javascript-client |
2 | | -Official JavaScript Client Library for WeBirr APIs |
| 1 | +Official JavaScript Client Library for WeBirr Payment Gateway APIs |
| 2 | + |
| 3 | +This Client Library provides convenient access to WeBirr Payment Gateway APIs from JavaScript/Node/ReactNative Apps. |
| 4 | + |
| 5 | +*Requires Javascript engine in browser or Node* |
| 6 | + |
| 7 | +## Install |
| 8 | + |
| 9 | +run the following command to install webirr client library |
| 10 | + |
| 11 | +With npm |
| 12 | + |
| 13 | +```bash |
| 14 | +$ npm install webirr |
| 15 | +``` |
| 16 | +With yarn |
| 17 | + |
| 18 | +```bash |
| 19 | +$ yarn add webirr |
| 20 | +``` |
| 21 | + |
| 22 | +With bower |
| 23 | + |
| 24 | +```bash |
| 25 | +$ bower install webirr |
| 26 | +``` |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +The library needs to be configured with a *merchant Id* & *API key*. You can get it by contacting [webirr.com](https://webirr.com) |
| 31 | + |
| 32 | +> 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 |
| 33 | +
|
| 34 | +## Example |
| 35 | + |
| 36 | +### Creating a new Bill / Updating an existing Bill on WeBirr Servers |
| 37 | + |
| 38 | +```javascript |
| 39 | + |
| 40 | +const WeBirrClient = require('webirr'); |
| 41 | + |
| 42 | +async function main() |
| 43 | +{ |
| 44 | + const apikey = 'YOUR_API_KEY'; |
| 45 | + const merchantId = 'YOUR_MERCHANT_ID'; |
| 46 | + |
| 47 | + var api = new WeBirrClient(apiKey, true); |
| 48 | + |
| 49 | + let bill = { |
| 50 | + amount: '270.90', |
| 51 | + customerCode: 'cc01', // it can be email address or phone number if you dont have customer code |
| 52 | + customerName: 'Elias Haileselassie', |
| 53 | + time: '2021-07-22 22:14', // your bill time, always in this format |
| 54 | + description: 'hotel booking', |
| 55 | + billReference: 'drt/2021/131', // your unique reference number |
| 56 | + merchantID: merchantId, |
| 57 | + }; |
| 58 | + |
| 59 | + console.log('Creating Bill...'); |
| 60 | + var res = await api.createBill(bill); |
| 61 | + |
| 62 | + if (!res.error) { |
| 63 | + // success |
| 64 | + let paymentCode = res.res; // returns paymentcode such as 429 723 975 |
| 65 | + console.log( `Payment Code = ${paymentCode}`); // we may want to save payment code in local db. |
| 66 | + |
| 67 | + } else { |
| 68 | + // fail |
| 69 | + console.log(`error: ${res.error}`); |
| 70 | + console.log( |
| 71 | + `errorCode: ${res.errorCode}`); // can be used to handle specific busines error such as ERROR_INVLAID_INPUT_DUP_REF |
| 72 | + } |
| 73 | + |
| 74 | + // update existing bill if it is not paid |
| 75 | + bill.amount = "278.00"; |
| 76 | + bill.customerName = 'Elias javascript'; |
| 77 | + //bill.billReference = "WE CAN NOT CHANGE THIS"; |
| 78 | + |
| 79 | + console.log('Updating Bill...'); |
| 80 | + res = await api.updateBill(bill); |
| 81 | + |
| 82 | + if (!res.error) { |
| 83 | + // success |
| 84 | + console.log('bill is updated succesfully'); //res.res will be 'OK' no need to check here! |
| 85 | + } else { |
| 86 | + // fail |
| 87 | + console.log(`error: ${res.error}`); |
| 88 | + console.log(`errorCode: ${res.errorCode}`); // can be used to handle specific busines error such as ERROR_INVLAID_INPUT |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | +main(); |
| 94 | + |
| 95 | +``` |
| 96 | + |
| 97 | +### Getting Payment status of an existing Bill from WeBirr Servers |
| 98 | + |
| 99 | +```javascript |
| 100 | + |
| 101 | +const WeBirrClient = require('webirr'); |
| 102 | + |
| 103 | +async function main() |
| 104 | +{ |
| 105 | + const apikey = 'YOUR_API_KEY'; |
| 106 | + const merchantId = 'YOUR_MERCHANT_ID'; |
| 107 | + |
| 108 | + var api = new WeBirrClient(apiKey, true); |
| 109 | + |
| 110 | + var paymentCode = 'PAYMENT_CODE_YOU_SAVED_AFTER_CREATING_A_NEW_BILL' // suchas as '141 263 782'; |
| 111 | + |
| 112 | + console.log('Getting Payment Status...'); |
| 113 | + var r = await api.getPaymentStatus('624549955') |
| 114 | + |
| 115 | + if (!r.error) { |
| 116 | + // success |
| 117 | + if (r.res.isPaid) { |
| 118 | + console.log('bill is paid'); |
| 119 | + console.log('bill payment detail'); |
| 120 | + console.log(`Bank: ${r.res.data.bankID}`); |
| 121 | + console.log(`Bank Reference Number: ${r.res.data.paymentReference}`); |
| 122 | + console.log('Amount Paid: ${r.res?.data?.amount}'); |
| 123 | + } else |
| 124 | + console.log('bill is pending payment'); |
| 125 | + } else { |
| 126 | + // fail |
| 127 | + console.log(`error: ${r.error}`); |
| 128 | + console.log( `errorCode: ${r.errorCode}`); // can be used to handle specific busines error such as ERROR_INVLAID_INPUT |
| 129 | + } |
| 130 | + |
| 131 | +} |
| 132 | + |
| 133 | +main(); |
| 134 | + |
| 135 | +``` |
| 136 | + |
| 137 | +### Deleting an existing Bill from WeBirr Servers (if it is not paid) |
| 138 | + |
| 139 | +```javascript |
| 140 | + |
| 141 | +const WeBirrClient = require('webirr'); |
| 142 | + |
| 143 | +async function main() |
| 144 | +{ |
| 145 | + const apikey = 'YOUR_API_KEY'; |
| 146 | + const merchantId = 'YOUR_MERCHANT_ID'; |
| 147 | + |
| 148 | + var api = new WeBirrClient(apiKey, true); |
| 149 | + |
| 150 | + var paymentCode = 'PAYMENT_CODE_YOU_SAVED_AFTER_CREATING_A_NEW_BILL' // suchas as '141 263 782'; |
| 151 | + |
| 152 | + console.log('Deleting Bill...'); |
| 153 | + res = await api.deleteBill(paymentCode); |
| 154 | + |
| 155 | + if (!res.error) { |
| 156 | + // success |
| 157 | + console.log('bill is deleted succesfully'); //res.res will be 'OK' no need to check here! |
| 158 | + } else { |
| 159 | + // fail |
| 160 | + console.log(`error: ${res.error}`); |
| 161 | + console.log(`errorCode: ${res.errorCode}`); // can be used to handle specific bussines error such as ERROR_INVLAID_INPUT |
| 162 | + } |
| 163 | + |
| 164 | +} |
| 165 | + |
| 166 | +main(); |
| 167 | + |
| 168 | +``` |
0 commit comments