-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_response_transformer.go
121 lines (103 loc) · 3.27 KB
/
transaction_response_transformer.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
package sofortpay
import (
"encoding/json"
"net/http"
"net/url"
"strings"
"github.com/google/uuid"
)
// Extract Transaction from response body
func prepareTransaction(r *http.Response) (*Transaction, error) {
defer r.Body.Close()
var data struct {
Amount float64 `json:"amount"`
CurrencyID string `json:"currency_id"`
Purpose string `json:"purpose"`
Metadata map[string]interface{} `json:"metadata"`
Language string `json:"language"`
Sender struct {
Holder string `json:"holder"`
Iban string `json:"iban"`
Bic string `json:"bic"`
BankName string `json:"bank_name"`
CountryID string `json:"country_id"`
} `json:"sender"`
SuccessURL string `json:"success_url"`
AbortURL string `json:"abort_url"`
WebhookURL string `json:"webhook_url"`
PayFormCode string `json:"payform_code"`
Recipient struct {
Holder string `json:"holder"`
Iban string `json:"iban"`
Bic string `json:"bic"`
BankName string `json:"bank_name"`
CountryID string `json:"country_id"`
Street string `json:"street"`
City string `json:"city"`
Zip string `json:"zip"`
} `json:"recipient"`
UUID string `json:"uuid"`
Status string `json:"status"`
TestMode bool `json:"testmode"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
return nil, err
}
uuiID, err := uuid.Parse(data.UUID)
if err != nil {
return nil, err
}
data.SuccessURL = strings.TrimSpace(data.SuccessURL)
data.AbortURL = strings.TrimSpace(data.AbortURL)
data.WebhookURL = strings.TrimSpace(data.WebhookURL)
var successURL *url.URL
var abortURL *url.URL
var webhookURL *url.URL
if data.SuccessURL != "" {
successURL, err = url.Parse(data.SuccessURL)
if err != nil {
return nil, err
}
}
if data.AbortURL != "" {
abortURL, err = url.Parse(data.AbortURL)
if err != nil {
return nil, err
}
}
if data.WebhookURL != "" {
webhookURL, err = url.Parse(data.WebhookURL)
if err != nil {
return nil, err
}
}
transaction := new(Transaction)
transaction.uuid = &uuiID
transaction.amount = data.Amount
transaction.currencyID = data.CurrencyID
transaction.purpose = data.Purpose
transaction.metadata = data.Metadata
transaction.language = data.Language
transaction.payFormCode = data.PayFormCode
transaction.status = data.Status
transaction.isTestMode = data.TestMode
transaction.successURL = successURL
transaction.abortURL = abortURL
transaction.webhookURL = webhookURL
transaction.sender = new(Sender)
transaction.sender.holder = data.Sender.Holder
transaction.sender.iban = data.Sender.Iban
transaction.sender.bic = data.Sender.Bic
transaction.sender.bankName = data.Sender.BankName
transaction.sender.countryID = data.Sender.CountryID
transaction.recipient = new(Recipient)
transaction.recipient.holder = data.Recipient.Holder
transaction.recipient.iban = data.Recipient.Iban
transaction.recipient.bic = data.Recipient.Bic
transaction.recipient.bankName = data.Recipient.BankName
transaction.recipient.countryID = data.Recipient.CountryID
transaction.recipient.street = data.Recipient.Street
transaction.recipient.city = data.Recipient.City
transaction.recipient.zip = data.Recipient.Zip
return transaction, nil
}