Skip to content

Commit

Permalink
Merge pull request #3 from yaphet17/dev
Browse files Browse the repository at this point in the history
Version 1.1.0 realese
  • Loading branch information
yaphet17 authored Oct 23, 2022
2 parents 37213a5 + 770a330 commit c839054
Show file tree
Hide file tree
Showing 13 changed files with 946 additions and 165 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
- name: Set up JDK 8
uses: actions/setup-java@v3
with:
java-version: '17'
java-version: '8'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
# - name: Lint Code Base
# uses: github/super-linter@v4
# env:
# VALIDATE_ALL_CODEBASE: false
# DEFAULT_BRANCH: main
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/upload-artifact@v2
if: failure()
with:
name: build-reports
path: |
build/reports
target
187 changes: 160 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,70 @@
# Chapa-Java
```
,-----. ,--. ,--.
' .--./ | ,---. ,--,--. ,---. ,--,--. ,-----. | | ,--,--. ,--. ,--. ,--,--.
| | | .-. | ' ,-. | | .-. | ' ,-. | '-----' ,--. | | ' ,-. | \ `' / ' ,-. |
' '--'\ | | | | \ '-' | | '-' ' \ '-' | | '-' / \ '-' | \ / \ '-' |
`-----' `--' `--' `--`--' | |-' `--`--' `-----' `--`--' `--' `--`--'
```

[![BUILD](https://github.com/yaphet17/chapa-java/actions/workflows/maven.yml/badge.svg)](https://github.com/yaphet17/chapa-java/actions/workflows/maven.yml/) [![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/yaphet17/chapa-java.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/yaphet17/chapa-java/context:java) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Unofficial Java package for Chapa Payment Gateway.

## What's new in this version
- You can now implement `ChapaClient` interface and create your own custom implementation
to use your favorite HTTP client.
- Includes split payment feature added by Chapa. You can now get list of supported banks, create
subaccount and perform a split payment. See [Split Payment](https://developer.chapa.co/docs/split-payment/) documentation for more details.
- Additional utility methods to help you to generate a convenient token for your transactions, to map json string
to `PostData` object etc.
- Bug fixes and improvements.
- Well tested and documented code.

## Table of Contents
1. [Installation](#installation)
2. [Documentation](#documentation)
3. [Usage](#usage)
4. [Contribution](#contribution)
5. [Example](#example)
6. [License](#license)

## Documentation
Visit official [Chapa's API Documentation](https://developer.chapa.co/docs)
## Installation
Add the below maven dependency to your `pom.xml` file.
```xml
```xml
<dependency>
<groupId>io.github.yaphet17</groupId>
<artifactId>chapa</artifactId>
<version>1.0.0</version>
<artifactId>Chapa</artifactId>
<version>1.1.0</version>
</dependency>
```
Or add the below gradle dependency to your `build.gradle` file.
```groovy
implementation 'io.github.yaphet17:Chapa:1.1.0'
```

## Usage

Instantiate a `Chapa` class.
```java
Chapa chapa = new Chapa("you-secrete-key");
Chapa chapa = new Chapa("your-secrete-key");
```
To initialize transaction, you can specify your information by either using our `PostData` class
Or if you want to use your own implementation of `ChapaClient` interface.
```java
Chapa chapa = new Chapa("your-secrete-key", new MyCustomChapaClient());
```
Note: `MyCustomChapaClient` must implement `ChapaClient` interface.

To initialize 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.

```java
Map<String, String> customizations = new HashMap<>();
customizations.put("customization[title]", "E-commerce");
customizations.put("customization[description]", "It is time to pay");
customizations.put("customization[logo]", "https://mylogo.com/log.png");
PostData formData = PostData.builder()
.amount(new BigDecimal("100"))
.currency( "ETB")
Expand All @@ -31,33 +73,124 @@ PostData formData = PostData.builder()
.email("[email protected]")
.tx_ref("tx-myecommerce12345")
.callback_url("https://chapa.co")
.customization_title("I love e-commerce")
.customization_description("It is time to pay")
.customization_logo("My logo")
.customization(customizations)
.build();
```
Or, you can use a string JSON data
```java
String formData = " {
'amount': '100',
'currency': 'ETB',
'email': '[email protected]',
'first_name': 'Abebe',
'last_name': 'Bikila',
'tx_ref': 'tx-myecommerce12345',
'callback_url': 'https://chapa.co',
'customization[title]': 'I love e-commerce',
'customization[description]': 'It is time to pay'
}"
}
Or, you can use a string JSON data.
```java
String formData = " { " +
"'amount': '100', " +
"'currency': 'ETB'," +
"'email': '[email protected]'," +
"'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'" +
" }" +
" }";
```
Intitialize payment
```java
String reponseString = chapa.initialize(formData).asString(); // get reponse in a string JSON format
Map<String, String> responseMap = chapa.initialize(formData).asMap(); // get reponse as a Map object
String reponseString = chapa.initialize(formData).asString(); // get response in a string JSON format
Map<String, String> responseMap = chapa.initialize(formData).asMap(); // get response as a Map object
```
Verify payment
```java
String reponseString = chapa.verify("tx-myecommerce12345").asString(); // get reponse in a string JSON format
Map<String, String> responseMap = chapa.verify("tx-myecommerce12345").asMap(); // get reponse as a Map object
String reponseString = chapa.verify("tx-myecommerce12345").asString(); // get response in a string JSON format
Map<String, String> responseMap = chapa.verify("tx-myecommerce12345").asMap(); // get response as a Map object
```
Get list of banks
```java
List<Bank> banks = chapa.getBanks();
```
To create a subaccount, you can specify your information by either using our `Subaccount` class.
```java
SubAccount subAccount = SubAccount.builder()
.businessName("Abebe Suq")
.accountName("Abebe Bikila")
.accountNumber("0123456789")
.bankCode("96e41186-29ba-4e30-b013-2ca36d7e7025")
.splitType(SplitType.PERCENTAGE) // or SplitType.FLAT
.splitValue(0.2)
.build();
```
Or, you can use a string JSON data.
```java
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
```java
String reponseString = chapa.createSubAccount(subAccount).asString(); // get response in a string JSON format
Map<String, String> responseMap = chapa.createSubAccount(subAccount).asMap(); // get response as a Map object
```
## Example
```java
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");

Map<String, String> customizations = new HashMap<>();
customizations.put("customization[title]", "E-commerce");
customizations.put("customization[description]", "It is time to pay");
customizations.put("customization[logo]", "https://mylogo.com/log.png");
PostData postData = PostData.builder()
.amount(new BigDecimal("100"))
.currency("ETB")
.firstName("Abebe")
.lastName("Bikila")
.email("[email protected]")
.txRef(Util.generateToken())
.callbackUrl("https://chapa.co")
.subAccountId("ACCT_xxxxxxxxx")
.customizations(customizations)
.build();

SubAccount subAccount = SubAccount.builder()
.businessName("Abebe Suq")
.accountName("Abebe Bikila")
.accountNumber("0123456789")
.bankCode("96e41186-29ba-4e30-b013-2ca36d7e7025")
.splitType(SplitType.PERCENTAGE) // or SplitType.FLAT
.splitValue(0.2)
.build();

// list of banks
List<Bank> banks = chapa.banks();
banks.forEach(bank -> System.out.println("Bank name: " + bank.getName() + " Bank Code: " + bank.getId()));
// create subaccount
System.out.println("Create SubAccount response: " + chapa.createSubAccount(subAccount).asString());
// initialize payment
System.out.println("Initialize response: " + chapa.initialize(postData).asString());
// verify payment
System.out.println("Verify response: " + chapa.verify(postData.getTxRef()).asString());
```
## Contribution
If you find any bug or have any suggestion, 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!
31 changes: 25 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

<groupId>io.github.yaphet17</groupId>
<artifactId>Chapa</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>Chapa</name>
<description>Publish open source library for Chapa payment gateway with OSSRH</description>
<description>Java package for Chapa Payment Gateway</description>
<url>https://github.com/yaphet17/chapa-java</url>
<developers>
<developer>
Expand Down Expand Up @@ -70,9 +70,28 @@
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>2.2.4</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down Expand Up @@ -161,7 +180,7 @@
</execution>
</executions>
<configuration>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
<source>8</source>
</configuration>
</plugin>
<plugin>
Expand Down
Loading

0 comments on commit c839054

Please sign in to comment.