From 1b5f48790b0a3fdeb5ecaae8bb12c3aedce96eaa Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Mon, 8 Apr 2024 16:48:11 -0600 Subject: [PATCH 1/2] - Use payment method object type as fallback when determining type --- CHANGELOG.md | 1 + payment_method.go | 4 +-- tests/billing_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ead90e3..304139e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next Release +- Fix payment method funding and deletion failures due to undetermined payment method type - Adds `RefundInsurance` function in Insurance service for requesting a refund for a standalone insurance ## v4.1.1 (2024-03-26) diff --git a/payment_method.go b/payment_method.go index 18b9047..d803cbd 100644 --- a/payment_method.go +++ b/payment_method.go @@ -41,9 +41,9 @@ type PaymentMethodObject struct { // getPaymentMethodObjectType returns the PaymentMethodType enum of a PaymentMethodObject. func (c *Client) getPaymentMethodObjectType(object *PaymentMethodObject) (out PaymentMethodType, err error) { - if strings.HasPrefix(object.ID, "card_") { + if strings.HasPrefix(object.ID, "card_") || object.Object == "CreditCard" { out = CreditCardPaymentType - } else if strings.HasPrefix(object.ID, "bank_") { + } else if strings.HasPrefix(object.ID, "bank_") || object.Object == "BankAccount" { out = BankAccountPaymentType } else { return out, newInvalidObjectError(NoMatchingPaymentMethod) diff --git a/tests/billing_test.go b/tests/billing_test.go index 8abf1a7..ee0f12f 100644 --- a/tests/billing_test.go +++ b/tests/billing_test.go @@ -51,7 +51,7 @@ func GetBillingMockRequests() []easypost.MockRequest { }, ResponseInfo: easypost.MockRequestResponseInfo{ StatusCode: 200, - Body: `{"id": "summary_123", "primary_payment_method": {"id": "card_123", "last4": "1234"}, "secondary_payment_method": {"id": "bank_123", "bank_name": "Mock Bank"}}`, + Body: `{"id": "summary_123", "primary_payment_method": {"id": "pm_123", "object": "CreditCard", "last4": "1234"}, "secondary_payment_method": {"id": "pm_123", "object": "BankAccount", "bank_name": "Mock Bank"}}`, }, }, } @@ -89,3 +89,71 @@ func (c *ClientTests) TestRetrievePaymentMethods() { assert.True(paymentMethods.PrimaryPaymentMethod != nil) assert.True(paymentMethods.SecondaryPaymentMethod != nil) } + +func (c *ClientTests) TestGetPaymentMethodInfoByObjectType() { + mockRequests := []easypost.MockRequest{ + { + MatchRule: easypost.MockRequestMatchRule{ + Method: "GET", + UrlRegexPattern: "v2\\/payment_methods$", + }, + ResponseInfo: easypost.MockRequestResponseInfo{ + StatusCode: 200, + Body: `{"id": "summary_123", "primary_payment_method": {"id": "pm_123", "object": "CreditCard"}, "secondary_payment_method": {"id": "pm_123", "object": "BankAccount"}}`, + }, + }, + { + MatchRule: easypost.MockRequestMatchRule{ + Method: "DELETE", + UrlRegexPattern: "v2\\/credit_cards\\/pm_123$", + }, + ResponseInfo: easypost.MockRequestResponseInfo{ + StatusCode: 200, + Body: `{}`, + }, + }, + } + + client := c.MockClient(mockRequests) + require := c.Require() + + // getPaymentMethodObjectType is a private method, so we can't test it directly, but we can test it via DeletePaymentMethod + // The mocking setup here makes it so only /v2/credit_cards/pm_123 is allowed to be called + // If the method works correctly without error, we can assume it's because it found the correct payment method type + err := client.DeletePaymentMethod(easypost.PrimaryPaymentMethodPriority) + require.NoError(err) +} + +func (c *ClientTests) TestGetPaymentMethodInfoByLegacyIdPrefix() { + mockRequests := []easypost.MockRequest{ + { + MatchRule: easypost.MockRequestMatchRule{ + Method: "GET", + UrlRegexPattern: "v2\\/payment_methods$", + }, + ResponseInfo: easypost.MockRequestResponseInfo{ + StatusCode: 200, + Body: `{"id": "summary_123", "primary_payment_method": {"id": "card_123", "object": null}, "secondary_payment_method": {"id": "bank_123", "object": null}}`, + }, + }, + { + MatchRule: easypost.MockRequestMatchRule{ + Method: "DELETE", + UrlRegexPattern: "v2\\/bank_accounts\\/bank_123$", + }, + ResponseInfo: easypost.MockRequestResponseInfo{ + StatusCode: 200, + Body: `{}`, + }, + }, + } + + client := c.MockClient(mockRequests) + require := c.Require() + + // getPaymentMethodObjectType is a private method, so we can't test it directly, but we can test it via DeletePaymentMethod + // The mocking setup here makes it so only /v2/bank_accounts/bank_123 is allowed to be called + // If the method works correctly without error, we can assume it's because it found the correct payment method type + err := client.DeletePaymentMethod(easypost.SecondaryPaymentMethodPriority) + require.NoError(err) +} From 1b221773ff9583fd5e947d34247252d936500818 Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Wed, 10 Apr 2024 10:48:54 -0600 Subject: [PATCH 2/2] - Use payment method object type only to determine payment method type --- payment_method.go | 8 ++------ tests/billing_test.go | 34 ---------------------------------- 2 files changed, 2 insertions(+), 40 deletions(-) diff --git a/payment_method.go b/payment_method.go index d803cbd..ca3400b 100644 --- a/payment_method.go +++ b/payment_method.go @@ -1,9 +1,5 @@ package easypost -import ( - "strings" -) - type PaymentMethodPriority int64 const ( @@ -41,9 +37,9 @@ type PaymentMethodObject struct { // getPaymentMethodObjectType returns the PaymentMethodType enum of a PaymentMethodObject. func (c *Client) getPaymentMethodObjectType(object *PaymentMethodObject) (out PaymentMethodType, err error) { - if strings.HasPrefix(object.ID, "card_") || object.Object == "CreditCard" { + if object.Object == "CreditCard" { out = CreditCardPaymentType - } else if strings.HasPrefix(object.ID, "bank_") || object.Object == "BankAccount" { + } else if object.Object == "BankAccount" { out = BankAccountPaymentType } else { return out, newInvalidObjectError(NoMatchingPaymentMethod) diff --git a/tests/billing_test.go b/tests/billing_test.go index ee0f12f..2ad288c 100644 --- a/tests/billing_test.go +++ b/tests/billing_test.go @@ -123,37 +123,3 @@ func (c *ClientTests) TestGetPaymentMethodInfoByObjectType() { err := client.DeletePaymentMethod(easypost.PrimaryPaymentMethodPriority) require.NoError(err) } - -func (c *ClientTests) TestGetPaymentMethodInfoByLegacyIdPrefix() { - mockRequests := []easypost.MockRequest{ - { - MatchRule: easypost.MockRequestMatchRule{ - Method: "GET", - UrlRegexPattern: "v2\\/payment_methods$", - }, - ResponseInfo: easypost.MockRequestResponseInfo{ - StatusCode: 200, - Body: `{"id": "summary_123", "primary_payment_method": {"id": "card_123", "object": null}, "secondary_payment_method": {"id": "bank_123", "object": null}}`, - }, - }, - { - MatchRule: easypost.MockRequestMatchRule{ - Method: "DELETE", - UrlRegexPattern: "v2\\/bank_accounts\\/bank_123$", - }, - ResponseInfo: easypost.MockRequestResponseInfo{ - StatusCode: 200, - Body: `{}`, - }, - }, - } - - client := c.MockClient(mockRequests) - require := c.Require() - - // getPaymentMethodObjectType is a private method, so we can't test it directly, but we can test it via DeletePaymentMethod - // The mocking setup here makes it so only /v2/bank_accounts/bank_123 is allowed to be called - // If the method works correctly without error, we can assume it's because it found the correct payment method type - err := client.DeletePaymentMethod(easypost.SecondaryPaymentMethodPriority) - require.NoError(err) -}