-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
116 lines (100 loc) · 3.13 KB
/
Copy pathexample_test.go
File metadata and controls
116 lines (100 loc) · 3.13 KB
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
package payvand_test
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/amiranmanesh/payvand"
"github.com/amiranmanesh/payvand/gateway/zibal"
)
// The virtual gateway keeps the examples runnable without a merchant account;
// swapping it for a real name is the only change a production program needs.
func Example() {
pv := payvand.Init(payvand.WithTimeout(20 * time.Second))
gw, err := pv.Gateway(payvand.Virtual, payvand.Config{MerchantKey: "merchant-key"})
if err != nil {
log.Fatal(err)
}
purchase, err := gw.Purchase(context.Background(), payvand.PurchaseRequest{
Amount: payvand.Toman(15_000),
OrderID: "1001",
CallbackURL: "https://shop.example/payments/callback",
Description: "Wallet top-up",
})
if err != nil {
log.Fatal(err)
}
fmt.Println("gateway:", gw.Name())
fmt.Println("amount:", purchase.Amount.Rial(), "Rial")
fmt.Println("redirect method:", purchase.Redirect.Method)
// Output:
// gateway: virtual
// amount: 150000 Rial
// redirect method: GET
}
// Provider specific options are ordinary [payvand.Option] values, so they
// compose with the shared ones in a single list.
func ExampleClient_Gateway() {
pv := payvand.Init(payvand.WithTimeout(15 * time.Second))
gw, err := pv.Gateway(payvand.Zibal, payvand.Config{MerchantKey: "zibal"},
payvand.WithSandbox(true),
zibal.WithFeeMode(1),
zibal.WithMultiplexing(zibal.Share{
BankAccount: "IR000000000000000000000001",
Amount: 50_000,
}),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(gw.Name(), gw.Capabilities().Multiplexing)
// Output: zibal true
}
// Operations a provider does not offer report [payvand.ErrNotSupported], so a
// caller can branch on capability instead of on provider name.
func ExampleGateway_refund() {
gw, err := payvand.New(payvand.Zarinpal, payvand.Config{MerchantKey: "merchant-id"})
if err != nil {
log.Fatal(err)
}
if !gw.Capabilities().Refund {
fmt.Println("refunds must be issued from the provider panel")
}
_, err = gw.Refund(context.Background(), payvand.RefundRequest{Token: "A1"})
fmt.Println(errors.Is(err, payvand.ErrNotSupported))
// Output:
// refunds must be issued from the provider panel
// true
}
// A callback handler is written once and works for every provider: the parsed
// callback carries the token, and the amount always comes from the merchant's
// own records.
func ExampleGateway_parseCallback() {
gw, err := payvand.New(payvand.Virtual, payvand.Config{})
if err != nil {
log.Fatal(err)
}
handler := func(w http.ResponseWriter, r *http.Request) {
callback, err := gw.ParseCallback(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if !callback.Succeeded {
http.Error(w, "the payer canceled the payment", http.StatusPaymentRequired)
return
}
// The amount is read from the order, never from the query string.
verified, err := gw.Verify(r.Context(), callback.VerifyRequest(payvand.Toman(15_000)))
if err != nil {
http.Error(w, err.Error(), http.StatusPaymentRequired)
return
}
fmt.Fprintln(w, "reference number:", verified.ReferenceNumber)
}
_ = handler
fmt.Println("ready")
// Output: ready
}