-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add partial authorization in payment request
- Loading branch information
1 parent
0bfe87e
commit cbc519c
Showing
2 changed files
with
127 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,15 @@ import ( | |
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/checkout/checkout-sdk-go/common" | ||
"github.com/checkout/checkout-sdk-go/payments" | ||
"github.com/checkout/checkout-sdk-go/payments/nas" | ||
"github.com/checkout/checkout-sdk-go/payments/nas/sources" | ||
) | ||
|
||
func TestIncrementAuthorization(t *testing.T) { | ||
t.Skip("Skipping because increment authorization needs an authorized payment") | ||
paymentResponse := makeCardPayment(t, false, 10) | ||
paymentResponse := makeCardPaymentPartialAuthorization(t, false, 10) | ||
assert.NotNil(t, paymentResponse, "Expected paymentResponse not to be nil") | ||
|
||
metadata := make(map[string]interface{}) | ||
metadata["TestIncrementAuthorization"] = "metadata" | ||
|
@@ -38,20 +40,26 @@ func TestIncrementAuthorization(t *testing.T) { | |
checkerOne: func(response *nas.IncrementAuthorizationResponse, err error) { | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.NotEmpty(t, response.Reference) | ||
assert.Equal(t, int64(5), response.Amount) | ||
assert.NotEmpty(t, response.ActionId) | ||
assert.NotEmpty(t, response.Currency) | ||
assert.False(t, response.Approved) | ||
assert.NotEmpty(t, response.ResponseCode) | ||
assert.NotEmpty(t, response.ResponseSummary) | ||
assert.NotEmpty(t, response.ExpiresOn) | ||
assert.NotEmpty(t, response.ProcessedOn) | ||
assert.NotEmpty(t, response.Balances) | ||
assert.NotEmpty(t, response.Links) | ||
assert.NotEmpty(t, response.Links["payment"]) | ||
}, | ||
checkerTwo: func(response *nas.GetPaymentResponse, err error) { | ||
assert.NotEmpty(t, response.Balances) | ||
assert.Equal(t, int64(10), response.Balances.TotalAuthorized) | ||
assert.Equal(t, int64(5), response.Balances.TotalCaptured) | ||
assert.Equal(t, int64(0), response.Balances.TotalCaptured) | ||
assert.Equal(t, int64(0), response.Balances.TotalRefunded) | ||
assert.Equal(t, int64(0), response.Balances.TotalVoided) | ||
assert.Equal(t, int64(0), response.Balances.AvailableToCapture) | ||
assert.Equal(t, int64(5), response.Balances.AvailableToRefund) | ||
assert.Equal(t, int64(0), response.Balances.AvailableToVoid) | ||
assert.Equal(t, int64(10), response.Balances.AvailableToCapture) | ||
assert.Equal(t, int64(0), response.Balances.AvailableToRefund) | ||
assert.Equal(t, int64(10), response.Balances.AvailableToVoid) | ||
}, | ||
}, | ||
} | ||
|
@@ -69,8 +77,8 @@ func TestIncrementAuthorization(t *testing.T) { | |
} | ||
|
||
func TestIncrementAuthorizationIdempotently(t *testing.T) { | ||
t.Skip("Skipping because increment authorization needs an authorized payment") | ||
paymentResponse := makeCardPayment(t, false, 10) | ||
paymentResponse := makeCardPaymentPartialAuthorization(t, false, 10) | ||
assert.NotNil(t, paymentResponse, "Expected paymentResponse not to be nil") | ||
|
||
metadata := make(map[string]interface{}) | ||
metadata["TestIncrementAuthorization"] = "metadata" | ||
|
@@ -115,7 +123,9 @@ func TestIncrementAuthorizationIdempotently(t *testing.T) { | |
checker: func(response1 interface{}, err1 error, response2 interface{}, err2 error) { | ||
assert.Nil(t, err1) | ||
assert.NotNil(t, response1) | ||
assert.NotNil(t, err2) | ||
assert.Nil(t, err2) | ||
assert.NotNil(t, response2) | ||
assert.NotEqual(t, response1.(*nas.IncrementAuthorizationResponse).ActionId, response2.(*nas.IncrementAuthorizationResponse).ActionId) | ||
}, | ||
}, | ||
} | ||
|
@@ -128,15 +138,15 @@ func TestIncrementAuthorizationIdempotently(t *testing.T) { | |
return client.IncrementAuthorization(tc.paymentId, tc.request, &tc.idempotencyKeyRandom1) | ||
} | ||
predicateOne := func(data interface{}) bool { | ||
response := data.(*payments.CaptureResponse) | ||
response := data.(*nas.IncrementAuthorizationResponse) | ||
return response.Links != nil && len(response.Links) >= 0 | ||
} | ||
|
||
processTwo := func() (interface{}, error) { | ||
return client.IncrementAuthorization(tc.paymentId, tc.request, &tc.idempotencyKeyRandom2) | ||
} | ||
predicateTwo := func(data interface{}) bool { | ||
response := data.(*payments.CaptureResponse) | ||
response := data.(*nas.IncrementAuthorizationResponse) | ||
return response.Links != nil && len(response.Links) >= 0 | ||
} | ||
|
||
|
@@ -146,3 +156,71 @@ func TestIncrementAuthorizationIdempotently(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func makeCardPaymentPartialAuthorization(t *testing.T, shouldCapture bool, amount int64) *nas.PaymentResponse { | ||
currentYear := time.Now().Year() + 1 | ||
|
||
cardSource := sources.NewRequestCardSource() | ||
cardSource.Name = "Mr. Test" | ||
cardSource.Number = "4556447238607884" | ||
cardSource.ExpiryYear = currentYear | ||
cardSource.ExpiryMonth = 12 | ||
cardSource.Cvv = "123" | ||
cardSource.BillingAddress = &common.Address{ | ||
AddressLine1: "CheckoutSdk.com", | ||
AddressLine2: "90 Tottenham Court Road", | ||
City: "London", | ||
State: "London", | ||
Zip: "W1T 4TJ", | ||
Country: common.GB, | ||
} | ||
cardSource.Phone = &common.Phone{ | ||
CountryCode: "44", | ||
Number: "1234567890", | ||
} | ||
|
||
customerRequest := &common.CustomerRequest{ | ||
Email: "[email protected]", | ||
Name: "Test Customer", | ||
Phone: &common.Phone{ | ||
CountryCode: "44", | ||
Number: "1234567890", | ||
}, | ||
} | ||
|
||
paymentIndividualSender := nas.NewRequestIndividualSender() | ||
paymentIndividualSender.FirstName = "Mr" | ||
paymentIndividualSender.LastName = "Test" | ||
paymentIndividualSender.Address = &common.Address{ | ||
AddressLine1: "CheckoutSdk.com", | ||
AddressLine2: "90 Tottenham Court Road", | ||
City: "London", | ||
State: "London", | ||
Zip: "W1T 4TJ", | ||
Country: common.GB, | ||
} | ||
|
||
paymentRequest := nas.PaymentRequest{ | ||
Source: cardSource, | ||
Amount: amount, | ||
Currency: common.USD, | ||
Reference: uuid.New().String(), | ||
Description: "Test Payment", | ||
Capture: shouldCapture, | ||
Customer: customerRequest, | ||
Sender: paymentIndividualSender, | ||
AuthorizationType: nas.EstimatedAuthorizationType, | ||
PartialAuthorization: &nas.PartialAuthorization{ | ||
Enabled: true, | ||
}, | ||
BillingDescriptor: &payments.BillingDescriptor{ | ||
Name: "CheckoutSdk.com", | ||
City: "London", | ||
}, | ||
} | ||
|
||
response, err := DefaultApi().Payments.RequestPayment(paymentRequest, nil) | ||
assert.Nil(t, err, "Expected no error in RequestPayment") | ||
assert.NotNil(t, response, "Expected response not to be nil") | ||
return response | ||
} |