Skip to content

Latest commit

 

History

History
198 lines (177 loc) · 7.35 KB

README.md

File metadata and controls

198 lines (177 loc) · 7.35 KB
 ,-----. ,--.                                             ,--.                              
'  .--./ |  ,---.   ,--,--.  ,---.   ,--,--. ,-----.      |  |  ,--,--. ,--.  ,--.  ,--,--. 
|  |     |  .-.  | ' ,-.  | | .-. | ' ,-.  | '-----' ,--. |  | ' ,-.  |  \  `'  /  ' ,-.  | 
'  '--'\ |  | |  | \ '-'  | | '-' ' \ '-'  |         |  '-'  / \ '-'  |   \    /   \ '-'  | 
 `-----' `--' `--'  `--`--' |  |-'   `--`--'          `-----'   `--`--'    `--'     `--`--'

BUILD Maven Central License: MIT

Unofficial Java SDK for Chapa Payment Gateway.

What's new in this version

  • You no longer need to deal with JSON or Map<String, Object> responses. You can just treat response data as a Java object using specific response classes for each request type (e.g. payment initialization, payment verification).
  • Better exception handling. The SDK will throw the newly added ChapaException on failed requests to Chapa API.
  • Bug fixes and design improvements.
  • Well-tested and documented code. Check out the Javadoc here.

Table of Contents

  1. Documentation
  2. Installation
  3. Usage
  4. Javadoc
  5. Contribution
  6. Example
  7. License

Documentation

Visit official Chapa's API Documentation

Installation

Add the below maven dependency to your pom.xml file.

    <dependency>
      <groupId>io.github.yaphet17</groupId>
      <artifactId>Chapa</artifactId>
      <version>1.2.2</version>
    </dependency>

Or add the below gradle dependency to your build.gradle file.

    implementation 'io.github.yaphet17:Chapa:1.2.2'

Usage

Instantiate a Chapa class.

Chapa chapa = new Chapa("your-secrete-key");

Or if you want to use your own implementation of ChapaClient interface.

Chapa chapa = new Chapa("your-secrete-key", new MyCustomChapaClient());

Note: MyCustomChapaClient must implement ChapaClient interface.

To initialize a transaction, you can specify your information by either using our PostData class.

Note: Starting from version 1.1.0 you have to specify customization fields as a Map<String, String> object.

Customization customization = new Customization()
    .setTitle("E-commerce")
    .setDescription("It is time to pay")
    .setLogo("https://mylogo.com/log.png");
PostData postData = new PostData()
    .setAmount(new BigDecimal("100"))
    .setCurrency("ETB")
    .setFirstName("Abebe")
    .setLastName("Bikila")
    .setEmail("abebe@bikila")
    .setTxRef(Util.generateToken())
    .setCallbackUrl("https://chapa.co")
    .setReturnUrl("https://chapa.co")
    .setSubAccountId("testSubAccountId")
    .setCustomization(customization);

Or, you can use a string JSON data.

String formData = " { " +
        "'amount': '100', " +
        "'currency': 'ETB'," +
        "'email': 'abebe@bikila.com'," +
        "'first_name': 'Abebe'," +
        "'last_name': 'Bikila'," +
        "'tx_ref': 'tx-myecommerce12345'," +
        "'callback_url': 'https://chapa.co'," +
        "'subaccount[id]': 'ACCT_xxxxxxxxx'," +
        "'customizations':{" +
        "       'customization[title]':'E-commerce'," +
        "       'customization[description]':'It is time to pay'," +
        "       'customization[logo]':'https://mylogo.com/log.png'" +
        "   }" +
        " }";

Initialize payment

InitializeResponseData responseData = chapa.initialize(postData);
// Get the response message
System.out.println(responseData.getMessage());
// Get the checkout URL from the response JSON
System.out.println(responseData.getData().getCheckOutUrl());
// Get the raw response JSON
System.out.println(responseData.getRawJson());

Verify payment

// Get the verification response data
VerifyResponseData verifyResponseData = chapa.verify("tx-myecommerce12345");

Get the list of banks

List<Bank> banks = chapa.getBanks();

To create a subaccount, you can specify your information by either using our Subaccount class.

SubAccount subAccount = new SubAccount()
                .setBusinessName("Abebe Suq")
                .setAccountName("Abebe Bikila")
                .setAccountNumber("0123456789")
                .setBankCode("96e41186-29ba-4e30-b013-2ca36d7e7025")
                .setSplitType(SplitType.PERCENTAGE)
                .setSplitValue(0.2);

Or, you can use a string JSON data.

String subAccount = " { " +
        "'business_name': 'Abebe Suq', " +
        "'account_name': 'Abebe Bikila'," +
        "'account_number': '0123456789'," +
        "'bank_code': '96e41186-29ba-4e30-b013-2ca36d7e7025'," +
        "'split_type': 'percentage'," +
        "'split_value': '0.2'" +
        " }";

Create subaccount

SubAccountResponseData subAccountResponseData = chapa.createSubAccount(subAccount);
// Get SubAccount id from the response JSOn
System.out.println(subAccountResponseData.getData().getSubAccountId());

Example

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.github.yaphet17.chapa.Chapa;
import io.github.yaphet17.chapa.PostData;
import io.github.yaphet17.chapa.SubAccount;
import io.github.yaphet17.chapa.SplitType;
import io.github.yaphet17.chapa.Bank;

public class ChapaExample {

    public static void main(String[] args) {
      Chapa chapa = new Chapa("your-secrete-key");
    
      Customization customization = new Customization()
                .setTitle("E-commerce")
                .setDescription("It is time to pay")
                .setLogo("https://mylogo.com/log.png");

      PostData postData = new PostData()
                .setAmount(new BigDecimal("100"))
                .setCurrency("ETB")
                .setFirstName("Abebe")
                .setLastName("Bikila")
                .setEmail("abebe@bikila")
                .setTxRef(Util.generateToken())
                .setCallbackUrl("https://chapa.co")
                .setReturnUrl("https://chapa.co")
                .setSubAccountId("testSubAccountId")
                .setCustomization(customization);
      
      SubAccount subAccount = new SubAccount()
                .setBusinessName("Abebe Suq")
                .setAccountName("Abebe Bikila")
                .setAccountNumber("0123456789")
                .setBankCode("96e41186-29ba-4e30-b013-2ca36d7e7025")
                .setSplitType(SplitType.PERCENTAGE)
                .setSplitValue(0.2);

       
       InitializeResponseData responseData = chapa.initialize(postData);
       VerifyResponseData verifyResponseData = chapa.verify("tx-myecommerce12345");
       SubAccountResponseData subAccountResponseData = chapa.createSubAccount(subAccount);

      }
 }

Contribution

If you find any bugs or have any suggestions, please feel free to open an issue or pull request.

License

This open-source library is licensed under the terms of the MIT License.

Enjoy!