|
| 1 | +Fat Zebra Java Library |
| 2 | +====================== |
| 3 | + |
| 4 | +Release version 1.3 |
| 5 | + |
| 6 | +This library provides the basic functionality for developers to integrate their applications against the Fat Zebra API. |
| 7 | + |
| 8 | +Currently the library provides the following functionality: |
| 9 | + |
| 10 | + * Create a Purchase |
| 11 | + * Find a Purchase |
| 12 | + * Refund a Purchase |
| 13 | + * Pre-auth a purchase (do not capture the purchase) |
| 14 | + * Capture a pre-authorised purchase |
| 15 | + |
| 16 | +Usage |
| 17 | +----- |
| 18 | + |
| 19 | +Within your application you can use the following code to perform transactions: |
| 20 | + |
| 21 | +```java |
| 22 | + |
| 23 | +// Setup - you only need to do this once upon application initialization |
| 24 | +FatZebra.username = "TEST"; |
| 25 | +FatZebra.token = "TEST"; |
| 26 | +FatZebra.sandbox = true; // Omit this for production |
| 27 | + |
| 28 | +// Alternatively you can setup a Gateway Context: |
| 29 | + |
| 30 | +GatewayContext ctx = new GatewayContext(); |
| 31 | + |
| 32 | +int cardExpiryMonth = 7; |
| 33 | +int cardExpiryYear = 2023; |
| 34 | + |
| 35 | +HashMap<String,Object> card_data = new HashMap<String, Object>(); |
| 36 | +card_data.put("card_number", "5123456789012346"); |
| 37 | +card_data.put("card_holder", "James Smith"); |
| 38 | +card_data.put("cvv", "123"); |
| 39 | +card_data.put("card_expiry", String.format("%d/%d", cardExpiryMonth, cardExpiryYear)); |
| 40 | + |
| 41 | +try { |
| 42 | + Purchase p = Purchase.create( |
| 43 | + 100, // The amount, as an integer. i.e. $150.75 would be 15075 |
| 44 | + card_data, // A HashMap of the card data we defined above |
| 45 | + "my_reference", // Your order reference, such as an invoice number |
| 46 | + "1.2.3.4", // The customers IP address |
| 47 | + "AUD" // The currency code (such as AUD, USD, EUR, GBP etc) |
| 48 | + ); |
| 49 | +} catch(IOException ex) { |
| 50 | + System.err.println("IOException in purchase request."); |
| 51 | + ex.printStackTrace(); |
| 52 | +} catch(APIError ex) { |
| 53 | + System.err.println("API Error in purchase request."); |
| 54 | + ex.printStackTrace(); |
| 55 | +} catch(NetworkError ex) { |
| 56 | + System.err.println("Network Error in purchase request."); |
| 57 | + ex.printStackTrace(); |
| 58 | +} |
| 59 | + |
| 60 | +// If no exceptions are raised then you can inspect the purchase object: |
| 61 | + |
| 62 | +if (p.successful) { |
| 63 | + // Ok we are good to go with the successful transactions. |
| 64 | + System.out.println(String.format("Transaction successful: %s", p.toString()); |
| 65 | +} else { |
| 66 | + // The transaction was declined in the bank network |
| 67 | + System.err.println(String.format("Transaction failed. Message: %s. Transaction: %s", p.message, p.toString())); |
| 68 | +} |
| 69 | + |
| 70 | +// If the transaction is successful you can refund it: |
| 71 | + |
| 72 | +try { |
| 73 | + boolean refunded = p.refund( |
| 74 | + 100, // Amount of refund - must be less then or equal to the transaction amount or remaining amount should any refunds be already made |
| 75 | + "my_refund_reference" // Optional - if omitted we will use the purchase original reference. |
| 76 | + ); |
| 77 | +} catch(IOException ex) { |
| 78 | + System.err.println("IOException in refund request."); |
| 79 | + ex.printStackTrace(); |
| 80 | +} catch(APIError ex) { |
| 81 | + System.err.println("API Error in refund request."); |
| 82 | + ex.printStackTrace(); |
| 83 | +} catch(NetworkError ex) { |
| 84 | + System.err.println("Network Error in refund request."); |
| 85 | + ex.printStackTrace(); |
| 86 | +} |
| 87 | + |
| 88 | +System.out.println(String.format("Refund successful: %s", refunded)); |
| 89 | + |
| 90 | +// Alternatively you can create a refund without the purchase |
| 91 | +try { |
| 92 | + Refund r = Refund.create( |
| 93 | + 100, // Amount as integer |
| 94 | + "071-P-ABC123D5", // The transaction ID |
| 95 | + "my refund ref" // Optional |
| 96 | + ); |
| 97 | +} catch(IOException ex) { |
| 98 | + System.err.println("IOException in refund request."); |
| 99 | + ex.printStackTrace(); |
| 100 | +} catch(APIError ex) { |
| 101 | + System.err.println("API Error in refund request."); |
| 102 | + ex.printStackTrace(); |
| 103 | +} catch(NetworkError ex) { |
| 104 | + System.err.println("Network Error in refund request."); |
| 105 | + ex.printStackTrace(); |
| 106 | +} |
| 107 | + |
| 108 | +System.out.println(r.toString()); |
| 109 | + |
| 110 | +// Finally you can lookup transactions: |
| 111 | + |
| 112 | +try { |
| 113 | + Purchase p2 = Purchase.find("my_reference"); // You can use the merchant reference, or the Fat Zebra Transaction ID such as 071-P-XXMRSYH6 |
| 114 | + p2.refund(100); |
| 115 | +} catch(IOException ex) { |
| 116 | + System.err.println("IOException in purchase lookup or refund request."); |
| 117 | + ex.printStackTrace(); |
| 118 | +} catch(APIError ex) { |
| 119 | + System.err.println("API Error in purchase lookup or refund request."); |
| 120 | + ex.printStackTrace(); |
| 121 | +} catch(NetworkError ex) { |
| 122 | + System.err.println("Network Error in purchase lookup or refund request."); |
| 123 | + ex.printStackTrace(); |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +``` |
| 128 | + |
| 129 | + |
| 130 | +Authorisations and Capture |
| 131 | +-------------------------- |
| 132 | + |
| 133 | +It is possible to pass a flag to the `Purchase.create()` method to perform an auth/capture transaction. Once a transaction is authorised it can be captured up to 72 hours |
| 134 | +after the successful auth. |
| 135 | + |
| 136 | +```java |
| 137 | + |
| 138 | +// Setup - you only need to do this once upon application initialization |
| 139 | +FatZebra.username = "TEST"; |
| 140 | +FatZebra.token = "TEST"; |
| 141 | +FatZebra.sandbox = true; // Omit this for production |
| 142 | + |
| 143 | + |
| 144 | +int cardExpiryMonth = 7; |
| 145 | +int cardExpiryYear = 2023; |
| 146 | + |
| 147 | +HashMap<String,Object> card_data = new HashMap<String, Object>(); |
| 148 | +card_data.put("card_number", "5123456789012346"); |
| 149 | +card_data.put("card_holder", "James Smith"); |
| 150 | +card_data.put("cvv", "123"); |
| 151 | +card_data.put("card_expiry", String.format("%d/%d", cardExpiryMonth, cardExpiryYear)); |
| 152 | + |
| 153 | +try { |
| 154 | + Purchase p = Purchase.create( |
| 155 | + 100, // The amount, as an integer. i.e. $150.75 would be 15075 |
| 156 | + card_data, // A HashMap of the card data we defined above |
| 157 | + "my_reference", // Your order reference, such as an invoice number |
| 158 | + "1.2.3.4", // The customers IP address |
| 159 | + "AUD", // The currency code (such as AUD, USD, EUR, GBP etc) |
| 160 | + false // Indicates whether the transaction should be captured or not - default = true |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +// Capture the transaction: |
| 165 | +if (p.successful) { |
| 166 | + boolean captured = p.capture(10); // The capture amount can be less then or equal to the authorisation amount |
| 167 | + if (captured) { |
| 168 | + System.out.println(String.format("Transaction captured for %d", p.captured_total)); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +``` |
| 173 | + |
| 174 | + |
| 175 | +Exceptions |
| 176 | +---------- |
| 177 | + |
| 178 | +There are two exception classes defined: |
| 179 | + |
| 180 | + * NetworkError - raised when connectivity issues are presented, such as a timeout, resolve or connect error |
| 181 | + * API Error - raised when there is an error response from the API, such as validation issues, uniqueness colissions etc |
| 182 | + |
| 183 | + |
| 184 | +3rd Party Dependencies |
| 185 | +---------------------- |
| 186 | + |
| 187 | +This library depends on the following external dependencies: |
| 188 | + |
| 189 | + * Google Gson version 2.2.4 |
| 190 | + * Apache HTTP Commons commons-codec version 1.6 |
| 191 | + |
| 192 | +These dependencies have been included in the project and packaged within the included jar. |
0 commit comments