1
|
|
|
package main |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"log" |
6
|
|
|
"os" |
7
|
|
|
"strconv" |
8
|
|
|
|
9
|
|
|
"github.com/VictorAvelar/mollie-api-go/v2/mollie" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
const ( |
13
|
|
|
MyLocale = "es_ES" |
14
|
|
|
VAT = "19.00" |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
var ( |
18
|
|
|
client *mollie.Client |
19
|
|
|
) |
20
|
|
|
|
21
|
|
|
func init() { |
22
|
|
|
config := mollie.NewConfig(false, mollie.APITokenEnv) |
23
|
|
|
m, err := mollie.NewClient(nil, config) |
24
|
|
|
if err != nil { |
25
|
|
|
print(err) |
26
|
|
|
os.Exit(1) |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
client = m |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
func main() { |
33
|
|
|
var order mollie.Order |
34
|
|
|
{ |
35
|
|
|
addr := &mollie.OrderAddress{ |
36
|
|
|
GivenName: "John", |
37
|
|
|
FamilyName: "Doe", |
38
|
|
|
Email: "[email protected]", |
39
|
|
|
StreetAndNumber: "C. d'Arístides Maillol, 12", |
40
|
|
|
PostalCode: "08028", |
41
|
|
|
City: "Barcelona", |
42
|
|
|
Country: "ES", |
43
|
|
|
} |
44
|
|
|
order = mollie.Order{ |
45
|
|
|
OrderNumber: "myRandUniqueOrderNumber-1023128312", |
46
|
|
|
Locale: MyLocale, |
47
|
|
|
BillingAddress: addr, |
48
|
|
|
ShippingAddress: *addr, // if different just construct a new address object. |
49
|
|
|
RedirectURL: "https://github.com/VictorAvelar/mollie-api-go", |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
product := createOrderLine( |
53
|
|
|
"Macbook PRO 2021 - M1 Spacegray", |
54
|
|
|
1, |
55
|
|
|
createAmount("1299.00", "EUR"), |
56
|
|
|
) |
57
|
|
|
|
58
|
|
|
order.Lines = []*mollie.OrderLine{product} |
59
|
|
|
order.Method = mollie.CreditCard |
60
|
|
|
order.Amount = product.TotalAmount |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
ord, err := client.Orders.Create(order, nil) |
64
|
|
|
if err != nil { |
65
|
|
|
panic(err) |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
fmt.Printf("Order created: Checkout at %s\n", ord.Links.Checkout.Href) |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
func createOrderLine(name string, quantity int, unitPrice *mollie.Amount) *mollie.OrderLine { |
72
|
|
|
return &mollie.OrderLine{ |
73
|
|
|
Name: name, |
74
|
|
|
Quantity: quantity, // quantity |
75
|
|
|
VatRate: VAT, // Tax rate as string of form 19.00 for 19%. |
76
|
|
|
VatAmount: calculateVatAmount(VAT, unitPrice), // Calculate vat amount from gross unit price. |
77
|
|
|
UnitPrice: unitPrice, // gross unit price. |
78
|
|
|
TotalAmount: unitPrice, // in case of single units the same as unit price, otherwise the sum of gross unit prices. |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
func createAmount(value, currency string) *mollie.Amount { |
83
|
|
|
return &mollie.Amount{ |
84
|
|
|
Value: value, |
85
|
|
|
Currency: currency, |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
func calculateVatAmount(rate string, rawPrice *mollie.Amount) (vat *mollie.Amount) { |
90
|
|
|
vatRateF := parseStrToFloat(VAT) |
91
|
|
|
rawPriceF := parseStrToFloat(rawPrice.Value) |
92
|
|
|
|
93
|
|
|
netPrice := rawPriceF / (float64(1) + (vatRateF / float64(100))) |
94
|
|
|
|
95
|
|
|
return &mollie.Amount{ |
96
|
|
|
Value: parseFloatToStr(rawPriceF - netPrice), |
97
|
|
|
Currency: rawPrice.Currency, |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
func parseStrToFloat(value string) float64 { |
102
|
|
|
val, err := strconv.ParseFloat(value, 64) |
103
|
|
|
if err != nil { |
104
|
|
|
log.Printf("%+v", err) |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return val |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
func parseFloatToStr(value float64) string { |
111
|
|
|
return fmt.Sprintf("%.2f", value) |
112
|
|
|
} |
113
|
|
|
|