diff --git a/src/main/java/jp/ne/paypay/auth/HmacAuth.java b/src/main/java/jp/ne/paypay/auth/HmacAuth.java index 81a8060..1bbc098 100644 --- a/src/main/java/jp/ne/paypay/auth/HmacAuth.java +++ b/src/main/java/jp/ne/paypay/auth/HmacAuth.java @@ -69,10 +69,20 @@ private byte[] getHmacData(String nonce, long epoch, String hash){ localContentType = this.contentType; } String DELIMITER = "\n"; - return (this.requestUrl + DELIMITER + this.httpMethod + DELIMITER + nonce + DELIMITER + epoch + DELIMITER + return (formatRequestUrl(this.requestUrl) + DELIMITER + this.httpMethod + DELIMITER + nonce + DELIMITER + epoch + DELIMITER + localContentType + DELIMITER + hash) .getBytes(StandardCharsets.UTF_8); } + private String formatRequestUrl(String requestUrl){ + try{ + if(requestUrl.contains("?")){ + return requestUrl.substring(0, requestUrl.indexOf("?")); + } + }catch (Exception e){ + System.out.println("Exception while formatting request url for HMAC Auth"); + } + return requestUrl; + } @Override public void applyToParams(List queryParams, Map headerParams) { if (apiKey == null || apiSecretKey == null) { @@ -84,7 +94,6 @@ public void applyToParams(List queryParams, Map headerPara }catch (Exception e){ System.err.println("Error in getting Authorization: "+e); } - } public String getApiKey() { diff --git a/src/main/java/jp/ne/paypay/example/UserApiExample.java b/src/main/java/jp/ne/paypay/example/UserApiExample.java new file mode 100644 index 0000000..ed992a8 --- /dev/null +++ b/src/main/java/jp/ne/paypay/example/UserApiExample.java @@ -0,0 +1,42 @@ +package jp.ne.paypay.example; + +import jp.ne.paypay.ApiClient; +import jp.ne.paypay.ApiException; +import jp.ne.paypay.Configuration; +import jp.ne.paypay.api.UserApi; +import jp.ne.paypay.model.NotDataResponse; +import jp.ne.paypay.model.UserAuthorizationStatus; + +public class UserApiExample { + + + public static void main(String[] args) throws ApiException { + ApiClient apiClient = new Configuration().getDefaultApiClient(); + apiClient.setProductionMode(false); + apiClient.setApiKey("API_KEY"); + apiClient.setApiSecretKey("API_SECRET_KEY"); + apiClient.setAssumeMerchant("ASSUME_MERCHANT_ID"); + String userAuthorizationId = "USER_AUTHORIZATION_ID"; + + UserApi userApi = new UserApi(apiClient); + + getOrUnlinkUser(userAuthorizationId, userApi, false); + //Replace the "userAuthorizationId" with the actual one. Please note that this will Unlink the user from the client + getOrUnlinkUser("userAuthorizationId", userApi, true); + } + + private static void getOrUnlinkUser(String userAuthorizationId, UserApi userApi, boolean unlinkUser) { + try{ + if(unlinkUser){ + NotDataResponse notDataResponse = userApi.unlinkUser(userAuthorizationId); + System.out.println(notDataResponse); + }else{ + UserAuthorizationStatus userAuthorizationStatus = userApi.getUserAuthorizationStatus(userAuthorizationId); + System.out.println(userAuthorizationStatus); + } + }catch (ApiException e){ + System.out.println(e.getResponseBody()); + } + } +} + diff --git a/src/test/java/jp/ne/paypay/api/HmacAuthTest.java b/src/test/java/jp/ne/paypay/api/HmacAuthTest.java index 2efde7b..a19828a 100644 --- a/src/test/java/jp/ne/paypay/api/HmacAuthTest.java +++ b/src/test/java/jp/ne/paypay/api/HmacAuthTest.java @@ -35,10 +35,15 @@ public void hmacAuthTest(){ Assert.assertNotNull(hmacAuth.getHttpMethod()); Assert.assertNotNull(hmacAuth.getRequestBody()); Assert.assertNotNull(hmacAuth.getRequestUrl()); + hmacAuth.setRequestUrl("/v2/api/test?param=p1"); + hmacAuth.applyToParams(queryParams, headerParams); + Assert.assertTrue(headerParams.get("Authorization").startsWith("hmac")); + hmacAuth.setRequestUrl(null); + hmacAuth.applyToParams(queryParams, headerParams); + Assert.assertTrue(headerParams.get("Authorization").startsWith("hmac")); hmacAuth.setContentType(null); headerParams = new HashMap<>(); hmacAuth.applyToParams(queryParams, headerParams); Assert.assertNull(headerParams.get("Authorization")); - } }