Skip to content

Commit

Permalink
Merge pull request #256 from mwarzybok-sumoheavy/feature/SP-579
Browse files Browse the repository at this point in the history
SP-579 Java - Implement better refund exceptions
  • Loading branch information
bobbrodie authored Nov 26, 2023
2 parents 3688226 + bf40111 commit 296d86c
Show file tree
Hide file tree
Showing 135 changed files with 1,717 additions and 5,735 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.bitpay</groupId>
<artifactId>bitpay_sdk</artifactId>
<version>9.0.2</version>
<version>10.0.0</version>
<packaging>jar</packaging>

<name>BitPay</name>
Expand Down
594 changes: 263 additions & 331 deletions src/main/java/com/bitpay/sdk/Client.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/java/com/bitpay/sdk/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Config {
/**
* BitPay Plugin Info Version.
*/
public static final String BITPAY_PLUGIN_INFO = "BitPay_Java_Client_v9.0.2";
public static final String BITPAY_PLUGIN_INFO = "BitPay_Java_Client_v10.0.0";
/**
* BitPay Api Frame.
*/
Expand Down
56 changes: 28 additions & 28 deletions src/main/java/com/bitpay/sdk/client/AuthorizationClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

package com.bitpay.sdk.client;

import com.bitpay.sdk.exceptions.BitPayException;
import com.bitpay.sdk.exceptions.BitPayApiException;
import com.bitpay.sdk.exceptions.BitPayExceptionProvider;
import com.bitpay.sdk.exceptions.BitPayGenericException;
import com.bitpay.sdk.model.Facade;
import com.bitpay.sdk.model.Token;
import com.bitpay.sdk.util.GuidGenerator;
Expand All @@ -16,7 +18,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.apache.http.HttpResponse;

/**
* The type Authorization client.
Expand Down Expand Up @@ -52,36 +53,34 @@ public AuthorizationClient(
* Authorize (pair) this client with the server using the specified pairing code.
*
* @param pairingCode A code obtained from the server; typically from bitpay.com/api-tokens.
* @throws BitPayException BitPayException class
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
public void authorizeClient(String pairingCode) throws BitPayException {
public void authorizeClient(String pairingCode) throws BitPayApiException, BitPayGenericException {
Token token = new Token();
token.setId(this.identity);
token.setGuid(this.guidGenerator.execute());
token.setPairingCode(pairingCode);

JsonMapper mapper = JsonMapperFactory.create();

String json;
String json = null;

try {
json = mapper.writeValueAsString(token);
} catch (JsonProcessingException e) {
throw new BitPayException(null, "failed to serialize Token object : " + e.getMessage());
BitPayExceptionProvider.throwGenericExceptionWithMessage(
"Failed to serialize Token object : " + e.getMessage());
}

HttpResponse response = this.bitPayClient.post("tokens", json);
String jsonResponse = this.bitPayClient.post("tokens", json);

List<Token> tokens;
List<Token> tokens = null;

try {
tokens = Arrays.asList(mapper.readValue(this.bitPayClient.responseToJsonString(response), Token[].class));
tokens = Arrays.asList(mapper.readValue(jsonResponse, Token[].class));
} catch (JsonProcessingException e) {
throw new BitPayException(null,
"failed to deserialize BitPay server response (Tokens) : " + e.getMessage());
} catch (Exception e) {
throw new BitPayException(null,
"failed to deserialize BitPay server response (Tokens) : " + e.getMessage());
BitPayExceptionProvider.throwDeserializeResourceException("Tokens", e.getMessage());
}

for (Token t : tokens) {
Expand All @@ -94,12 +93,14 @@ public void authorizeClient(String pairingCode) throws BitPayException {
*
* @param facade Defines the level of API access being requested
* @return A pairing code for claim at https://bitpay.com/dashboard/merchant/api-tokens.
* @throws BitPayException BitPayException class
* @throws BitPayGenericException BitPayGenericException class
* @throws BitPayApiException BitPayApiException class
*/
public String authorizeClient(Facade facade) throws BitPayException {
public String authorizeClient(Facade facade) throws BitPayApiException, BitPayGenericException {
if (Objects.isNull(facade)) {
throw new BitPayException(null, "missing required parameter");
BitPayExceptionProvider.throwValidationException("Missing required parameter");
}

Token token = new Token();
token.setId(this.identity);
token.setGuid(this.guidGenerator.execute());
Expand All @@ -108,32 +109,31 @@ public String authorizeClient(Facade facade) throws BitPayException {

JsonMapper mapper = JsonMapperFactory.create();

String json;
String json = null;

try {
json = mapper.writeValueAsString(token);
} catch (JsonProcessingException e) {
throw new BitPayException(null, "failed to serialize Token object : " + e.getMessage());
BitPayExceptionProvider.throwSerializeResourceException("Token", e.getMessage());
}

HttpResponse response = this.bitPayClient.post("tokens", json);
String response = this.bitPayClient.post("tokens", json);

List<Token> tokens;
List<Token> tokens = null;

try {
tokens = Arrays.asList(mapper.readValue(this.bitPayClient.responseToJsonString(response), Token[].class));
tokens = Arrays.asList(mapper.readValue(response, Token[].class));

// Expecting a single token resource.
if (tokens.size() != 1) {
throw new BitPayException(null, "failed to get token resource; expected 1 token, got " + tokens.size());
BitPayExceptionProvider.throwDeserializeResourceException(
"Token",
"expected 1 token, got " + tokens.size()
);
}

} catch (JsonProcessingException e) {
throw new BitPayException(null,
"failed to deserialize BitPay server response (Tokens) : " + e.getMessage());
} catch (Exception e) {
throw new BitPayException(null,
"failed to deserialize BitPay server response (Tokens) : " + e.getMessage());
BitPayExceptionProvider.throwDeserializeResourceException("Tokens", e.getMessage());
}

this.accessToken.put(tokens.get(0).getFacade(), tokens.get(0).getValue());
Expand Down
Loading

0 comments on commit 296d86c

Please sign in to comment.