1
|
|
|
// Example app to show the following Mollie features: |
2
|
|
|
// - Get customer |
3
|
|
|
// - Update customer |
4
|
|
|
// - Delete customer |
5
|
|
|
// - List customers// |
6
|
|
|
// - Create customer payment |
7
|
|
|
// - List customer payments |
8
|
|
|
package main |
9
|
|
|
|
10
|
|
|
import ( |
11
|
|
|
"fmt" |
12
|
|
|
"github.com/VictorAvelar/mollie-api-go/mollie" |
13
|
|
|
"log" |
14
|
|
|
"os" |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
func main() { |
18
|
|
|
fmt.Printf("Connecting to Mollie....\n") |
19
|
|
|
m := CreateMollie() |
20
|
|
|
|
21
|
|
|
//create customer |
22
|
|
|
cs := CreateCustomer(m) |
23
|
|
|
|
24
|
|
|
// get customer |
25
|
|
|
cs = GetCustomer(cs, m) |
26
|
|
|
|
27
|
|
|
//update customer |
28
|
|
|
UpdateCustomer(cs, m) |
29
|
|
|
|
30
|
|
|
//list all customers |
31
|
|
|
ListAllCustomers(m) |
32
|
|
|
|
33
|
|
|
//create payment for a single customer |
34
|
|
|
CreateCustomerPayment(cs, m) |
35
|
|
|
|
36
|
|
|
//get all payments for a particular customer |
37
|
|
|
GetCustomerPayments(cs, m) |
38
|
|
|
|
39
|
|
|
os.Exit(0) |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
//GetCustomerPayments extract all payments for a particular customer from mollie |
43
|
|
|
func GetCustomerPayments(cs *mollie.Customer, m *mollie.Client) { |
44
|
|
|
// List customer payment |
45
|
|
|
fmt.Printf("Listing customer ID %s payments\n", cs.ID) |
46
|
|
|
pl, err := m.Customers.GetPayments(cs.ID, nil) |
47
|
|
|
if err != nil { |
48
|
|
|
log.Fatal(err) |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
fmt.Printf("....customer with id %s has %d payments", cs.ID, pl.Count) |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
//CreateCustomerPayment create a single payment for a particular customer from mollie |
55
|
|
|
func CreateCustomerPayment(cs *mollie.Customer, m *mollie.Client) { |
56
|
|
|
// Create customer payment |
57
|
|
|
fmt.Printf("Creating payment for customer with id %s \n", cs.ID) |
58
|
|
|
var bt = mollie.BankTransfer |
59
|
|
|
|
60
|
|
|
var p = mollie.Payment{ |
61
|
|
|
Amount: &mollie.Amount{ |
62
|
|
|
Currency: "EUR", |
63
|
|
|
Value: "10.12", |
64
|
|
|
}, |
65
|
|
|
Description: "Order #12345", |
66
|
|
|
Method: &bt, |
67
|
|
|
RedirectURL: "https://webshop.example.org/order/12345/", |
68
|
|
|
WebhookURL: "https://webshop.example.org/payments/webhook/", |
69
|
|
|
} |
70
|
|
|
pp, err := m.Customers.CreatePayment(cs.ID, p) |
71
|
|
|
if err != nil { |
72
|
|
|
log.Fatal(err) |
73
|
|
|
} |
74
|
|
|
fmt.Printf("....created new payment with the following info - ID %s, currency : %s, "+ |
75
|
|
|
"value : %s ", |
76
|
|
|
pp.ID, pp.Amount.Currency, pp.Amount.Value) |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
//ListAllCustomers list all customers from mollie |
80
|
|
|
func ListAllCustomers(m *mollie.Client) { |
81
|
|
|
fmt.Printf("List all customers....\n") |
82
|
|
|
output, err := m.Customers.List(nil) |
83
|
|
|
if err != nil { |
84
|
|
|
log.Fatal(err) |
85
|
|
|
} |
86
|
|
|
fmt.Printf("....total number customer - %d\n", output.Count) |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
//UpdateCustomer update email of a particular customer from mollie |
90
|
|
|
func UpdateCustomer(cs *mollie.Customer, m *mollie.Client) { |
91
|
|
|
fmt.Printf("Updating customer with id %s\n", cs.ID) |
92
|
|
|
var uc = mollie.Customer{ |
93
|
|
|
Email: "[email protected]", |
94
|
|
|
} |
95
|
|
|
cs, err := m.Customers.Update(cs.ID, uc) |
96
|
|
|
if err != nil { |
97
|
|
|
log.Fatal(err) |
98
|
|
|
} |
99
|
|
|
fmt.Printf("....existing customer ID %s email has been updated to %s", cs.ID, cs.Email) |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//GetCustomer extract a particular customer from mollie |
103
|
|
|
func GetCustomer(cs *mollie.Customer, m *mollie.Client) *mollie.Customer { |
104
|
|
|
//2. Get customer |
105
|
|
|
fmt.Printf("Retrieving customer information with id %s\n", cs.ID) |
106
|
|
|
cs, err := m.Customers.Get(cs.ID) |
107
|
|
|
if err != nil { |
108
|
|
|
log.Fatal(err) |
109
|
|
|
} |
110
|
|
|
fmt.Printf("....existing customer - name %s, ID %s, email %s\n", cs.Name, cs.ID, cs.Email) |
111
|
|
|
return cs |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
//CreateCustomer create a single customer from mollie |
115
|
|
|
func CreateCustomer(m *mollie.Client) *mollie.Customer { |
116
|
|
|
var c = mollie.Customer{ |
117
|
|
|
Name: "testkingkong", |
118
|
|
|
Email: "[email protected]", |
119
|
|
|
} |
120
|
|
|
fmt.Printf("Creating new customer with the following info - name : %s , email : %s\n", c.Name, |
121
|
|
|
c.Email) |
122
|
|
|
cs, err := m.Customers.Create(c) |
123
|
|
|
if err != nil { |
124
|
|
|
log.Fatal(err) |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
fmt.Printf("....new customer created has ID %s\n", cs.ID) |
128
|
|
|
return cs |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// CreateMollie create a mollie connection |
132
|
|
|
func CreateMollie() *mollie.Client { |
133
|
|
|
config := mollie.NewConfig(false, mollie.APITokenEnv) |
134
|
|
|
m, err := mollie.NewClient(nil, config) |
135
|
|
|
if err != nil { |
136
|
|
|
log.Fatal(err) |
137
|
|
|
} |
138
|
|
|
return m |
139
|
|
|
} |
140
|
|
|
|