|
1
|
|
|
package coinpayments |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"io/ioutil" |
|
6
|
|
|
"net/http" |
|
7
|
|
|
"net/url" |
|
8
|
|
|
"testing" |
|
9
|
|
|
|
|
10
|
|
|
"github.com/NdoleStudio/coinpayments-go/internal/helpers" |
|
11
|
|
|
"github.com/NdoleStudio/coinpayments-go/internal/stubs" |
|
12
|
|
|
"github.com/stretchr/testify/assert" |
|
13
|
|
|
) |
|
14
|
|
|
|
|
15
|
|
|
func TestPaymentService_CreateTransaction_Request(t *testing.T) { |
|
16
|
|
|
// Setup |
|
17
|
|
|
t.Parallel() |
|
18
|
|
|
|
|
19
|
|
|
// Arrange |
|
20
|
|
|
request := new(http.Request) |
|
21
|
|
|
key := "test-api-key" |
|
22
|
|
|
secret := "test-api-secret" |
|
23
|
|
|
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.CreatePaymentsOkResponse(), request) |
|
24
|
|
|
client := New(WithBaseURL(server.URL), WithAPIKey(key), WithAPISecret(secret)) |
|
25
|
|
|
params := &CreatePaymentRequest{ |
|
26
|
|
|
Amount: "1.00000000", |
|
27
|
|
|
OriginalCurrency: "USD", |
|
28
|
|
|
SendingCurrency: "USD", |
|
29
|
|
|
BuyerEmail: "[email protected]", |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
expectedHMAC := "e3dc88dea6ead6e8e7cef2f1aecec0a1a66f4ada8af0289d03b04e73ed1e218e842ce553938e1c9461767c5459ddac5c611ca3e9622c2511e156d71d097b9563" |
|
33
|
|
|
|
|
34
|
|
|
expectedRequest := url.Values{} |
|
35
|
|
|
expectedRequest.Add("amount", params.Amount) |
|
36
|
|
|
expectedRequest.Add("currency1", params.OriginalCurrency) |
|
37
|
|
|
expectedRequest.Add("currency2", params.SendingCurrency) |
|
38
|
|
|
|
|
39
|
|
|
// Act |
|
40
|
|
|
_, _, err := client.Payment.CreateTransaction(context.Background(), params) |
|
41
|
|
|
|
|
42
|
|
|
// Assert |
|
43
|
|
|
assert.Nil(t, err) |
|
44
|
|
|
assert.NotNil(t, request) |
|
45
|
|
|
|
|
46
|
|
|
assert.Equal(t, expectedHMAC, request.Header.Get("HMAC")) |
|
47
|
|
|
|
|
48
|
|
|
buf, err := ioutil.ReadAll(request.Body) |
|
49
|
|
|
assert.NoError(t, err) |
|
50
|
|
|
|
|
51
|
|
|
assert.Equal(t, expectedRequest.Encode(), string(buf)) |
|
52
|
|
|
|
|
53
|
|
|
// Teardown |
|
54
|
|
|
server.Close() |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
func TestPaymentService_CreateTransaction_Ok(t *testing.T) { |
|
58
|
|
|
// Setup |
|
59
|
|
|
t.Parallel() |
|
60
|
|
|
|
|
61
|
|
|
// Arrange |
|
62
|
|
|
key := "test-api-key" |
|
63
|
|
|
secret := "test-api-secret" |
|
64
|
|
|
server := helpers.MakeTestServer(http.StatusOK, stubs.CreatePaymentsOkResponse()) |
|
65
|
|
|
client := New(WithBaseURL(server.URL), WithAPIKey(key), WithAPISecret(secret)) |
|
66
|
|
|
params := &CreatePaymentRequest{ |
|
67
|
|
|
Amount: "1.00000000", |
|
68
|
|
|
OriginalCurrency: "USD", |
|
69
|
|
|
SendingCurrency: "USD", |
|
70
|
|
|
BuyerEmail: "[email protected]", |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Act |
|
74
|
|
|
transactionResponse, apiResponse, err := client.Payment.CreateTransaction(context.Background(), params) |
|
75
|
|
|
|
|
76
|
|
|
// Assert |
|
77
|
|
|
assert.Nil(t, err) |
|
78
|
|
|
assert.Equal(t, http.StatusOK, apiResponse.HTTPResponse.StatusCode) |
|
79
|
|
|
|
|
80
|
|
|
assert.Equal(t, &CreatePaymentResponse{ |
|
81
|
|
|
Error: "ok", |
|
82
|
|
|
Result: CreatePaymentResult{ |
|
83
|
|
|
Amount: "1.00000000", |
|
84
|
|
|
Address: "ZZZ", |
|
85
|
|
|
DestTag: "YYY", |
|
86
|
|
|
TransactionID: "XXX", |
|
87
|
|
|
ConfirmsNeeded: "10", |
|
88
|
|
|
Timeout: 9000, |
|
89
|
|
|
CheckoutURL: "https://www.coinpayments.net/index.php?cmd=checkout&id=XXX&key=ZZZ", |
|
90
|
|
|
StatusURL: "https://www.coinpayments.net/index.php?cmd=status&id=XXX&key=ZZZ", |
|
91
|
|
|
QrcodeURL: "https://www.coinpayments.net/qrgen.php?id=XXX&key=ZZZ", |
|
92
|
|
|
}, |
|
93
|
|
|
}, transactionResponse) |
|
94
|
|
|
|
|
95
|
|
|
// Teardown |
|
96
|
|
|
server.Close() |
|
97
|
|
|
} |
|
98
|
|
|
|