Skip to content

Commit

Permalink
Refactor: Payload to C2BPayload
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCollinsByte committed Nov 23, 2024
1 parent 8097b06 commit 298cc33
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ This configuration sets up the MpesaKeyProviderFromEnvironment to retrieve the A
The C2B API call is used as a standard customer-to-business transaction. Funds from the customer’s mobile money wallet will be deducted and be transferred to the mobile money wallet of the business. To authenticate and authorize this transaction, M-Pesa Payments Gateway will initiate a USSD Push message to the customer to gather and verify the mobile money PIN number. This number is not stored and is used only to authorize the transaction.

```java
Payload payload =
Payload customerToBusinessPayload =
new Payload.Builder()
.setAmount("10.00")
.setCustomerMSISDN("+255-762-578-467")
Expand All @@ -104,7 +104,7 @@ CustomerToBusinessTransaction customerToBusinessTransaction =
new CustomerToBusinessTransaction.Builder()
.setApiEndpoint(new ApiEndpoint(Environment.SANDBOX, Market.VODACOM_TANZANIA))
.setEncryptedSessionKey(session.getEncryptedSessionKey())
.setPayload(payload)
.setPayload(customerToBusinessPayload)
.build();
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.kwawingu.payments.ApiEndpoint;
import com.kwawingu.payments.Service;
import com.kwawingu.payments.client.MpesaHttpClient;
import com.kwawingu.payments.client.payload.Payload;
import com.kwawingu.payments.client.payload.CustomerToBusinessPayload;
import com.kwawingu.payments.client.response.CustomerToBusinessTransactionResponse;
import com.kwawingu.payments.session.keys.MpesaEncryptedSessionKey;
import java.io.IOException;
Expand All @@ -24,13 +24,13 @@ public class CustomerToBusinessTransaction {
private final MpesaHttpClient mpesaHttpClientClient;
private final ApiEndpoint apiEndpoint;
private final MpesaEncryptedSessionKey encryptedSessionKey;
private final Payload payload;
private final CustomerToBusinessPayload customerToBusinessPayload;

public CustomerToBusinessTransaction(
ApiEndpoint apiEndpoint, MpesaEncryptedSessionKey encryptedSessionKey, Payload payload) {
ApiEndpoint apiEndpoint, MpesaEncryptedSessionKey encryptedSessionKey, CustomerToBusinessPayload customerToBusinessPayload) {
this.apiEndpoint = apiEndpoint;
this.encryptedSessionKey = encryptedSessionKey;
this.payload = payload;
this.customerToBusinessPayload = customerToBusinessPayload;
mpesaHttpClientClient = new MpesaHttpClient();
}

Expand All @@ -44,7 +44,7 @@ public String synchronousPayment() throws IOException, InterruptedException {
CustomerToBusinessTransactionResponse.SynchronousResponses response =
mpesaHttpClientClient.customerToBusinessTransactionRequest(
headers,
HttpRequest.BodyPublishers.ofString(payload.toJsonString()),
HttpRequest.BodyPublishers.ofString(customerToBusinessPayload.toJsonString()),
apiEndpoint.getUrl(Service.CUSTOMER_TO_BUSINESS));
return response.toJson();
}
Expand All @@ -53,7 +53,7 @@ public String synchronousPayment() throws IOException, InterruptedException {
public static class Builder {
private ApiEndpoint apiEndpoint;
private MpesaEncryptedSessionKey encryptedSessionKey;
private Payload payload;
private CustomerToBusinessPayload customerToBusinessPayload;

public Builder setApiEndpoint(ApiEndpoint apiEndpoint) {
this.apiEndpoint = apiEndpoint;
Expand All @@ -65,17 +65,17 @@ public Builder setEncryptedSessionKey(MpesaEncryptedSessionKey encryptedSessionK
return this;
}

public Builder setPayload(Payload payload) {
this.payload = payload;
public Builder setPayload(CustomerToBusinessPayload customerToBusinessPayload) {
this.customerToBusinessPayload = customerToBusinessPayload;
return this;
}

public CustomerToBusinessTransaction build() {
Objects.requireNonNull(apiEndpoint, "API End-Point cannot be null");
Objects.requireNonNull(encryptedSessionKey, "An Encrypted Session Key cannot be null");
Objects.requireNonNull(payload, "Payload cannot be null");
Objects.requireNonNull(customerToBusinessPayload, "CustomerToBusinessPayload cannot be null");

return new CustomerToBusinessTransaction(apiEndpoint, encryptedSessionKey, payload);
return new CustomerToBusinessTransaction(apiEndpoint, encryptedSessionKey, customerToBusinessPayload);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.Objects;

public class Payload {
public class CustomerToBusinessPayload {
private final String amount;
private final String customerMSISDN;
private final String country;
Expand All @@ -15,7 +15,7 @@ public class Payload {
private final String thirdPartyConversationID;
private final String purchasedItemsDesc;

private Payload(Builder builder) {
private CustomerToBusinessPayload(Builder builder) {
this.amount = builder.amount;
this.customerMSISDN = builder.customerMSISDN;
this.country = builder.country;
Expand Down Expand Up @@ -77,7 +77,7 @@ public Builder setPurchasedItemsDesc(String purchasedItemsDesc) {
return this;
}

public Payload build() {
public CustomerToBusinessPayload build() {
Objects.requireNonNull(amount, "Transaction amount cannot be null");
Objects.requireNonNull(customerMSISDN, "Customer MSISDN cannot be null");
Objects.requireNonNull(country, "country cannot be null");
Expand All @@ -87,11 +87,11 @@ public Payload build() {
Objects.requireNonNull(
thirdPartyConversationID, "Third Party Conversation ID cannot be null");
Objects.requireNonNull(purchasedItemsDesc, "Purchased Item Description cannot be null");
return new Payload(this);
return new CustomerToBusinessPayload(this);
}
}

// Convert the Payload object to JSON String
// Convert the CustomerToBusinessPayload object to JSON String
public String toJsonString() {
return "{"
+ "\"input_Amount\": \""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.kwawingu.payments.ApiEndpoint;
import com.kwawingu.payments.Environment;
import com.kwawingu.payments.Market;
import com.kwawingu.payments.client.payload.Payload;
import com.kwawingu.payments.client.payload.CustomerToBusinessPayload;
import com.kwawingu.payments.exception.SessionKeyUnavailableException;
import com.kwawingu.payments.session.MpesaSession;
import com.kwawingu.payments.session.provider.MpesaKeyProviderFromEnvironment;
Expand Down Expand Up @@ -49,7 +49,7 @@ public void setUp()
Market.VODACOM_TANZANIA
);

Payload payload = new Payload.Builder()
CustomerToBusinessPayload customerToBusinessPayload = new CustomerToBusinessPayload.Builder()
.setAmount("10.00")
.setCustomerMSISDN("+255 762578467")
.setCountry(Market.VODACOM_TANZANIA.getInputCountryValue())
Expand All @@ -63,7 +63,7 @@ public void setUp()
transaction = new CustomerToBusinessTransaction.Builder()
.setApiEndpoint(new ApiEndpoint(Environment.SANDBOX, Market.VODACOM_TANZANIA))
.setEncryptedSessionKey(session.getEncryptedSessionKey())
.setPayload(payload)
.setPayload(customerToBusinessPayload)
.build();
}

Expand Down

0 comments on commit 298cc33

Please sign in to comment.