-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathe2e_test.go
116 lines (95 loc) · 3.85 KB
/
e2e_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main_test
import (
"fmt"
"testing"
"github.com/alovak/cardflow-playground/acquirer"
acquirerClient "github.com/alovak/cardflow-playground/acquirer/client"
"github.com/alovak/cardflow-playground/acquirer/models"
"github.com/alovak/cardflow-playground/issuer"
issuerClient "github.com/alovak/cardflow-playground/issuer/client"
issuerModels "github.com/alovak/cardflow-playground/issuer/models"
"github.com/alovak/cardflow-playground/log"
"github.com/stretchr/testify/require"
)
func TestEndToEndTransaction(t *testing.T) {
// Initialize the issuer and acquirer components here
issuerBasePath, iso8583ServerAddr := setupIssuer(t)
acquirerBasePath := setupAcquirer(t, iso8583ServerAddr)
// configure the issuer client
issuerClient := issuerClient.New(issuerBasePath)
acquirerClient := acquirerClient.New(acquirerBasePath)
// Given: Create an account with $100 balance
accountID, err := issuerClient.CreateAccount(issuerModels.CreateAccount{
Balance: 100_00, // $100
Currency: "USD",
})
require.NoError(t, err)
// Issue a card for the account
card, err := issuerClient.IssueCard(accountID)
require.NoError(t, err)
// Given: Create a new merchant for the acquirer
merchant, err := acquirerClient.CreateMerchant(models.CreateMerchant{
Name: "Demo Merchant",
MCC: "5411",
PostalCode: "12345",
WebSite: "https://demo.merchant.com",
})
require.NoError(t, err)
// When: Acquirer receives the payment request for the merchant with the issued card
payment, err := acquirerClient.CreatePayment(merchant.ID, models.CreatePayment{
Card: models.Card{
Number: card.Number,
CardVerificationValue: card.CardVerificationValue,
ExpirationDate: card.ExpirationDate,
},
Amount: 10_00, // $10
Currency: "USD",
})
require.NoError(t, err)
// Then: There should be an authorized transaction in the acquirer
payment, err = acquirerClient.GetPayment(merchant.ID, payment.ID)
require.NoError(t, err)
require.Equal(t, models.PaymentStatusAuthorized, payment.Status)
// In the issuer, there should be an authorized transaction for the card
transactions, err := issuerClient.GetTransactions(accountID)
require.NoError(t, err)
require.Len(t, transactions, 1)
require.Equal(t, card.ID, transactions[0].CardID)
// check the transaction details
require.Equal(t, int64(10_00), transactions[0].Amount)
require.Equal(t, "USD", transactions[0].Currency)
require.Equal(t, issuerModels.TransactionStatusAuthorized, transactions[0].Status)
require.Equal(t, payment.AuthorizationCode, transactions[0].AuthorizationCode)
// check the merchant details
require.Equal(t, merchant.Name, transactions[0].Merchant.Name)
require.Equal(t, merchant.MCC, transactions[0].Merchant.MCC)
require.Equal(t, merchant.PostalCode, transactions[0].Merchant.PostalCode)
require.Equal(t, merchant.WebSite, transactions[0].Merchant.WebSite)
// Account's available balance should be less by the transaction amount
account, err := issuerClient.GetAccount(accountID)
require.NoError(t, err)
require.Equal(t, int64(100_00-10_00), account.AvailableBalance)
require.Equal(t, int64(10_00), account.HoldBalance)
}
func setupIssuer(t *testing.T) (string, string) {
app := issuer.NewApp(log.New(), &issuer.Config{
HTTPAddr: "127.0.0.1:0", // use random port
ISO8583Addr: "127.0.0.1:0", // use random port
})
err := app.Start()
require.NoError(t, err)
// dont' forget to shutdown the issuer app
t.Cleanup(app.Shutdown)
return fmt.Sprintf("http://%s", app.Addr), app.ISO8583ServerAddr
}
func setupAcquirer(t *testing.T, iso8583ServerAddr string) string {
app := acquirer.NewApp(log.New(), &acquirer.Config{
HTTPAddr: "127.0.0.1:0", // use random port
ISO8583Addr: iso8583ServerAddr,
})
err := app.Start()
require.NoError(t, err)
// dont' forget to shutdown the acquirer app
t.Cleanup(app.Shutdown)
return fmt.Sprintf("http://%s", app.Addr)
}