-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpis.go
156 lines (147 loc) · 3.63 KB
/
pis.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/google/uuid"
"io"
"io/ioutil"
"net/http"
)
const configPath = "../config.json"
const APIOrigin = "https://api.enablebanking.com"
const ASPSPName = "S-Pankki"
const ASPSPCountry = "FI"
func main() {
config := Config{}
configFile, err := ioutil.ReadFile(configPath)
err = json.Unmarshal(configFile, &config)
if err != nil {
panic(err)
}
jwt, err := getJwt(config.KeyPath, config.ApplicationId)
if err != nil {
panic(err)
}
client := &http.Client{}
request, err := http.NewRequest("GET", APIOrigin+"/application", nil)
if err != nil {
panic(err)
}
request.Header.Add("Authorization", "Bearer "+jwt)
response, err := client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
fmt.Print("Application details: ")
fmt.Println(string(body))
request, err = http.NewRequest("GET", APIOrigin+"/aspsps", nil)
if err != nil {
panic(err)
}
request.Header.Add("Authorization", "Bearer "+jwt)
response, err = client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err = ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
// Commented out, because output is too long
//fmt.Print("ASPSP list: ")
//fmt.Println(string(body))
// Initiating a payment
paymentBody := map[string]interface{}{
"payment_type": "SEPA",
"payment_request": map[string]interface{}{
"credit_transfer_transaction": []interface{}{
map[string]interface{}{
"beneficiary": map[string]interface{}{
"creditor_account": map[string]string{
"scheme_name": "IBAN",
"identification": "FI7473834510057469",
},
"creditor": map[string]interface{}{
"name": "John Doe",
},
},
"instructed_amount": map[string]string{
"currency": "EUR",
"amount": "100.00",
},
"reference_number": "123",
},
},
},
"aspsp": map[string]string{
"name": ASPSPName,
"country": ASPSPCountry,
},
"state": uuid.NewString(),
"redirect_url": config.RedirectUrl,
"psu_type": "personal",
}
paymentBodyJson, err := json.Marshal(paymentBody)
if err != nil {
panic(err)
}
request, err = http.NewRequest("POST", APIOrigin+"/payments", bytes.NewBuffer(paymentBodyJson))
if err != nil {
panic(err)
}
request.Header.Add("Authorization", "Bearer "+jwt)
response, err = client.Do(request)
if err != nil {
panic(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(response.Body)
body, err = io.ReadAll(response.Body)
if err != nil {
panic(err)
}
responseJson := struct {
Url string `json:"url"`
PaymentId string `json:"payment_id"`
}{}
err = json.Unmarshal(body, &responseJson)
if err != nil {
panic(err)
}
fmt.Println("Use following credentials to authenticate: customera / 12345678")
fmt.Print("To authenticate open URL: ")
fmt.Println(responseJson.Url)
// This request can be called multiple times to check the status of the payment
paymentStatusRequest, err := http.NewRequest("GET", APIOrigin+"/payments/"+responseJson.PaymentId, nil)
if err != nil {
panic(err)
}
paymentStatusRequest.Header.Add("Authorization", "Bearer "+jwt)
paymentStatusResponse, err := client.Do(paymentStatusRequest)
if err != nil {
panic(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(paymentStatusResponse.Body)
paymentStatusBody, err := io.ReadAll(paymentStatusResponse.Body)
if err != nil {
panic(err)
}
fmt.Print("Payment status: ")
fmt.Println(string(paymentStatusBody))
}