Skip to content

Commit

Permalink
Submit for partial settlement (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbook13 authored Apr 21, 2021
1 parent 1b9363c commit 6684e71
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
5 changes: 3 additions & 2 deletions apple_pay_card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func TestApplePayCard_MarshalXML(t *testing.T) {

expected := `<apple-pay-card><expiration-month>10</expiration-month><expiration-year>22</expiration-year><eci-indicator>07</eci-indicator><cryptogram>testCardCryptogram</cryptogram><number>4111111111111111</number><cardholder-name>Test User</cardholder-name></apple-pay-card>`

if string(cardBytes) != expected {
cardString := string(cardBytes)
if cardString != expected {
t.Errorf("Marshalled xml got [%s], want [%s]", cardString, expected)
}
}
}
18 changes: 18 additions & 0 deletions transaction_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ func (g *TransactionGateway) Clone(ctx context.Context, id string, tx *Transacti
return nil, &invalidResponseError{resp}
}

// SubmitForPartialSettlement submits the transaction with the specified id for settlement for the specified amount.
// This function can be called multiple times for the same transaction as long as the total settled amount does not
// exceed the value of the initial authorization.
func (g *TransactionGateway) SubmitForPartialSettlement(ctx context.Context, id string, amount *Decimal) (*Transaction, error) {
tx := &TransactionRequest{
Amount: amount,
}
resp, err := g.execute(ctx, "POST", "transactions/"+id+"/submit_for_partial_settlement", tx)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 201:
return resp.transaction()
}
return nil, &invalidResponseError{resp}
}

// SubmitForSettlement submits the transaction with the specified id for settlement.
// If the amount is omitted, the full amount is settled.
func (g *TransactionGateway) SubmitForSettlement(ctx context.Context, id string, amount ...*Decimal) (*Transaction, error) {
Expand Down
62 changes: 60 additions & 2 deletions transaction_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,65 @@ func TestTransactionCreateSubmitForSettlementAndVoid(t *testing.T) {
}
}

func TestTransactionSubmitForPartialSettlement(t *testing.T) {
t.Parallel()

ctx := context.Background()

tx, err := testGateway.Transaction().Create(ctx, &TransactionRequest{
Type: "sale",
Amount: NewDecimal(2000, 2),
CreditCard: &CreditCard{
Number: testCardVisa,
ExpirationDate: "05/25",
},
})

t.Log(tx)

if err != nil {
t.Fatal(err)
}
if tx.Id == "" {
t.Fatal("Received invalid ID on new transaction")
}
if tx.Status != TransactionStatusAuthorized {
t.Fatal(tx.Status)
}

ten := NewDecimal(1000, 2)

// First partial settlement submission
tx2, err := testGateway.Transaction().SubmitForPartialSettlement(ctx, tx.Id, ten)

t.Log(tx2)

if err != nil {
t.Fatal(err)
}
if x := tx2.Status; x != TransactionStatusSubmittedForSettlement {
t.Fatal(x)
}
if amount := tx2.Amount; amount.Cmp(ten) != 0 {
t.Fatalf("transaction settlement amount (%s) did not equal amount requested (%s)", amount, ten)
}

// Second partial settlement submission
tx3, err := testGateway.Transaction().SubmitForPartialSettlement(ctx, tx.Id, ten)

t.Log(tx3)

if err != nil {
t.Fatal(err)
}
if x := tx3.Status; x != TransactionStatusSubmittedForSettlement {
t.Fatal(x)
}
if amount := tx3.Amount; amount.Cmp(ten) != 0 {
t.Fatalf("transaction settlement amount (%s) did not equal amount requested (%s)", amount, ten)
}
}

func TestTransactionSearchIDs(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1827,7 +1886,7 @@ func TestTransactionExternalVault(t *testing.T) {
ExpirationDate: "05/14",
},
ExternalVault: &ExternalVault{
Status: ExternalVaultStatusVaulted,
Status: ExternalVaultStatusVaulted,
PreviousNetworkTransactionId: *tx.NetworkTransactionId,
},
})
Expand All @@ -1844,4 +1903,3 @@ func TestTransactionExternalVault(t *testing.T) {

t.Log(tx2)
}

0 comments on commit 6684e71

Please sign in to comment.