-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrl_braintree-1.0.tm
More file actions
459 lines (422 loc) · 15.7 KB
/
rl_braintree-1.0.tm
File metadata and controls
459 lines (422 loc) · 15.7 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# rl_braintree.tcl
#
# A tcl implementation of the Braintree server side functions,
# based on those as defined in their published Ruby SDK
#
# Copyright (c) 2009-2014 Braintree, a division of PayPal, Inc.
# Copyright (c) 2015 Ruby Lane
#
# See the file LICENSE for the information on the usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES
#
package require Tcl 8.6
package require sha1
package require tdom
package require rl_http
catch { namespace delete ::rl_braintree }
catch { rename ::rl_braintree {} }
oo::class create rl_braintree {
variable {*}{
config
headers
query_path
query_body
}
constructor args { #<<<
set config [dict merge {
-sandbox 0 \
} $args]
if {[self next] ne ""} next
dict for {k v} $config {
if {[string index $k 0] ne "-"} {
throw {SYNTAX GENERAL} "Invalid property name \"$k\""
}
}
# Must receive the public and private key to be used
foreach reqf {
merchant_id
public_key
private_key
} {
if {![dict exists $config -$reqf]} {
throw [list missing_field $reqf] "Must set -$reqf"
}
}
set query_body ""
set basic_auth [binary encode base64 "[dict get $config -public_key]:[dict get $config -private_key]"]
set headers [dict create \
"Authorization" "Basic $basic_auth" \
"X-ApiVersion" 4 \
"Content-Type" "application/xml; charset=utf-8" \
]
}
#>>>
method base_merchant_path {} { #<<<
return [file join "merchants" [dict get $config -merchant_id]]
}
#>>>
method config_key { key } { #<<<
if {[dict exists $config -$key]} {
set val [dict get $config -$key]
} else {
set val ""
}
}
#>>>
method generate_client_token {} { #<<<
my _set_query_path "client_token"
set query_body [my _build_query_body [list version] {
version { type integer } { t 1 }
}]
set token ""
set resp [my _parse_xml_response [my _send_request]]
if {[dict exists $resp value]} {
# Debug why this is not returned already encoded.
set token [binary encode base64 [dict get $resp value]]
}
set token
}
#>>>
method merchant_account { action data } { #<<<
# Full details available at https://developers.braintreepayments.com/javascript+ruby/guides/marketplace/create
if {[dict size $data] eq 0} {
throw [list "merchant account invalid data"] "No data"
}
# Field validation should be handled prior to calling this
# procedure.
switch -- $action {
create {
my _set_query_path [file join merchant_accounts create_via_api]
set query_body [my _get_merchant_query $action $data]
set query_type POST
}
find {
my _set_query_path [file join merchant_accounts [dict get $data id]]
set query_body ""
set query_type get
}
update {
if {![dict exists $data id]} {
throw [list "Invalid merchant id"] "No key \"id\" provided in dictionary"
}
my _set_query_path [file join merchant_accounts [dict get $data id] update_via_api]
set query_body [my _get_merchant_query $action $data]
set query_type PUT
}
default {
throw [list "merchant account unknown action"] "Unknown action: $action"
}
}
# Send request and pas back the response
# Error handling to be managed by the caller
set resp [my _parse_xml_response [my _send_request $query_type]]
}
#>>>
method transaction { action data } { #<<<
switch -- $action {
sale {
my _set_query_path transactions
set query_type POST
set query_body [my _get_transaction_query $action $data]
}
find {
my _set_query_path [file join transactions [dict get $data id]]
set query_type GET
set query_body ""
}
default {
throw [list "transaction unknown action"] "Unknown action: $action"
}
}
set resp [my _parse_xml_response [my _send_request $query_type]]
}
#>>>
method _build_query_body { keys script } { #<<<
try {
dom createDocument client doc
$doc documentElement root
foreach key $keys {
dom createNodeCmd elementNode $key
}
dom createNodeCmd textNode t
$root appendFromScript $script
} on error {errmsg options} {
throw [list "build_query_data failed"] "Unable to build query data: $errmsg"
} on ok {} {
$root asXML
} finally {
if {[info exist doc]} { $doc delete }
}
}
#>>>
method _get_api_url {} { #<<<
if {[dict get $config -sandbox] eq 0} {
return "api.braintreegateway.com"
} else {
return "api.sandbox.braintreegateway.com"
}
}
#>>>
method _get_merchant_query { action data } { #<<<
try {
dom createDocument merchant-account doc
$doc documentElement root
foreach key {
individual email date-of-birth first-name last-name phone ssn
address street-address locality region postal-code
business dba_name legal-name tax-id
funding destination descriptor account-number routing-number mobile-phone
tos-accepted master-merchant-account-id id
} {
dom createNodeCmd elementNode $key
}
dom createNodeCmd textNode t
$root appendFromScript {
individual {
first-name { t [dict get $data individual first_name] }
last-name { t [dict get $data individual last_name] }
email { t [dict get $data individual email] }
if {[dict exists $data individual phone]} {
phone { t [dict get $data individual phone] }
}
date-of-birth { t [dict get $data individual date_of_birth]}
if {[dict exists $data individual ssn]} {
ssn { t [dict get $data individual ssn] }
}
address {
street-address { t [dict get $data individual address street_address] }
locality { t [dict get $data individual address locality] }
region { t [dict get $data individual address region] }
postal-code { t [dict get $data individual address postal_code] }
}
}
if {[dict exists $data business]} {
business {
legal-name { t [dict get $data business legal_name]}
if {[dict exists $data business dba_name]} {
# optional
dba_name { t [dict get $data business dba_name] }
}
tax-id { t [dict get $data business tax_id] }
}
if {[dict exists $data business address]} {
# If provided, all fields required
street-address { t [dict get $data business address street_address] }
locality { t [dict get $data business address locality] }
region { t [dict get $data business address region] }
postal-code { t [dict get $data business address postal_code] }
}
}
funding {
if {[dict exists $data funding descriptor]} {
descriptor { t [dict get $data funding descriptor] }
}
destination { t [dict get $data funding destination] }
switch -- [dict get $data funding destination] {
bank {
account-number { t [dict get $data funding account_number] }
routing-number { t [dict get $data funding routing_number] }
}
mobile_phone {
mobile-phone { t [dict get $data funding mobile_phone]}
}
email {
email { t [dict get $data funding email] }
}
default {
# Throw error - unknown funding option
}
}
}
if { $action eq "create" } {
tos-accepted {type boolean} { t "true" }
master-merchant-account-id { t [dict get $data master_merchant_account_id] }
if {[dict exists $data id] && [dict get $data id] ne ""} {
id { t [dict get $data id] }
}
}
}
} on error {errmsg options} {
throw [list "merchant account query error"] "Problem formatting xml query: $errmsg"
} on ok {} {
$root asXML
} finally {
if {[info exists doc]} {
$doc delete
}
}
}
#>>>
method _get_transaction_query { action data } { #<<<
try {
dom createDocument transaction doc
$doc documentElement root
foreach key {
amount order_id merchant-account-id payment-method-nonce
customer first-name last-name company phone fax website email
billing street-address extended-address locality region postal-code country-code-alpha2
shipping
options submit-for-settlement
channel
type service-fee-amount
custom-fields
} {
dom createNodeCmd elementNode $key
}
dom createNodeCmd textNode t
$root appendFromScript {
type { t "sale" }
merchant-account-id { t [dict get $data merchant_account_id] }
amount { t [dict get $data amount] }
payment-method-nonce { t [dict get $data payment_method_nonce] }
service-fee-amount { t [dict get $data service_fee_amount] }
if {[dict exists $data order_id]} { order_id { t [dict get $data order_id]} }
foreach { key subkeys } {
customer {
first_name
last_name
company
phone
fax
website
email
}
billing {
first_name
last_name
company
street_address
extended_address
locality
region
postal_code
country_code_alpha2
}
shipping {
first_name
last_name
company
street_address
extended_address
locality
region
postal_code
country_code_alpha2
}
} {
if {[dict exists $data $key]} {
${key} {
foreach k $subkeys {
if {[dict exists $data ${key} $k]} {
[string map [list _ -] $k] { t [dict get $data ${key} $k]}
}
}
}
}
}
if {[dict exists $data channel]} { channel { t [dict get $data channel]} }
if {[dict exists $data options]} {
options {
submit-for-settlement {type boolean} { t "true" }
}
}
}
} on error {errmsg options} {
throw [list "transaction query error"] "Problem formatting xml query: $errmsg"
} on ok {} {
$root asXML
} finally {
if {[info exists doc]} {
$doc delete
}
}
}
#>>>
method _parse_node { n } { #<<<
# This is quite simplistic and does not
# cater for all cases.
# Where repeated elements, at the same level
# are not nested structures, this is fine
set dat [dict create]
while {[$n hasChildNodes]} {
set child [$n firstChild]
set node_name [$child nodeName]
if {[llength [$child childNodes]] >= 1 && [[$child firstChild] nodeType] ne "TEXT_NODE"} {
# dict set dat $node_name [my _parse_node $child]
set cn [my _parse_node $child]
if {[dict exists $dat $node_name]} {
dict set dat $node_name [list {*}[dict get $dat $node_name] {*}$cn]
} else {
dict set dat $node_name $cn
}
} elseif {[llength [$child childNodes]] eq 0} {
# puts "Empty node: $node_name"
dict set dat $node_name {}
} else {
# puts "Text node: $node_name"
if {[dict exists $dat $node_name]} {
# puts "appending to $node_name: [$child asText]"
dict set dat $node_name [list {*}[dict get $dat $node_name] [$child asText]]
} else {
# puts "set key $node_name to [$child asText]"
dict set dat $node_name [$child asText]
}
}
$n removeChild $child
}
return $dat
}
#>>>
method _parse_xml_response { resp } { #<<<
# puts "rl_braintree _parse_xml_reponse: check resp: [string trim $resp]"
if {[string length [string trim $resp]] < 1} {
# Nothing to parse
return $resp
}
# Parse XML response from Braintree
# Check if the xml response starts with
# <?xml ... ?> - strip it if it does before
# parsing with tdom
if {[regexp -indices {(<\?xml.*\?>)} $resp match]} {
set start [lindex $match 0]
set end [lindex $match 1]
set resp [string replace $resp $start $end ""]
}
set info [dict create]
try {
dom parse $resp doc
$doc documentElement root
return [my _parse_node $root]
} on error {errmsg options} {
throw [list "parse xml error"] "Problem parsing response from Braintree: $errmsg"
} finally {
if {[info exists doc]} {
$doc delete
}
}
}
#>>>
method _send_request { { type POST }} { #<<<
# puts "Sending Braintree request ($type) :\nurl: $query_path\nheaders: $headers\ndata: $query_body "
try {
rl_http create h [string toupper $type] https://$query_path \
-data [encoding convertto utf-8 $query_body] \
-headers $headers \
-accept "application/xml"
} on error {errmsg options} {
throw [list "send request error"] "Problem sending request: $errmsg"
} on ok {} {
# [h code] returns the response html code, e.g 200, etc
set resp [h body]
} finally {
if {[info object isa object h]} { h destroy }
}
}
#>>>
method _set_query_path action { #<<<
# This only accomodates Braintree Marketplace, at this point
set query_path [file join [my _get_api_url] [my base_merchant_path] $action]
}
#>>>
}
# vim: foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4 ft=tcl