Skip to content

Commit

Permalink
Merge a451877 into 245ef62
Browse files Browse the repository at this point in the history
  • Loading branch information
javidlulu committed Oct 27, 2020
2 parents 245ef62 + a451877 commit 9fb9854
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 40 deletions.
29 changes: 23 additions & 6 deletions src/main/java/jp/ne/paypay/ApiClient.java
@@ -1,5 +1,6 @@
package jp.ne.paypay;

import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.MediaType;
Expand All @@ -8,8 +9,10 @@
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.http.HttpMethod;
import jp.ne.paypay.api.ApiUtil;
import jp.ne.paypay.auth.Authentication;
import jp.ne.paypay.auth.HmacAuth;
import jp.ne.paypay.model.NotDataResponse;
import okio.BufferedSink;
import okio.Okio;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -611,15 +614,15 @@ public File prepareDownloadFile(Response response) throws IOException {
}

/**
* {@link #execute(Call, Type)}
* {@link #execute(Call, Type, String)}
*
* @param <T> Type
* @param call An instance of the Call object
* @throws ApiException If fail to execute the call
* @return ApiResponse object
*/
public <T> ApiResponse<T> execute(Call call) throws ApiException {
return execute(call, null);
return execute(call, null, null);
}

/**
Expand All @@ -628,15 +631,16 @@ public <T> ApiResponse<T> execute(Call call) throws ApiException {
* @param returnType The return type used to deserialize HTTP response body
* @param <T> The return type corresponding to (same with) returnType
* @param call Call
* @param apiName The API Name for resolve url
* @return ApiResponse object containing response status, headers and
* data, which is a Java object deserialized from response body and would be null
* when returnType is null.
* @throws ApiException If fail to execute the call
*/
public <T> ApiResponse<T> execute(Call call, Type returnType) throws ApiException {
public <T> ApiResponse<T> execute(Call call, Type returnType, String apiName) throws ApiException {
try {
Response response = call.execute();
T data = handleResponse(response, returnType);
T data = handleResponse(response, returnType, apiName);
return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
} catch (IOException e) {
throw new ApiException(e);
Expand All @@ -648,12 +652,13 @@ public <T> ApiResponse<T> execute(Call call, Type returnType) throws ApiExceptio
*
* @param <T> Type
* @param response Response
* @param apiName The API Name for resolve url
* @param returnType Return type
* @throws ApiException If the response has a unsuccessful status code or
* fail to deserialize the response body
* @return Type
*/
public <T> T handleResponse(Response response, Type returnType) throws ApiException {
public <T> T handleResponse(Response response, Type returnType, String apiName) throws ApiException {
if (response.isSuccessful()) {
if (returnType == null || response.code() == 204) {
// returning null if the returnType is not defined,
Expand All @@ -678,10 +683,22 @@ public <T> T handleResponse(Response response, Type returnType) throws ApiExcept
throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap());
}
}
throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody);
String resolveUrl = getResolveUrl(apiName, respBody);
throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody).setResolveUrl(resolveUrl);
}
}

private String getResolveUrl(String apiName, String respBody) {
NotDataResponse responseBody = json.deserialize(respBody, new TypeToken<NotDataResponse>() {
}.getType());
String resolveUrl = null;
if(StringUtils.isNotEmpty(apiName) && responseBody.getResultInfo() != null){
resolveUrl = ApiUtil.buildResolveUrl(apiName, responseBody.getResultInfo().getCode(), responseBody.getResultInfo().getCodeId());
System.out.println("This link helps you to troubleshoot the issue: "+resolveUrl);
}
return resolveUrl;
}

/**
* Build HTTP call with the given options.
*
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/jp/ne/paypay/ApiException.java
Expand Up @@ -7,6 +7,7 @@ public class ApiException extends Exception {
private int code = 0;
private Map<String, List<String>> responseHeaders = null;
private String responseBody = null;
private String resolveUrl;

public ApiException() {}

Expand Down Expand Up @@ -74,4 +75,23 @@ public Map<String, List<String>> getResponseHeaders() {
public String getResponseBody() {
return responseBody;
}

/**
* Get the Resolve Url.
*
* @return Resolve Url in the form of string
*/
public String getResolveUrl() {
return resolveUrl;
}

/**
* Set the Resolve Url.
*
* @return ApiException with resolve url set
*/
public ApiException setResolveUrl(String resolveUrl) {
this.resolveUrl = resolveUrl;
return this;
}
}
3 changes: 2 additions & 1 deletion src/main/java/jp/ne/paypay/api/ApiConstants.java
Expand Up @@ -9,5 +9,6 @@ public class ApiConstants {
public static final String CODE_ID = "codeId";
public static final String HMAC_AUTH = "HmacAuth";
public static final String CONTENT_TYPE = "Content-Type";
public static final String USER_AUTHORIZATION_ID = "userAuthorizationId";
public static final String USER_AUTHORIZATION_ID = "userAuthorizationId";
public static final String RESOLVE_BASE_URL = "https://developer.paypay.ne.jp/develop/resolve";
}
26 changes: 26 additions & 0 deletions src/main/java/jp/ne/paypay/api/ApiNameConstants.java
@@ -0,0 +1,26 @@
package jp.ne.paypay.api;

public class ApiNameConstants {
//API NAMES FOR RESOLVE URL
public static final String CANCEL_PAYMENT = "v2_cancelPayment";
public static final String CAPTURE_PAYMENT = "v2_captureAuthorizedOrder";
public static final String CREATE_PAYMENT = "v2_createPayment";
public static final String CREATE_QRCODE = "v2_createDynamicQRCode";
public static final String DELETE_QRCODE = "v2_deleteDynamicQRCode";
public static final String GET_PAYMENT = "v2_getPaymentDetail";
public static final String GET_QR_PAYMENT = "v2_getQRPaymentDetails";
public static final String GET_REFUND = "v2_getRefundDetails";
public static final String REFUND_PAYMENT = "v2_createRefundPayment";
public static final String REVERT_AUTHORIZE = "v2_revertAuthorizedOrder";
public static final String PREAUTHORIZE_PAYMENT = "v2_createOrderAndAuthorize";
public static final String CREATE_CONTINUOUS_PAYMENT = "v1_createSubscriptionPayment";
public static final String CREATE_REQUEST_ORDER = "v1_createRequestOrder";
public static final String GET_REQUEST_ORDER = "v1_getRequestOrder";
public static final String CANCEL_REQUEST_ORDER = "v1_cancelRequestOrder";
public static final String REFUND_REQUEST_ORDER = "v1_createRequestOrderRefund";
public static final String GET_SECURE_USER_PROFILE = "v2_getSecureUserProfile";
public static final String CHECK_BALANCE = "v2_checkWalletBalance";
public static final String GET_USER_AUTH_STATUS = "v2_userAuthStatus";
public static final String UNLINK_USER = "v2_unlinkUser";
public static final String CREATE_QR_SESSION = "v1_qrSession";
}
5 changes: 5 additions & 0 deletions src/main/java/jp/ne/paypay/api/ApiUtil.java
Expand Up @@ -69,4 +69,9 @@ public static Call postCallObject(ApiClient apiClient, String url, Object body,
return apiClient.buildCall(url, "POST", localVarQueryParams, localVarCollectionQueryParams,
body, localVarHeaderParams, localVarFormParams, localVarAuthNames);
}

public static String buildResolveUrl(String apiName, String code, String codeId){
String resolveUrl = ApiConstants.RESOLVE_BASE_URL+"?api-name=%s&code=%s&code-id=%s";
return String.format(resolveUrl, apiName, code, codeId);
}
}
26 changes: 13 additions & 13 deletions src/main/java/jp/ne/paypay/api/PaymentApi.java
Expand Up @@ -74,7 +74,7 @@ protected ApiResponse<NotDataResponse> cancelPaymentWithHttpInfo(String merchant
Call call = cancelPaymentValidateBeforeCall(merchantPaymentId);
Type localVarReturnType = new TypeToken<NotDataResponse>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.CANCEL_PAYMENT);
}

/**
Expand Down Expand Up @@ -105,7 +105,7 @@ protected ApiResponse<PaymentDetails> capturePaymentAuthWithHttpInfo(Object body
Call call = capturePaymentAuthCall(body);
Type localVarReturnType = new TypeToken<PaymentDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.CAPTURE_PAYMENT);
}

private Call capturePaymentAuthCall(Object body) throws ApiException {
Expand Down Expand Up @@ -147,7 +147,7 @@ protected ApiResponse<PaymentDetails> createPaymentWithHttpInfo(Object body, Str
Call call = createPaymentValidateBeforeCall(body, agreeSimilarTransaction);
Type localVarReturnType = new TypeToken<PaymentDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.CREATE_PAYMENT);
}

/**
Expand Down Expand Up @@ -194,7 +194,7 @@ protected ApiResponse<QRCodeDetails> createQRCodeWithHttpInfo(Object body) throw
Call call = createQRCodeValidateBeforeCall(body);
Type localVarReturnType = new TypeToken<QRCodeDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.CREATE_QRCODE);
}

private Call deleteQRCodeValidateBeforeCall(String codeId) throws ApiException {
Expand Down Expand Up @@ -227,7 +227,7 @@ protected ApiResponse<NotDataResponse> deleteQRCodeWithHttpInfo(String codeId) t
Call call = deleteQRCodeValidateBeforeCall(codeId);
Type localVarReturnType = new TypeToken<NotDataResponse>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.DELETE_QRCODE);
}

private Call getPaymentDetailsValidateBeforeCall(String merchantPaymentId) throws ApiException {
Expand Down Expand Up @@ -260,7 +260,7 @@ protected ApiResponse<PaymentDetails> getPaymentDetailsWithHttpInfo(String merch
Call call = getPaymentDetailsValidateBeforeCall(merchantPaymentId);
Type localVarReturnType = new TypeToken<PaymentDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.GET_PAYMENT);
}


Expand Down Expand Up @@ -295,7 +295,7 @@ protected ApiResponse<PaymentDetails> getCodesPaymentDetailsWithHttpInfo(String
Call call = getCodesPaymentDetailsValidateBeforeCall(merchantPaymentId);
Type localVarReturnType = new TypeToken<PaymentDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.GET_QR_PAYMENT);
}


Expand Down Expand Up @@ -330,7 +330,7 @@ protected ApiResponse<RefundDetails> getRefundDetailsWithHttpInfo(String merchan
Call call = getRefundDetailsValidateBeforeCall(merchantRefundId);
Type localVarReturnType = new TypeToken<RefundDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.GET_REFUND);
}

private Call refundPaymentValidateBeforeCall(Object body) throws ApiException {
Expand Down Expand Up @@ -363,7 +363,7 @@ protected ApiResponse<RefundDetails> refundPaymentWithHttpInfo(Object body) thro
Call call = refundPaymentValidateBeforeCall(body);
Type localVarReturnType = new TypeToken<RefundDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.REFUND_PAYMENT);
}

private Call revertAuthValidateBeforeCall(Object body) throws ApiException {
Expand Down Expand Up @@ -395,7 +395,7 @@ protected ApiResponse<RevertAuthResponse> revertAuthWithHttpInfo(Object body) th
Call call = revertAuthValidateBeforeCall(body);
Type localVarReturnType = new TypeToken<RevertAuthResponse>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.REVERT_AUTHORIZE);
}

/**
Expand Down Expand Up @@ -424,7 +424,7 @@ protected ApiResponse<LinkQRCodeResponse> createAccountLinkQRCodeWithHttpInfo(Ob
Call call = createAccountLinkQrCodeCall(body);
Type localVarReturnType = new TypeToken<LinkQRCodeResponse>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.CREATE_QR_SESSION);
}

private Call createAccountLinkQrCodeCall(Object body) throws ApiException {
Expand Down Expand Up @@ -460,7 +460,7 @@ protected ApiResponse<PaymentDetails> createPaymentAuthorizationWithHttpInfo(Obj
Call call = ApiUtil.postCallObject(apiClient, "/v2/payments/preauthorize", body, agreeSimilarTransaction);
Type localVarReturnType = new TypeToken<PaymentDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.PREAUTHORIZE_PAYMENT);
}


Expand Down Expand Up @@ -490,7 +490,7 @@ protected ApiResponse<PaymentDetails> createContinuousPaymentWithHttpInfo(Object
Call call = createContinuousPaymentCall(body);
Type localVarReturnType = new TypeToken<PaymentDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.CREATE_CONTINUOUS_PAYMENT);
}

private Call createContinuousPaymentCall(Object body) throws ApiException {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/jp/ne/paypay/api/PendingPaymentApi.java
Expand Up @@ -69,7 +69,7 @@ protected ApiResponse<PaymentDetails> createPendingPaymentWithHttpInfo(Payment p
Call call = createPendingPaymentCall(payment);
Type localVarReturnType = new TypeToken<PaymentDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.CREATE_REQUEST_ORDER);
}

private Call createPendingPaymentCall(Payment payment) throws ApiException {
Expand Down Expand Up @@ -101,7 +101,7 @@ protected ApiResponse<PaymentDetails> getPendingPaymentDetailsWithHttpInfo(Strin
Call call = getPaymentDetailsValidateBeforeCall(merchantPaymentId);
Type localVarReturnType = new TypeToken<PaymentDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.GET_REQUEST_ORDER);
}

private Call getPaymentDetailsValidateBeforeCall(String merchantPaymentId) throws ApiException {
Expand Down Expand Up @@ -134,7 +134,7 @@ protected ApiResponse<NotDataResponse> cancelPendingOrderWithHttpInfo(String mer
Call call = cancelPendingOrderValidateBeforeCall(merchantPaymentId);
Type localVarReturnType = new TypeToken<NotDataResponse>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.CANCEL_REQUEST_ORDER);
}

private Call cancelPendingOrderValidateBeforeCall(String merchantPaymentId) throws ApiException {
Expand Down Expand Up @@ -166,7 +166,7 @@ protected ApiResponse<RefundDetails> refundPaymentWithHttpInfo(Refund refund) th
Call call = refundPaymentCall(refund);
Type localVarReturnType = new TypeToken<RefundDetails>() {
}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.REFUND_REQUEST_ORDER);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/jp/ne/paypay/api/UserApi.java
Expand Up @@ -63,7 +63,7 @@ public MaskedUserProfileResponse getMaskedUserProfile(String userAuthorizationId
protected ApiResponse<MaskedUserProfileResponse> getMaskedUserProfileWithHttpInfo(String userAuthorizationId) throws ApiException {
Call call = getMaskedUserProfileValidateBeforeCall(userAuthorizationId);
Type localVarReturnType = new TypeToken<MaskedUserProfileResponse>(){}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.GET_SECURE_USER_PROFILE);
}


Expand Down Expand Up @@ -96,7 +96,7 @@ public UserAuthorizationStatus getUserAuthorizationStatus(String userAuthorizati
protected ApiResponse<UserAuthorizationStatus> getUserAuthorizationStatusWithHttpInfo(String userAuthorizationId) throws ApiException {
Call call = getUserAuthorizationStatusValidateBeforeCall(userAuthorizationId);
Type localVarReturnType = new TypeToken<UserAuthorizationStatus>(){}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.GET_USER_AUTH_STATUS);
}


Expand Down Expand Up @@ -129,6 +129,6 @@ public NotDataResponse unlinkUser(String userAuthorizationId) throws ApiExceptio
protected ApiResponse<NotDataResponse> unlinkUserWithHttpInfo(String userAuthorizationId) throws ApiException {
Call call = unlinkUserValidateBeforeCall(userAuthorizationId);
Type localVarReturnType = new TypeToken<NotDataResponse>(){}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.UNLINK_USER);
}
}
2 changes: 1 addition & 1 deletion src/main/java/jp/ne/paypay/api/WalletApi.java
Expand Up @@ -139,6 +139,6 @@ protected ApiResponse<WalletBalance> checkWalletBalanceWithHttpInfo(String userA
String currency, ProductType productType) throws ApiException {
Call call = checkWalletBalanceValidateBeforeCall(userAuthorizationId, amount, currency, productType);
Type localVarReturnType = new TypeToken<WalletBalance>(){}.getType();
return apiClient.execute(call, localVarReturnType);
return apiClient.execute(call, localVarReturnType, ApiNameConstants.CHECK_BALANCE);
}
}

0 comments on commit 9fb9854

Please sign in to comment.