This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_transaction.go
87 lines (71 loc) · 1.87 KB
/
create_transaction.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
package idpay
import (
"bytes"
"encoding/json"
"net/http"
)
type NewTransaction struct {
ID string `json:"id"`
Link string `json:"link"`
}
type TransactionOptions struct {
// Name is Payer name. up to 255 characters long accepted.
Name string
// Phone is payer phone number. should be 11 characters long.
// valid examples: 9382198592, 09382198592, 989382198592
Phone string
// Mail is payer mail. up to 255 characters long accepted.
Mail string
// Description is transaction's description. up to 255 characters long accepted.
Description string
}
// CreateTransaction created new idpay transaction
//
// orderID: Acceptor's order number. up to 50 characters.
//
// callbackURL:
//
// amount: The desired amount is in Rials. it must be between 1,000 and 500,000,000 Rials.
func (i IDPay) CreateTransaction(orderID, callbackURL string, amount int, opts *TransactionOptions) (*NewTransaction, error) {
body := map[string]interface{}{
"order_id": orderID,
"amount": amount,
"callback": callbackURL,
}
if opts != nil {
if opts.Name != "" {
body["name"] = opts.Name
}
if opts.Phone != "" {
body["phone"] = opts.Phone
}
if opts.Mail != "" {
body["mail"] = opts.Mail
}
if opts.Description != "" {
body["desc"] = opts.Description
}
}
payload, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, HOST+"/payment", bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-KEY", i.apiKey)
req.Header.Set("X-SANDBOX", i.IsSandbox())
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return nil, getError(resp.Body)
}
data := &NewTransaction{}
err = json.NewDecoder(resp.Body).Decode(data)
return data, err
}