-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneoff.go
80 lines (62 loc) · 2.05 KB
/
oneoff.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
package everypay
import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/google/uuid"
)
// Data to initiate payment
type OneOff struct {
// Transaction amount.
// The currency is taken from the specified processing account.
// Can be also set as 0 for card verification (saving card for further token payments).
Amount float32 `json:"amount"`
// Unique order reference.
OrderReference string `json:"order_reference"`
// When description is provided, it will be used with open banking payment methods.
Description string `json:"payment_description,omitempty"`
// Customer’s email for fraud prevention.
CustomerEmail string `json:"email"`
// Customer’s IP address for fraud prevention.
CustomerIp string `json:"customer_ip"`
// URL where the Customer should be redirected after completing the payment.
CustomerUrl string `json:"customer_url"`
// It must be sent when RequestToken defined.
// It is the type of the agreement:
// - `unscheduled`
// - `recurring`
TokenAgreement string `json:"token_agreement,omitempty"`
// Token should be returned in the response for future usage
RequestToken string `json:"request_token,omitempty"`
}
type oneOffRequest struct {
*OneOff
ApiUsername string `json:"api_username"`
AccountName string `json:"account_name"`
Nonce string `json:"nonce"`
Timestamp time.Time `json:"timestamp"`
}
type oneOffResponse struct {
OrderReference string `json:"order_reference"`
PaymentReference string `json:"payment_reference"`
PaymentLink string `json:"payment_link"`
}
// InitialPayment returns the payment link
func (e *Everypay) InitialPayment(o *OneOff) (string, error) {
u := &url.URL{
Path: "payments/oneoff",
}
requestData := &oneOffRequest{
OneOff: o,
ApiUsername: e.username,
AccountName: e.account,
Nonce: uuid.NewString(),
Timestamp: time.Now().UTC(),
}
responseData := &oneOffResponse{}
if err := e.request(http.MethodPost, u, requestData, responseData); err != nil {
return "", fmt.Errorf("initial payment: %w", err)
}
return responseData.PaymentLink, nil
}