-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment.nim
322 lines (246 loc) · 9.81 KB
/
payment.nim
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import addtl_procs
import extras
import marshal
import pay
import times
import types
proc newPayment*(self:var Connection, `method`: PaymentMethod,
returnUrl, cancelUrl: string): PaymentRequest =
## Creates a new Payment.
## Use `.add()` to add Transactions to the Payment.
## When ready, use `.authorize()` to gain payment authorization and
## `.execute()` to execute the final, authorized payment.
if `method` == PaymentMethod.PayPal:
# Make sure we're still authenticated. Will refresh if not.
self.authenticate()
result = PaymentRequest(
intent: Intent.Sale,
redirect_urls: RedirectURLs (
return_url: return_url,
cancel_url: cancel_url,
),
payer: PayerBase(
payment_method: `method`, # PayPal
),
transactions: @[],
)
# TODO: Because some fields are only valid when `payment_method` is `paypal`, I
# should really have separate constructors for paypal vs non-paypal transactions.
#
## A PayPal payment sends a list of transactions. Use `newTransaction()` to
## create a single transaction and then its `addItem()` method to add amounts.
proc newTransaction*(currency: CurrencyType,
shipping: float64,
handling: float64,
insurance: float64,
shippingDiscount: float64,
description: string,
invoiceNumber: string,
custom: string,
softDescriptor: string,
address: ShippingAddress,
): Transaction =
# Total equals sum of subtotal, shipping, tax, handling and insurance minus
# the shippingDiscount.
result.amount = TransactionAmount(
currency: currency,
# total: calculated from `details` fields
details: TransactionDetails(
# tax: calculated from `item_list` members
# subtotal: calculated from `item_list` members
shipping: shipping,
handling_fee: handling,
insurance: insurance,
shipping_discount: shippingDiscount,
# This field would be under `item_list`, except that it's needed for the
# `tax` and `subtotal` calculations, so it's here instead with an
# `item_list` proc in its place on the `Transaction` object.
item_list: ItemList(
shipping_address: address,
items: @[],
)
),
)
result.invoice_number = invoiceNumber
result.custom = custom
result.soft_descriptor = softDescriptor
## Add an individual amount to a Transaction. This is similar to a line item on
## an invoice.
## The currency type is whatever was assigned to the Transaction object.
proc addItem*(self:var Transaction, quantity: Natural,
price, tax: float64,
name, description, sku: string) =
self.amount.details.item_list.items.add(Item(quantity: quantity,
name: name,
price: price,
sku: sku,
currency: self.amount.currency,
description: description,
tax: tax))
# This is used because the `item_list` field of the `Transaction` object is
# actually held in the `TransactionDetails` held by `TransactionAmount`.
proc item_list*(self: Transaction): ItemList =
self.amount.details.item_list
proc tax(self:TransactionDetails): float64 =
for item in self.item_list.items:
result += item.tax
proc subtotal(self:TransactionDetails): float64 =
for item in self.item_list.items:
result += float64(item.quantity) * item.price
proc total(self: TransactionAmount): float64 =
result = (self.details.tax() +
self.details.subtotal() +
self.details.shipping +
self.details.handling_fee +
self.details.insurance) - self.details.shipping_discount
proc total(self: Amount): float64 =
result = (self.details.tax +
self.details.subtotal +
self.details.shipping +
self.details.handling_fee +
self.details.insurance) - self.details.shipping_discount
proc add*(self:var PaymentRequest, trans: varargs[Transaction]) =
## Add `Transaction` objects before making the request.
for t in trans:
self.transactions.add(t)
proc send*(self:var Connection, pymtReq: PaymentRequest): Link =
# When there's a valid response, redirect the user to the given
# "approval_url".
var pymtResp = pymtReq.toSub(Payment)
self.make_request(RType.PaypalPayment, pymtReq, pymtResp)
if pymtResp.state == PaymentState.created:
# Return URL info for the user to redirect to for approval
return pymtResp.links.get("approval_url")
else:
raise new Exception # TODO: UnexpectedResponse
proc execute*(self: Connection, query: string) =
# TODO: complete this method. It's all fictional right now.
let query = query.parse()
if query.isNil:
raise new Exception # Query params not valid
var payerid = query.Get("PayerID")
if payerid == "":
raise new Exception # Query params not valid
if pymt.isNil:
return fmt.Errorf("Payment Object is missing\n")
var pathname = "payments/payment" / pymtid / "execute"
self.make_request("POST", pathname, "{\"payer_id\":\"" & payerid & "\"}",
"execute_", pymt, false)
if pymt.GetState() != Approved:
#/*
# var s, err = json.Marshal(pymt)
# if err != nil {
# return fmt.Errorf("JSON marshal error\n")
# }
# fmt.Println(string(s))
#*/
raise new Exception # Payment not approved
# Pagination
# Assuming `start_time`, `start_index` and `start_id` are mutually exclusive
# ...going to treat them that way anyhow until I understand better.
# I'm going to ignore `start_index` for now since I don't see its usefulness
proc getAll*(self: Connection, size: range[0..20], sortBy: SortBy,
sortOrder: SortOrder, timeRange: varargs[Time]): PaymentBatcher =
var qry = fmt.Sprintf("?sort_order=%s&sort_by=%s&count=%d", sort_by, sort_order, size)
if len(time_range) > 0:
if time_range[0].IsZero() == false:
qry = fmt.Sprintf("%s&start_time=%s", qry, time_range[0].Format(time.RFC3339))
if len(time_range) > 1 and time_range[1].After(time_range[0]):
qry = fmt.Sprintf("%s&end_time=%s", qry, time_range[1].Format(time.RFC3339))
return PaymentBatcher(
base_query: qry,
next_id: "",
done: false,
connection: connection,
)
#/****************************************
#
# PaymentBatcher
#
#Manages paginated requests for Payments
#
#*****************************************/
proc isDone* (self: PaymentBatcher): bool =
return self.done
# TODO: Should `.next()` take an optional filter function?
proc next (self:var PaymentBatcher): ([]Payment, error) =
if self.done:
return nil, ErrNoResults
var pymt_list = new(payment_list)
var qry = self.base_query
if self.next_id != "":
qry = fmt.Sprintf("%s&start_id=%s", qry, self.next_id)
var err = self.connection.make_request("GET", "payments/payment"+qry, nil,
"", pymt_list, false)
if not err.isNil:
return nil, err
if pymt_list.Count == 0:
self.done = true
self.next_id = ""
return nil, ErrNoResults
self.next_id = pymt_list.Next_id
if self.next_id == "":
self.done = true
return pymt_list.Payments, nil
# These provide a way to both get and set the `next_id`.
# This gives the ability to cache the ID, and then set it in a new Batcher.
# Useful if a session is not desired or practical
proc getNextId*(self: PaymentBatcher): string =
return self.next_id
proc setNextId*(self:var PaymentBatcher, id: string) =
self.next_id = id
proc parseRawData*(self: Payments, rawdata: string): Payment =
var po: Payment
var err = json.Unmarshal(rawdata, &po)
if not err.isNil:
return nil
result = &po
# TODO: Should this hold the `execute` path so that it doesn't need to be constructed in `Execute()`?
proc getState*(self: PaymentExecutor): State =
return self.State
proc getId*(self: PaymentExecutor): string =
return self.Id
proc getPayerID*(self: PaymentExecutor): string =
return self.PayerID
proc execute*(self: PaymentExecutor, r: *http.Request): error =
return self.payments.Execute(self, r)
#/***************************
#
# Payment object methods
#
#***************************/
proc getState*(self: Payment): State =
return self.State
proc getId*(self: Payment): string =
return self.Id
proc getPayerID*(self: Payment): string =
if not self.isNil and self.Payer.Payer_info != nil:
return self.Payer.Payer_info.Payer_id
return ""
proc makeExecutor*(self: Payment): PaymentExecutor =
return &PaymentExecutor{
id*: self.Id,
state*: self.State,
payments: self.payments,
}
proc addItem*(t: Transaction, qty: uint, price: float64, curr: CurrencyType,
name, sku: string) =
if t.item_list.isNil:
t.item_list = new(item_list)
t.item_list.items.add(Item(
quantity: qty,
name: name,
price: price,
currency: curr.currency_type(),
sku: sku,
))
# TODO: I'm returning a list of Sale objects because every payment can have multiple transactions.
# I need to find out why a payment can have multiple transactions, and see if I should eliminate that in the API
# Also, I need to find out why `related_resources` is an array. Can there be more than one per type?
proc getSale*(self: Payment): seq[SaleObject] =
var sales = []*SaleObject{}
for transaction in self.Transactions:
for related_resource in transaction.related_resources:
if not related_resource.sale.isNil:
sales.add(related_resource.Sale)
return sales