Skip to content

Commit

Permalink
PreAuth capture api integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
javidlulu committed Aug 20, 2020
1 parent 3840b8e commit 1624155
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 1 deletion.
72 changes: 72 additions & 0 deletions src/main/java/jp/ne/paypay/api/PaymentApi.java
Expand Up @@ -837,4 +837,76 @@ private com.squareup.okhttp.Call CreateAccountLinkQRCodeCall(Object body) throws
localVarHeaderParams, localVarFormParams, localVarAuthNames);
}

/**
* Create a payment authorization
* Create a payment authorization to block the money. **Timeout: 30s**
*
* @param body Payment
* @param agreeSimilarTransaction (Optional) If the parameter is set to \"true\", the payment duplication check will be bypassed. (optional)
* @return PaymentDetails
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
*/
public PaymentDetails createPaymentAuthorization(Payment body, String agreeSimilarTransaction) throws ApiException {
String message = validator.validate(body);
if (message != null) {
throw new IllegalArgumentException(message);
}
ApiResponse<PaymentDetails> resp = createPaymentAuthorizationWithHttpInfo(body, agreeSimilarTransaction);
return resp.getData();
}

/**
* Create a payment authorization
* Create a payment authorization to block the money. **Timeout: 30s**
*
* @param body Payment
* @param agreeSimilarTransaction (Optional) If the parameter is set to \&quot;true\&quot;, the payment duplication check will be bypassed. (optional)
* @return ApiResponse&lt;PaymentDetails&gt;
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
*/
protected ApiResponse<PaymentDetails> createPaymentAuthorizationWithHttpInfo(Object body, String agreeSimilarTransaction) throws ApiException {
com.squareup.okhttp.Call call = createPaymentAuthorizationCall(body, agreeSimilarTransaction);
Type localVarReturnType = new TypeToken<PaymentDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
}

/**
* Build call for createPaymentAuthorization
*
* @param body Payment
* @param agreeSimilarTransaction (Optional) If the parameter is set to \&quot;true\&quot;, the payment duplication check will be bypassed. (optional)
* @return Call to execute
* @throws ApiException If fail to serialize the request body object
*/
private com.squareup.okhttp.Call createPaymentAuthorizationCall(Object body, String agreeSimilarTransaction) throws ApiException {

// create path and map variables
String localVarPath = "/v2/payments/preauthorize";

List<Pair> localVarQueryParams = new ArrayList<>();
List<Pair> localVarCollectionQueryParams = new ArrayList<>();
if (agreeSimilarTransaction != null)
localVarQueryParams.addAll(apiClient.parameterToPair("agreeSimilarTransaction", agreeSimilarTransaction));

Map<String, String> localVarHeaderParams = new HashMap<>();

Map<String, Object> localVarFormParams = new HashMap<>();

final String[] localVarAccepts = {
APPLICATION_JSON
};
final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
if (localVarAccept != null) localVarHeaderParams.put(ACCEPT, localVarAccept);

final String[] localVarContentTypes = {

};
final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
localVarHeaderParams.put(CONTENT_TYPE, localVarContentType);
String[] localVarAuthNames = new String[]{HMAC_AUTH};
apiClient.setReadTimeout(30);
return apiClient.buildCall(localVarPath, "POST", localVarQueryParams, localVarCollectionQueryParams, body, localVarHeaderParams, localVarFormParams, localVarAuthNames);
}

}
66 changes: 65 additions & 1 deletion src/main/java/jp/ne/paypay/example/PaymentApiExample.java
Expand Up @@ -16,6 +16,7 @@
import jp.ne.paypay.model.NotDataResponse;
import jp.ne.paypay.model.Payment;
import jp.ne.paypay.model.PaymentDetails;
import jp.ne.paypay.model.PaymentState;
import jp.ne.paypay.model.PaymentStateRevert;
import jp.ne.paypay.model.QRCode;
import jp.ne.paypay.model.QRCodeDetails;
Expand Down Expand Up @@ -47,13 +48,44 @@ public static void main(String[] args) {
createAccountLinkQrCode(paymentApi);

String userAuthorizationId = "USER_AUTHORIZATION_ID";

preAuthCaptureFlow(walletApiInstance, paymentApi, userAuthorizationId);
directDebitFlow(walletApiInstance, paymentApi, userAuthorizationId);
appInvokeFlow(paymentApi, walletApiInstance, userAuthorizationId);


}

private static PaymentDetails createPaymentAuthorization(final PaymentApi apiInstance, String merchantPaymentId,
String userAuthorizationId, int amount) {
PaymentDetails result = null;
try {
Payment payment = new Payment();
payment.setAmount(new MoneyAmount().amount(amount).currency(MoneyAmount.CurrencyEnum.JPY));
payment.setMerchantPaymentId(merchantPaymentId);
payment.setUserAuthorizationId(userAuthorizationId);
payment.setRequestedAt(Instant.now().getEpochSecond());
payment.setStoreId(RandomStringUtils.randomAlphabetic(8));
payment.setTerminalId(RandomStringUtils.randomAlphanumeric(8));
payment.setOrderReceiptNumber(RandomStringUtils.randomAlphanumeric(8));
payment.setOrderDescription("Payment for Order ID:"+UUID.randomUUID().toString());
MerchantOrderItem merchantOrderItem =
new MerchantOrderItem()
.category("Dessert").name("Red Velvet Cake")
.productId(RandomStringUtils.randomAlphanumeric(8)).quantity(1)
.unitPrice(new MoneyAmount().amount(10).currency(MoneyAmount.CurrencyEnum.JPY));
List<MerchantOrderItem> merchantOrderItems = new ArrayList<>();
merchantOrderItems.add(merchantOrderItem);
payment.setOrderItems(new ArrayList<MerchantOrderItem>(merchantOrderItems));
result = apiInstance.createPaymentAuthorization(payment, "true");
System.out.println("\nAPI RESPONSE\n------------------\n");
System.out.println(result);
} catch (ApiException e) {
System.err.println(e.getResponseBody());
}
return result;
}


private static void createAccountLinkQrCode(final PaymentApi apiInstance){
try{
AccountLinkQRCode accountLinkQRCode = new AccountLinkQRCode();
Expand Down Expand Up @@ -136,6 +168,38 @@ private static void directDebitFlow(WalletApi walletApiInstance, PaymentApi paym
}
}

}

private static void preAuthCaptureFlow(WalletApi walletApiInstance, PaymentApi paymentApi, String userAuthorizationId){

String merchantPaymentId = UUID.randomUUID().toString();
System.out.println("Checking wallet balance...");
int amount =1; String currency = "JPY";
WalletBalance walletBalance = getWalletBalance(walletApiInstance, userAuthorizationId, amount, currency);
if(walletBalance != null && walletBalance.getData().isHasEnoughBalance()){
System.out.println("There is enough balance, now creating payment...");
PaymentDetails paymentDetails = createPaymentAuthorization(paymentApi, merchantPaymentId, userAuthorizationId, amount);
if (paymentDetails != null) {
System.out.println("Now capture the payment authorization for a payment, Don't capture if you need to check cancel payment");
capturePayment(paymentApi, merchantPaymentId, amount);
System.out.println("Payment created successfully, Now calling the API to get payment details for payment "
+ "ID:"+merchantPaymentId);
paymentDetails = getPaymentDetails(paymentApi, merchantPaymentId);
if(paymentDetails != null) {
if(paymentDetails.getData().getStatus() == PaymentState.StatusEnum.COMPLETED){
String refundId = UUID.randomUUID().toString();
System.out.println("Creating Refund for the payment:" + paymentDetails.getData().getPaymentId());
createRefund(paymentApi, paymentDetails.getData().getPaymentId(), refundId);
System.out.println("Get refund details:"+refundId);
getRefundDetails(paymentApi, refundId);
}else{
System.out.println("Finally cancel the payment");
cancelPayment(paymentApi, merchantPaymentId);
}
}
}
}

}
private static WalletBalance getWalletBalance(final WalletApi apiInstance, String userAuthorizationId, int amount,
String currency) {
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/jp/ne/paypay/api/PaymentApiTest.java
Expand Up @@ -131,6 +131,44 @@ public void createPaymentTest() throws ApiException {
PaymentDetails response = api.createPayment(payment, agreeSimilarTransaction);
Assertions.assertEquals(response.getResultInfo().getMessage(), "SUCCESS");
}

/**
* Create a payment Authorization
*
* Create a direct debit payment and start the money transfer. **Timeout: 30s**
*
* @throws ApiException
* if the Api call fails
*/
@Test
public void createPaymentAuthorizationTest() throws ApiException {

Payment payment = new Payment();
payment.setAmount(new MoneyAmount().amount(10).currency(MoneyAmount.CurrencyEnum.JPY));
payment.setMerchantPaymentId("merchantPaymentId");
payment.setUserAuthorizationId("userAuthorizationId");
payment.setRequestedAt(Instant.now().getEpochSecond());
payment.setStoreId(RandomStringUtils.randomAlphabetic(8));
payment.setTerminalId(RandomStringUtils.randomAlphanumeric(8));
payment.setOrderReceiptNumber(RandomStringUtils.randomAlphanumeric(8));
payment.setOrderDescription("Payment for Order ID:"+UUID.randomUUID().toString());
MerchantOrderItem merchantOrderItem =
new MerchantOrderItem()
.category("Dessert").name("Red Velvet Cake")
.productId(RandomStringUtils.randomAlphanumeric(8)).quantity(1)
.unitPrice(new MoneyAmount().amount(10).currency(MoneyAmount.CurrencyEnum.JPY));
List<MerchantOrderItem> merchantOrderItems = new ArrayList<>();
merchantOrderItems.add(merchantOrderItem);
payment.setOrderItems(new ArrayList<MerchantOrderItem>(merchantOrderItems));

PaymentDetails paymentDetails = new PaymentDetails();
paymentDetails.setResultInfo(resultInfo);
String agreeSimilarTransaction = "True";
ApiResponse<PaymentDetails> paymentDetailsApiResponse = new ApiResponse<>(00001, null, paymentDetails);
Mockito.when(api.createPaymentAuthorizationWithHttpInfo(payment, agreeSimilarTransaction)).thenReturn(paymentDetailsApiResponse);
PaymentDetails response = api.createPaymentAuthorization(payment, agreeSimilarTransaction);
Assertions.assertEquals(response.getResultInfo().getMessage(), "SUCCESS");
}

/**
* Create a Code
Expand Down

0 comments on commit 1624155

Please sign in to comment.