This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sbp_test.go
98 lines (85 loc) · 2.23 KB
/
sbp_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
package qiwi
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestSBP(t *testing.T) {
const SBPLink = "https://qr.nspk.ru/SOMEUID?type=02&sum=405&cur=RUB&crc=5C01D"
const errResp = `{
"serviceName" : "payin-core",
"errorCode" : "validation.sbptest",
"description" : %q,
"userMessage" : "Validation error",
"dateTime" : "2018-11-13T16:49:59.166+03:00",
"traceId" : "fd0e2a08c63ace83"
}`
// HTTP MOCK
serv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
var buf bytes.Buffer
var p Payment
_, err = io.Copy(&buf, r.Body) //nolint
if err != nil {
fmt.Fprintf(w, errResp, err)
}
err = json.Unmarshal(buf.Bytes(), &p)
if err != nil {
fmt.Fprintf(w, errResp, err)
}
if p.PaymentMethod.Type != SBPPayment {
fmt.Fprintf(w, errResp, "Wrong payment method for SBP")
t.Error("Wrong payment method for SBP")
}
if p.Amount.Currency != rub {
fmt.Fprintf(w, errResp, "SBP accept only RUB as currency")
t.Error("SBP accept only RUB as currency")
}
fmt.Fprintln(w, `{
"paymentId": "sbp-test-18",
"billId": "autogenerated-billid",
"createdDateTime": "2020-12-11T11:09:48+03:00",
"amount": {
"currency": "RUB",
"value": "4.05"
},
"capturedAmount": "..",
"refundedAmount": "..",
"paymentMethod": {
"type": "SBP"
},
"requirements": {
"sbp": {
"qrcId": "SOMWUID",
"image": {
"mediaType": "image/png",
"content": "BASE64IMAGE"
},
"payload": "https://qr.nspk.ru/SOMEUID?type=02&sum=405&cur=RUB&crc=5C01D"
}
},
"status": {
"value": "WAITING",
"changedDateTime": "2020-12-11T11:09:49+03:00"
}
}`)
}))
serv.Start()
defer serv.Close()
pay := New("OrderID", "BillID", "TOKEN", serv.URL)
err := pay.SBP(context.TODO(), 405, "") // Request payment for 4.05RUB via SBP
if pay.PaymentMethod.Type != SBPPayment {
t.Errorf("Wrong payment type %s", pay.PaymentMethod.Type)
}
if pay.Req.SBP.PayURL != SBPLink {
t.Errorf("SBP fail to provide correct payment link: %s, want %s", pay.Req.SBP.PayURL, SBPLink)
}
if err != nil {
t.Errorf("SBP error: %s", err)
}
}