|
1 |
| -# webirr-api-kotlin-jvm-client |
2 | 1 | Official Kotlin/JVM Client Library for WeBirr Payment Gateway APIs
|
| 2 | + |
| 3 | +This Client Library provides convenient access to WeBirr Payment Gateway APIs from Java/Kotlin/JVM Apps. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +include the following lines to add webirr client library into your project build |
| 8 | + |
| 9 | +With gradle |
| 10 | + |
| 11 | +Step 1. Add the JitPack repository to your build file |
| 12 | + |
| 13 | +```groovy |
| 14 | +allprojects { |
| 15 | + repositories { |
| 16 | + ... |
| 17 | + maven { url 'https://jitpack.io' } |
| 18 | + } |
| 19 | +} |
| 20 | +``` |
| 21 | +Step 2. Add the dependency |
| 22 | +```groovy |
| 23 | +dependencies { |
| 24 | + implementation 'com.webirr:webirr:Tag' |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +With maven |
| 29 | + |
| 30 | +Step 1. Add the JitPack repository to your build file |
| 31 | + |
| 32 | +```xml |
| 33 | +<repositories> |
| 34 | + <repository> |
| 35 | + <id>jitpack.io</id> |
| 36 | + <url>https://jitpack.io</url> |
| 37 | + </repository> |
| 38 | +</repositories> |
| 39 | +``` |
| 40 | + |
| 41 | +Step 2. Add the dependency |
| 42 | + |
| 43 | +```xml |
| 44 | +<dependency> |
| 45 | + <groupId>com.webirr</groupId> |
| 46 | + <artifactId>webirr</artifactId> |
| 47 | + <version>Tag</version> |
| 48 | +</dependency> |
| 49 | +``` |
| 50 | + |
| 51 | +## Usage |
| 52 | + |
| 53 | +The library needs to be configured with a *merchant Id* & *API key*. You can get it by contacting [webirr.com](https://webirr.com) |
| 54 | + |
| 55 | +> 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 |
| 56 | +
|
| 57 | +## Example |
| 58 | + |
| 59 | +```kotlin |
| 60 | +package webirr.example |
| 61 | + |
| 62 | +import webirr.*; |
| 63 | + |
| 64 | +val apiKey = "YOUR_API_KEY" |
| 65 | +val merchantId = "YOUR_MERCHANT_ID" |
| 66 | + |
| 67 | +//val apiKey = System.getenv("wb_apikey_1") ?: "" |
| 68 | +//val merchantId = System.getenv("wb_merchid_1") ?: "" |
| 69 | + |
| 70 | +fun main() { |
| 71 | + |
| 72 | + createAndUpdateBillAsync(); Thread.sleep(2000); |
| 73 | + getPaymentStatusAsync(); Thread.sleep(2000); |
| 74 | + deleteBillAsync() |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Creating a new Bill / Updating an existing Bill on WeBirr Servers |
| 80 | + */ |
| 81 | +fun createAndUpdateBillAsync(){ |
| 82 | + |
| 83 | + val api = WeBirrClient(apiKey, true) |
| 84 | + |
| 85 | + var bill = Bill( |
| 86 | + "cc01", |
| 87 | + "Elias Haileselassie", |
| 88 | + "kt/2021/130", |
| 89 | + "2021-07-22 22:14", |
| 90 | + "hotel booking", |
| 91 | + "270.90", |
| 92 | + merchantId, |
| 93 | + ) |
| 94 | + |
| 95 | + println("Creating Bill...") |
| 96 | + |
| 97 | + api.createBillAsync(bill) { it -> |
| 98 | + |
| 99 | + if (it.error == null) { |
| 100 | + // success |
| 101 | + val paymentCode = it.res ?: "" // returns paymentcode such as 429 723 975 |
| 102 | + println("Payment Code = ${paymentCode}") // we may want to save payment code in local db. |
| 103 | + |
| 104 | + } else { |
| 105 | + // fail |
| 106 | + println("error: ${it.error}") |
| 107 | + println("errorCode: ${it.errorCode}") // can be used to handle specific busines error such as ERROR_INVLAID_INPUT_DUP_REF |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // the above method call is async! |
| 112 | + Thread.sleep(2000) |
| 113 | + |
| 114 | + // update existing bill if it is not paid |
| 115 | + bill.amount = "278.00" |
| 116 | + bill.customerName = "Elias kotlin" |
| 117 | + //bill.billReference = "WE SHOULD NOT CHANGE THIS"; |
| 118 | + |
| 119 | + println("Updating Bill..."); |
| 120 | + api.updateBillAsync(bill) { it -> |
| 121 | + |
| 122 | + if (it.error == null) { |
| 123 | + // success |
| 124 | + println("bill is updated successfully") //it.res will be 'OK' no need to check here! |
| 125 | + |
| 126 | + } else { |
| 127 | + // fail |
| 128 | + println("error: ${it.error}") |
| 129 | + println("errorCode: ${it.errorCode}") // can be used to handle specific busines error such as ERROR_INVLAID_INPUT |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Getting Payment status of an existing Bill from WeBirr Servers |
| 136 | + */ |
| 137 | +fun getPaymentStatusAsync(){ |
| 138 | + |
| 139 | + val api = WeBirrClient(apiKey, true) |
| 140 | + |
| 141 | + var paymentCode = "PAYMENT_CODE_YOU_SAVED_AFTER_CREATING_A_NEW_BILL" // such as '141 263 782'; |
| 142 | + |
| 143 | + println("Getting Payment Status...") |
| 144 | + |
| 145 | + api.getPaymentStatusAsync(paymentCode) { it -> |
| 146 | + |
| 147 | + if (it.error == null) { |
| 148 | + // success |
| 149 | + if (it.res?.isPaid ?: false) |
| 150 | + { |
| 151 | + println("bill is paid"); |
| 152 | + println("bill payment detail"); |
| 153 | + println("Bank: ${it.res?.data?.bankID}"); |
| 154 | + println("Bank Reference Number: ${it.res?.data?.paymentReference}"); |
| 155 | + println("Amount Paid: ${it.res?.data?.amount}"); |
| 156 | + } |
| 157 | + else |
| 158 | + println("bill is pending payment"); |
| 159 | + |
| 160 | + } else { |
| 161 | + // fail |
| 162 | + println("error: ${it.error}"); |
| 163 | + println("errorCode: ${it.errorCode}"); // can be used to handle specific busines error such as ERROR_INVLAID_INPUT |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | +} |
| 168 | + |
| 169 | +fun deleteBillAsync(){ |
| 170 | + |
| 171 | + val api = WeBirrClient(apiKey, true) |
| 172 | + |
| 173 | + var paymentCode = "PAYMENT_CODE_YOU_SAVED_AFTER_CREATING_A_NEW_BILL" // suchas as '141 263 782'; |
| 174 | + |
| 175 | + println("Deleting Bill...") |
| 176 | + |
| 177 | + api.deleteBillAsync(paymentCode) { it -> |
| 178 | + |
| 179 | + if (it.error == null) { |
| 180 | + // success |
| 181 | + println("bill is deleted successfully"); //res.res will be 'OK' no need to check here! |
| 182 | + } else { |
| 183 | + // fail |
| 184 | + println("error: ${it.error}"); |
| 185 | + println("errorCode: ${it.errorCode}"); // can be used to handle specific busines error such as ERROR_INVLAID_INPUT |
| 186 | + } |
| 187 | + |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +``` |
| 192 | + |
| 193 | + |
| 194 | + |
0 commit comments