From e5653b12bd8e5b0efb488d0cfd49aed63788919f Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Thu, 20 Mar 2025 14:55:01 -0600 Subject: [PATCH] feat: adds referral customer billing functions --- CHANGELOG.md | 5 + examples | 2 +- .../java/com/easypost/model/ClientSecret.java | 8 ++ .../easypost/model/PaymentMethodObject.java | 2 + .../service/BetaReferralCustomerService.java | 41 ++++++++ .../service/ReferralCustomerService.java | 53 +++++++++++ .../create_bank_account_client_secret.json | 94 +++++++++++++++++++ .../create_credit_card_client_secret.json | 91 ++++++++++++++++++ .../add_bank_account_from_stripe.json | 91 ++++++++++++++++++ .../add_credit_card_from_stripe.json | 91 ++++++++++++++++++ .../{referral => referral_customer}/all.json | 0 .../create.json | 0 .../create_bad_stripe_token.json | 0 .../get_next_page.json | 0 .../referral_add_credit_card.json | 0 .../update.json | 0 .../easypost/BetaReferralCustomerTest.java | 30 ++++++ src/test/java/com/easypost/Fixtures.java | 20 ++-- ...ralTest.java => ReferralCustomerTest.java} | 50 +++++++++- .../easypost/fixtures/FixtureStructure.java | 4 + .../easypost/fixtures/components/Billing.java | 20 ++++ 21 files changed, 590 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/easypost/model/ClientSecret.java create mode 100644 src/test/cassettes/beta_referral_customer/create_bank_account_client_secret.json create mode 100644 src/test/cassettes/beta_referral_customer/create_credit_card_client_secret.json create mode 100644 src/test/cassettes/referral_customer/add_bank_account_from_stripe.json create mode 100644 src/test/cassettes/referral_customer/add_credit_card_from_stripe.json rename src/test/cassettes/{referral => referral_customer}/all.json (100%) rename src/test/cassettes/{referral => referral_customer}/create.json (100%) rename src/test/cassettes/{referral => referral_customer}/create_bad_stripe_token.json (100%) rename src/test/cassettes/{referral => referral_customer}/get_next_page.json (100%) rename src/test/cassettes/{referral => referral_customer}/referral_add_credit_card.json (100%) rename src/test/cassettes/{referral => referral_customer}/update.json (100%) rename src/test/java/com/easypost/{ReferralTest.java => ReferralCustomerTest.java} (76%) create mode 100644 src/test/java/com/easypost/fixtures/components/Billing.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a05adbe7..9ba4ded72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ ## Next Release - Adds `WebhookCustomHeader` model, allowing `custom_headers` to be passed when creating/updating a webhook +- Adds the following functions to assist ReferralCustomers add credit cards and bank accounts: + - `betaReferralCustomer.createCreditCardClientSecret` + - `betaReferralCustomer.createBankAccountClientSecret` + - `referralCustomer.addCreditCardFromStripe` + - `referralCustomer.addBankAccountFromStripe` - Adds `tracking_codes` param to tracker index endpoint - Fixes error parsing - Allows for alternative format of `errors` field (previously we deserialized the `errors` field into a list of `Error` objects; however, sometimes the errors are simply a list of strings. This change make the `errors` field a list of `Object` allowing for either the new `FieldError` object or a list of strings. Users will need to check for the type of error returned and handle appropriately) diff --git a/examples b/examples index 7669825fb..394ea5eff 160000 --- a/examples +++ b/examples @@ -1 +1 @@ -Subproject commit 7669825fb53be074d7f585c78c4f38ad4fefe0d0 +Subproject commit 394ea5effde57b304c88999761126953cb1c7e91 diff --git a/src/main/java/com/easypost/model/ClientSecret.java b/src/main/java/com/easypost/model/ClientSecret.java new file mode 100644 index 000000000..cde3d932e --- /dev/null +++ b/src/main/java/com/easypost/model/ClientSecret.java @@ -0,0 +1,8 @@ +package com.easypost.model; + +import lombok.Getter; + +@Getter +public final class ClientSecret { + private String clientSecret; +} diff --git a/src/main/java/com/easypost/model/PaymentMethodObject.java b/src/main/java/com/easypost/model/PaymentMethodObject.java index a4f924e4a..e978134be 100644 --- a/src/main/java/com/easypost/model/PaymentMethodObject.java +++ b/src/main/java/com/easypost/model/PaymentMethodObject.java @@ -48,6 +48,8 @@ String getEndpoint() { private String name; // bank_account private boolean verified; + // both + private boolean requiresMandateCollection; /** * Get the type of this PaymentMethodObject object. diff --git a/src/main/java/com/easypost/service/BetaReferralCustomerService.java b/src/main/java/com/easypost/service/BetaReferralCustomerService.java index 474312c40..e76704b8d 100644 --- a/src/main/java/com/easypost/service/BetaReferralCustomerService.java +++ b/src/main/java/com/easypost/service/BetaReferralCustomerService.java @@ -6,6 +6,7 @@ import com.easypost.http.Requestor; import com.easypost.http.Requestor.RequestMethod; import com.easypost.model.BetaPaymentRefund; +import com.easypost.model.ClientSecret; import com.easypost.model.PaymentMethod; import com.easypost.model.PaymentMethodObject; @@ -92,4 +93,44 @@ public BetaPaymentRefund refundByPaymentLog(String paymentLogId) throws EasyPost return Requestor.request(RequestMethod.POST, endpoint, params, BetaPaymentRefund.class, client, "beta"); } + + /** + * Creates a client secret to use with Stripe when adding a credit card. + * + * @return ClientSecret containing the client secret. + * @throws EasyPostException When the request fails. + */ + public ClientSecret createCreditCardClientSecret() throws EasyPostException { + String endpoint = "setup_intents"; + + return Requestor.request(RequestMethod.POST, endpoint, null, ClientSecret.class, client, "beta"); + } + + /** + * Creates a client secret to use with Stripe when adding a bank account. + * + * @return ClientSecret containing the client secret. + * @throws EasyPostException When the request fails. + */ + public ClientSecret createBankAccountClientSecret() throws EasyPostException { + return createBankAccountClientSecret(null); + } + + /** + * Creates a client secret to use with Stripe when adding a bank account. + * + * @param returnUrl Optional return URL for the bank account setup. + * @return ClientSecret containing the client secret. + * @throws EasyPostException When the request fails. + */ + public ClientSecret createBankAccountClientSecret(String returnUrl) throws EasyPostException { + HashMap params = new HashMap<>(); + if (returnUrl != null) { + params.put("return_url", returnUrl); + } + + String endpoint = "financial_connections_sessions"; + + return Requestor.request(RequestMethod.POST, endpoint, params, ClientSecret.class, client, "beta"); + } } diff --git a/src/main/java/com/easypost/service/ReferralCustomerService.java b/src/main/java/com/easypost/service/ReferralCustomerService.java index 92c0b4309..e21f2320f 100644 --- a/src/main/java/com/easypost/service/ReferralCustomerService.java +++ b/src/main/java/com/easypost/service/ReferralCustomerService.java @@ -166,6 +166,59 @@ public PaymentMethodObject addCreditCardToUser(final String referralApiKey, fina return createEasypostCreditCard(referralApiKey, stripeToken, priority.toString().toLowerCase()); } + /** + * Add a credit card to EasyPost for a ReferralCustomer with a payment method ID from Stripe. + * This function requires the ReferralCustomer User's API key. + * + * @param referralApiKey API key of the referral user. + * @param paymentMethodId Payment method ID from Stripe. + * @param priority Priority of the credit card (e.g., "primary" or "secondary"). + * @return PaymentMethodObject object. + * @throws EasyPostException when the request fails. + */ + public PaymentMethodObject addCreditCardFromStripe(final String referralApiKey, final String paymentMethodId, + final PaymentMethod.Priority priority) throws EasyPostException { + Map params = new HashMap<>(); + Map creditCardParams = new HashMap<>(); + creditCardParams.put("payment_method_id", paymentMethodId); + creditCardParams.put("priority", priority.toString().toLowerCase()); + params.put("credit_card", creditCardParams); + + EasyPostClient referralClient = new EasyPostClient(referralApiKey); + + String endpoint = "credit_cards"; + + return Requestor.request(RequestMethod.POST, endpoint, params, PaymentMethodObject.class, referralClient); + } + + /** + * Add a bank account to EasyPost for a ReferralCustomer. + * This function requires the ReferralCustomer User's API key. + * + * @param referralApiKey API key of the referral user. + * @param financialConnectionsId Financial connections ID from Stripe. + * @param mandateData Mandate data for the bank account. + * @param priority Priority of the bank account (e.g., "primary" or "secondary"). + * @return PaymentMethodObject object. + * @throws EasyPostException when the request fails. + */ + public PaymentMethodObject addBankAccountFromStripe(final String referralApiKey, + final String financialConnectionsId, + final Map mandateData, + final PaymentMethod.Priority priority) + throws EasyPostException { + Map params = new HashMap<>(); + params.put("financial_connections_id", financialConnectionsId); + params.put("mandate_data", mandateData); + params.put("priority", priority.toString().toLowerCase()); + + EasyPostClient referralClient = new EasyPostClient(referralApiKey); + + String endpoint = "bank_accounts"; + + return Requestor.request(RequestMethod.POST, endpoint, params, PaymentMethodObject.class, referralClient); + } + /** * Retrieve EasyPost Stripe API key. * diff --git a/src/test/cassettes/beta_referral_customer/create_bank_account_client_secret.json b/src/test/cassettes/beta_referral_customer/create_bank_account_client_secret.json new file mode 100644 index 000000000..b884ece5c --- /dev/null +++ b/src/test/cassettes/beta_referral_customer/create_bank_account_client_secret.json @@ -0,0 +1,94 @@ +[ + { + "recordedAt": 1742502951, + "request": { + "body": "{}", + "method": "POST", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ], + "Content-Type": [ + "application/json" + ] + }, + "uri": "https://api.easypost.com/beta/financial_connections_sessions" + }, + "response": { + "body": "{\n \"client_secret\": \"fcsess_client_secret_TGjLr3f2VIuNe5Nm7otCKq4a\"\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 201 Created" + ], + "content-length": [ + "65" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb53nuq" + ], + "vary": [ + "Origin" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-download-options": [ + "noopen" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-backend": [ + "easypost" + ], + "pragma": [ + "no-cache" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-ep-request-uuid": [ + "64f5803467dc7c26e2ba8772002bb9ad" + ], + "x-proxied": [ + "intlb4nuq 284c5d344a", + "extlb1nuq 99aac35317" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.463688" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202503202010-5de35d6868-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 201, + "message": "Created" + }, + "uri": "https://api.easypost.com/beta/financial_connections_sessions" + }, + "duration": 580 + } +] \ No newline at end of file diff --git a/src/test/cassettes/beta_referral_customer/create_credit_card_client_secret.json b/src/test/cassettes/beta_referral_customer/create_credit_card_client_secret.json new file mode 100644 index 000000000..d8e427da9 --- /dev/null +++ b/src/test/cassettes/beta_referral_customer/create_credit_card_client_secret.json @@ -0,0 +1,91 @@ +[ + { + "recordedAt": 1742502952, + "request": { + "body": "", + "method": "POST", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ] + }, + "uri": "https://api.easypost.com/beta/setup_intents" + }, + "response": { + "body": "{\n \"client_secret\": \"seti_0R4pohDqT4huGUvdzzsjybrJ_secret_RynPW651kwMzuL7DNOr8EdW7vvLqQJ8\"\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 200 OK" + ], + "content-length": [ + "88" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb53nuq" + ], + "vary": [ + "Origin" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-download-options": [ + "noopen" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-backend": [ + "easypost" + ], + "pragma": [ + "no-cache" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-ep-request-uuid": [ + "64f5803467dc7c27e2ba8776002bbab8" + ], + "x-proxied": [ + "intlb3nuq 284c5d344a", + "extlb1nuq 99aac35317" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.586783" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202503202010-5de35d6868-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 200, + "message": "OK" + }, + "uri": "https://api.easypost.com/beta/setup_intents" + }, + "duration": 697 + } +] \ No newline at end of file diff --git a/src/test/cassettes/referral_customer/add_bank_account_from_stripe.json b/src/test/cassettes/referral_customer/add_bank_account_from_stripe.json new file mode 100644 index 000000000..a22b51d94 --- /dev/null +++ b/src/test/cassettes/referral_customer/add_bank_account_from_stripe.json @@ -0,0 +1,91 @@ +[ + { + "recordedAt": 1742503697, + "request": { + "body": "{\n \"financial_connections_id\": \"fca_0QAc7sDqT4huGUvdf6BahYa9\",\n \"mandate_data\": {\n \"ip_address\": \"127.0.0.1\",\n \"accepted_at\": 1.72251073E9,\n \"user_agent\": \"Mozilla/5.0\"\n },\n \"priority\": \"primary\"\n}", + "method": "POST", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ], + "Content-Type": [ + "application/json" + ] + }, + "uri": "https://api.easypost.com/v2/bank_accounts" + }, + "response": { + "body": "{\n \"error\": {\n \"code\": \"BANK_ACCOUNT.INVALID_PARAMS\",\n \"message\": \"account_holder_name must be present when creating a Financial Connections payment method\",\n \"errors\": []\n }\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 422 Unprocessable Entity" + ], + "content-length": [ + "161" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb33nuq" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-download-options": [ + "noopen" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-backend": [ + "easypost" + ], + "pragma": [ + "no-cache" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-ep-request-uuid": [ + "64f5803467dc7f11e2baa010002efd3c" + ], + "x-proxied": [ + "intlb4nuq 284c5d344a", + "extlb1nuq 99aac35317" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.036008" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202503202010-5de35d6868-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 422, + "message": "Unprocessable Entity" + }, + "uri": "https://api.easypost.com/v2/bank_accounts" + }, + "duration": 146 + } +] \ No newline at end of file diff --git a/src/test/cassettes/referral_customer/add_credit_card_from_stripe.json b/src/test/cassettes/referral_customer/add_credit_card_from_stripe.json new file mode 100644 index 000000000..a11a1a9ec --- /dev/null +++ b/src/test/cassettes/referral_customer/add_credit_card_from_stripe.json @@ -0,0 +1,91 @@ +[ + { + "recordedAt": 1742503697, + "request": { + "body": "{\n \"credit_card\": {\n \"payment_method_id\": \"pm_0Pn6bQDqT4huGUvd0CjpRerH\",\n \"priority\": \"primary\"\n }\n}", + "method": "POST", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ], + "Content-Type": [ + "application/json" + ] + }, + "uri": "https://api.easypost.com/v2/credit_cards" + }, + "response": { + "body": "{\n \"error\": {\n \"code\": \"CREDIT_CARD.NOT_FOUND\",\n \"message\": \"Stripe::PaymentMethod does not exist for the specified reference_id\",\n \"errors\": []\n }\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 404 Not Found" + ], + "content-length": [ + "134" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb42nuq" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-download-options": [ + "noopen" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-backend": [ + "easypost" + ], + "pragma": [ + "no-cache" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-ep-request-uuid": [ + "64f5803167dc7f11e2baa012002efd6f" + ], + "x-proxied": [ + "intlb3nuq 284c5d344a", + "extlb1nuq 99aac35317" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.444659" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202503202010-5de35d6868-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 404, + "message": "Not Found" + }, + "uri": "https://api.easypost.com/v2/credit_cards" + }, + "duration": 562 + } +] \ No newline at end of file diff --git a/src/test/cassettes/referral/all.json b/src/test/cassettes/referral_customer/all.json similarity index 100% rename from src/test/cassettes/referral/all.json rename to src/test/cassettes/referral_customer/all.json diff --git a/src/test/cassettes/referral/create.json b/src/test/cassettes/referral_customer/create.json similarity index 100% rename from src/test/cassettes/referral/create.json rename to src/test/cassettes/referral_customer/create.json diff --git a/src/test/cassettes/referral/create_bad_stripe_token.json b/src/test/cassettes/referral_customer/create_bad_stripe_token.json similarity index 100% rename from src/test/cassettes/referral/create_bad_stripe_token.json rename to src/test/cassettes/referral_customer/create_bad_stripe_token.json diff --git a/src/test/cassettes/referral/get_next_page.json b/src/test/cassettes/referral_customer/get_next_page.json similarity index 100% rename from src/test/cassettes/referral/get_next_page.json rename to src/test/cassettes/referral_customer/get_next_page.json diff --git a/src/test/cassettes/referral/referral_add_credit_card.json b/src/test/cassettes/referral_customer/referral_add_credit_card.json similarity index 100% rename from src/test/cassettes/referral/referral_add_credit_card.json rename to src/test/cassettes/referral_customer/referral_add_credit_card.json diff --git a/src/test/cassettes/referral/update.json b/src/test/cassettes/referral_customer/update.json similarity index 100% rename from src/test/cassettes/referral/update.json rename to src/test/cassettes/referral_customer/update.json diff --git a/src/test/java/com/easypost/BetaReferralCustomerTest.java b/src/test/java/com/easypost/BetaReferralCustomerTest.java index c83b2ae46..c47784f95 100644 --- a/src/test/java/com/easypost/BetaReferralCustomerTest.java +++ b/src/test/java/com/easypost/BetaReferralCustomerTest.java @@ -2,11 +2,13 @@ import com.easypost.exception.API.InvalidRequestError; import com.easypost.exception.EasyPostException; +import com.easypost.model.ClientSecret; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class BetaReferralCustomerTest { private static TestUtils.VCR vcr; @@ -69,4 +71,32 @@ public void testRefundByPaymentLogId() throws EasyPostException { assertEquals(422, exception.getStatusCode()); assertEquals("We could not find a transaction with that id.", exception.getMessage()); } + + /** + * Test creating a client secret for credit cards. + * + * @throws EasyPostException when the request fails. + */ + @Test + public void testCreateCreditCardClientSecret() throws EasyPostException { + vcr.setUpTest("create_credit_card_client_secret"); + + ClientSecret response = vcr.client.betaReferralCustomer.createCreditCardClientSecret(); + + assertTrue(response.getClientSecret().startsWith("seti_")); + } + + /** + * Test creating a client secret for bank accounts. + * + * @throws EasyPostException when the request fails. + */ + @Test + public void testCreateBankAccountClientSecret() throws EasyPostException { + vcr.setUpTest("create_bank_account_client_secret"); + + ClientSecret response = vcr.client.betaReferralCustomer.createBankAccountClientSecret(); + + assertTrue(response.getClientSecret().startsWith("fcsess_client_secret_")); + } } diff --git a/src/test/java/com/easypost/Fixtures.java b/src/test/java/com/easypost/Fixtures.java index a7217337c..9608649bd 100644 --- a/src/test/java/com/easypost/Fixtures.java +++ b/src/test/java/com/easypost/Fixtures.java @@ -17,6 +17,8 @@ import java.util.List; import java.util.Objects; +import com.easypost.fixtures.components.Billing; + import static com.easypost.TestUtils.getSourceFileDirectory; import static com.easypost.TestUtils.readFile; @@ -104,15 +106,6 @@ public static String reportDate() { return "2022-05-04"; } - /** - * Get the default report prefix. - * - * @return The default report prefix - */ - public static String reportIdPrefix() { - return "shprep_"; - } - /** * Get the first default address. * @@ -380,4 +373,13 @@ public static String plannedShipDate() { public static String desiredDeliveryDate() { return "2024-08-21"; } + + /** + * Get the billing fixture data. + * + * @return The billing fixture data + */ + public static Billing billing() { + return Objects.requireNonNull(getFixtureData()).billing; + } } diff --git a/src/test/java/com/easypost/ReferralTest.java b/src/test/java/com/easypost/ReferralCustomerTest.java similarity index 76% rename from src/test/java/com/easypost/ReferralTest.java rename to src/test/java/com/easypost/ReferralCustomerTest.java index dad873208..fecd51a5e 100644 --- a/src/test/java/com/easypost/ReferralTest.java +++ b/src/test/java/com/easypost/ReferralCustomerTest.java @@ -2,7 +2,9 @@ import com.easypost.exception.API.ExternalApiError; import com.easypost.exception.EasyPostException; +import com.easypost.exception.API.InvalidRequestError; import com.easypost.exception.General.EndOfPaginationError; +import com.easypost.exception.API.NotFoundError; import com.easypost.model.PaymentMethod; import com.easypost.model.PaymentMethodObject; import com.easypost.model.ReferralCustomer; @@ -23,7 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; -public final class ReferralTest { +public final class ReferralCustomerTest { private static TestUtils.VCR vcr; /** @@ -33,7 +35,7 @@ public final class ReferralTest { */ @BeforeAll public static void setup() throws EasyPostException { - vcr = new TestUtils.VCR("referral", TestUtils.ApiKey.PARTNER); + vcr = new TestUtils.VCR("referral_customer", TestUtils.ApiKey.PARTNER); } /** @@ -169,4 +171,48 @@ public void testCreateBadStripeToken() throws Exception { () -> vcr.client.referralCustomer.addCreditCardToUser(referralUserKey(), "1234", 1234, 1234, "1234", PaymentMethod.Priority.PRIMARY)); } + + /** + * Test adding a credit card from Stripe for a Referral user raises an error when it fails. + * + * @throws EasyPostException when the request fails. + */ + @Test + public void testAddCreditCardFromStripe() throws EasyPostException { + vcr.setUpTest("add_credit_card_from_stripe"); + + NotFoundError exception = assertThrows(NotFoundError.class, () -> { + vcr.client.referralCustomer.addCreditCardFromStripe( + referralUserKey(), + Fixtures.billing().paymentMethodId, + PaymentMethod.Priority.PRIMARY + ); + }); + + assertEquals("Stripe::PaymentMethod does not exist for the specified reference_id", exception.getMessage()); + } + + /** + * Test adding a bank account from Stripe for a Referral user raises an error when it fails. + * + * @throws EasyPostException when the request fails. + */ + @Test + public void testAddBankAccountFromStripe() throws EasyPostException { + vcr.setUpTest("add_bank_account_from_stripe"); + + InvalidRequestError exception = assertThrows(InvalidRequestError.class, () -> { + vcr.client.referralCustomer.addBankAccountFromStripe( + referralUserKey(), + Fixtures.billing().financialConnectionsId, + Fixtures.billing().mandateData, + PaymentMethod.Priority.PRIMARY + ); + }); + + assertEquals( + "account_holder_name must be present when creating a Financial Connections payment method", + exception.getMessage() + ); + } } diff --git a/src/test/java/com/easypost/fixtures/FixtureStructure.java b/src/test/java/com/easypost/fixtures/FixtureStructure.java index 3be812070..9f7c2ace1 100644 --- a/src/test/java/com/easypost/fixtures/FixtureStructure.java +++ b/src/test/java/com/easypost/fixtures/FixtureStructure.java @@ -1,6 +1,7 @@ package com.easypost.fixtures; import com.easypost.fixtures.components.Addresses; +import com.easypost.fixtures.components.Billing; import com.easypost.fixtures.components.CarrierAccounts; import com.easypost.fixtures.components.CarrierStrings; import com.easypost.fixtures.components.Claims; @@ -26,6 +27,9 @@ public final class FixtureStructure { @SerializedName ("addresses") public Addresses addresses; + @SerializedName ("billing") + public Billing billing; + @SerializedName ("carrier_accounts") public CarrierAccounts carrierAccounts; diff --git a/src/test/java/com/easypost/fixtures/components/Billing.java b/src/test/java/com/easypost/fixtures/components/Billing.java new file mode 100644 index 000000000..b7432c7a0 --- /dev/null +++ b/src/test/java/com/easypost/fixtures/components/Billing.java @@ -0,0 +1,20 @@ +package com.easypost.fixtures.components; + +import com.google.gson.annotations.SerializedName; + +import java.util.HashMap; + +public final class Billing { + @SerializedName ("payment_method_id") + public String paymentMethodId; + + @SerializedName ("financial_connections_id") + public String financialConnectionsId; + + @SerializedName ("mandate_data") + public HashMap mandateData; + + @SerializedName ("priority") + public String priority; + +}