forked from paywhirl/python-pwclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaywhirl.py
More file actions
executable file
·680 lines (508 loc) · 21.1 KB
/
paywhirl.py
File metadata and controls
executable file
·680 lines (508 loc) · 21.1 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
"""PayWhirl API Library
====================
This library has been made available to simplify
interfacing with PayWhirl's API
located at [api.paywhirl.com](https://api.paywhirl.com)
API keys can be obtained after making an account on
[PayWhirl's account page](https://app.paywhirl.com/api-keys)
Example Usage:
--------------
```
import paywhirl as pw
api_key = '<your_api_key_here>'
api_secret = '<your_secret_key_here>'
paywhirl = pw.PayWhirl(api_key, api_secret)
list_size_limit = {'limit': 2}
myobj = paywhirl.get_customers(list_size_limit)
print(myobj)
```
For information on type hints in Python 3.5 and higher see
https://www.python.org/dev/peps/pep-0484/
"""
from typing import Any
import requests
class PayWhirl:
_api_key = ''
_api_secret = ''
_api_base = ''
def __init__(
self,
api_key: str,
api_secret: str,
api_base: str = 'https://api.paywhirl.com') -> None:
"""Initialize the paywhirl object for making requests.
Args:
api_key: the api key for your account
api_secret: your secret key
api_base: the target URL for requests.
Defaults to 'https://api.paywhirl.com'
"""
self._api_key = api_key
self._api_secret = api_secret
self._api_base = api_base
def get_customers(self, data: dict) -> list:
"""Get a list of customers associated with your account.
Args:
data:
{
'limit': (int),
'order_key': (str),
'order_direction': (str),
'before_id': (int),
'after_id': (int),
'keyword': (str)
}
'limit' defaults to 100. 'order_key' defaults to 'id'.
'order_direction' options are 'asc' and 'desc'
for ascend and descend, respectively.
'before_id' returns all customers less than the
specified id, and 'after_id' returns
all customers greater than the specified id.
'keyword' will filter the results by the chosen string.
Returns:
A list of customer dicts filtered by your arguments,
or an error message indicating what went wrong.
"""
return self._get('/customers', data)
def get_customer(self, customer_id: int) -> Any:
"""Get a single customer.
Args:
customer_id: the id number obtained from paywhirl's servers.
(use the get_customers() method to find your IDs)
Returns:
A dictionary with complete customer data
or an error message indicating what went wrong.
"""
return self._get(str.format('/customer/{0}', customer_id))
def create_customer(self, data: dict) -> Any:
"""Create a new customer with supplied data.
Args:
data:
{
'first_name': (str),
'last_name': (str),
'email': (str),
'password': (str),
'currency': (str)
}
Only the required key: value pairs are listed above,
more information about additional options can be found
on the docs site located in the header of this file.
Returns:
A response containing either the created customer dictionary
or an error message indicating what went wrong.
"""
return self._post('/create/customer', data)
def update_customer(self, data: dict) -> Any:
"""Update an existing customer (selected by id) with new info.
Args:
data:
{
'id': (int),
...
}
Any element existing in a current customer object should
be a viable key-value pair to pass in for modification.
Returns:
A dict containing either the updated customer
or an error message indicating what went wrong.
"""
return self._post('/update/customer', data)
def get_questions(self, return_list_size: int = 100) -> Any:
"""Retrieve a list of all questions associated with your
account.
Args:
return_list_size: on a successful query, this will
specify the number of elements in the returned list.
Default value is 100.
Returns:
A list containing answer dicts,
or an error message indicating what went wrong.
"""
data = {'limit': return_list_size}
return self._get('/questions', data)
def update_answer(self, data: dict) -> Any:
"""Update an existing answer with new info.
Args:
data:
{
'customer_id': (int),
'question_name': (str),
'answer': (str),
'address_id': (int)
}
Returns:
A dict containing either the updated answer,
a list of answers,
or an error message indicating what went wrong.
"""
return self._post('/update/answer', data)
def get_answers(self, customer_id: int) -> Any:
"""Get a list of answers associated with a customer.
Args:
customer_id: the 'id' value from a customer dict.
you can find this via the get_customers() method.
Returns:
A list containing answer dictionaries,
or an error message indicating what went wrong.
"""
data = {'customer_id': customer_id}
return self._get('/answers', data)
def get_plans(self, data: dict) -> Any:
"""Get a list of plans associated with your account.
Args:
data:
{
'limit': (int),
'order_key': (str),
'order_direction': (str),
'before_id': (int),
'after_id': (int)
}
'limit' defaults to 100. 'order_key' defaults to 'id'.
'order_direction' can be 'asc' or 'desc'. Defaults to
descending. 'before_id' and 'after_id' will return plans
with 'id's less than or greater than the selected 'id'
number, respectively.
Returns:
A list containing plan dictionaries,
or an error message indicating what went wrong.
"""
return self._get('/plans', data)
def get_plan(self, plan_id: int) -> Any:
"""Get a single plan using the plan's ID
Args:
plan_id: the id number obtained from paywhirl's servers.
(use the get_plans() method to find your IDs)
Returns:
A dictionary with data for a given plan
or an error message indicating what went wrong.
"""
return self._get(str.format('/plan/{0}', plan_id))
def create_plan(self, data: dict) -> Any:
"""Create a plan to set rules for how a customer will be billed.
Args:
data: A dictionary containing plan rules.
See the docs linked in the header for more info.
Returns:
A dictionary containing the created plan
or an error message indicating what went wrong.
"""
return self._post('/create/plan', data)
def update_plan(self, data: dict) -> Any:
"""Update an existing plan selected by a plan's 'id' member.
Args:
data: A dictionary containing plan rules. the 'id' field
is required.
See the docs linked in the header for more info.
Returns:
A dictionary containing the updated plan
or an error message indicating what went wrong.
"""
return self._post('/update/plan', data)
def get_subscriptions(self, customer_id: int) -> Any:
"""Retrieve a list of all subscriptions for a given customer.
Args:
customer_id: This can be found using the get_customers()
method.
Returns:
A list containing plan dictionaries
or an error message indicating what went wrong.
"""
return self._get(str.format('/subscriptions/{0}', customer_id))
def get_subscription(self, subscription_id: int) -> Any:
"""Retrieve a single subscription by passing in an ID.
Args:
subscription_id: These can be found by using the
get_subscriptions() method.
Returns:
A single dict containing subscription information
or an error message indicating what went wrong.
"""
return self._get(str.format('/subscription/{0}', subscription_id))
def subscribe_customer(self, data: dict) -> Any:
"""Subscribe a customer to a given plan.
Args:
data:
{
'customer_id': (int),
'plan_id': (int),
'quantity': (int),
'promo_id': (int),
'trial_end': (int)
}
customer_id: The existing customer. (These can be found
with the get_customers() method).
plan_id: The plan to subscribe to. (These can be found
with the get_plans() method).
trial_end(optional): A UNIX timestamp indicating when a
trial period should end. The docs linked in the header
have extra information on how to generate these.
Defaults to no trial.
promo_id(optional): An existing promo code ID number.
(These can be found with the get_promos() method).
quantity(optional): Number of subscriptions to subscribe to.
This defaults to 1.
Returns:
A dictionary containing information about the subscription
or an error message indicating what went wrong.
"""
return self._post('/subscribe/customer', data)
def update_subscription(self, subscription_id: int, plan_id: int, quantity: int=None) -> Any:
"""Change a customer's subscription to a different plan.
Args:
subscription_id: The current subscription id.
plan_id: The new plan for the subscription.
Returns:
A dictionary containing information about the subscription
or an error message indicating what went wrong.
"""
data = dict([('subscription_id', subscription_id),
('plan_id', plan_id)])
if quantity is not None:
data['quantity'] = quantity
return self._post('/update/subscription', data)
def unsubscribe_customer(self, subscription_id: int) -> Any:
"""Cancel a customer's existing subscription.
Args:
subscription_id: You can find these by using the
get_subscriptions() method for a given customer.
Returns:
A dictionary with {'status: 'success' or 'fail'}
or an error message indicating what went wrong.
"""
data = dict([('subscription_id', subscription_id)])
return self._post('/unsubscribe/customer', data)
def get_subscribers(self, data: dict) -> Any:
"""Get a list of all active subscribers.
Args:
data:
{
'limit': (int),
'order': (str),
'keyword': (str),
'starting_after': (int),
'starting_before': (int)
}
'limit' defaults to 20.
'order' can be 'asc', 'desc', or 'rand'.
'starting_after' will return subscribers with
subscription IDs greater than 'starting_after'.
'starting_before' will return subscribers with
subscription IDs greater than 'starting_before'.
'keyword' will filter the results by that word.
Returns:
A list containing subscriber dictionaries,
or an error message indicating what went wrong.
"""
return self._get('/subscribers', data)
def get_invoice(self, invoice_id: int) -> Any:
"""Get the data for a single invoice when given an ID number.
Args:
invoice_id: Pass in a known invoice ID or use get_invoices()
to get a collection of them from a single customer.
Returns:
A dictionary containing information about the selected
invoice, or an error message indicating what went wrong.
"""
return self._get(str.format('/invoice/{0}', invoice_id))
def get_invoices(self, customer_id: int) -> Any:
"""Get a list of upcoming invoices for a specified customer.
Args:
customer_id: These can be found using the get_customers()
method.
Returns:
A dictionary or list of dictionaries containing invoice
data, or an error message indicating what went wrong.
"""
return self._get(str.format('/invoices/{0}', customer_id))
def get_gateways(self) -> Any:
"""Returns a list of your payment gateways.
Returns:
A dictionary or list of dictionaries containing gateway
data, or an error message indicating what went wrong.
"""
return self._get('/gateways')
def get_gateway(self, gateway_id: int) -> Any:
"""Get a gateway specified by its ID number.
Args:
gateway_id: this can be found using get_gateways().
Returns:
A dictionary or list of dictionaries containing gateway
data, or an error message indicating what went wrong.
"""
return self._get(str.format('/gateway/{0}', gateway_id))
def create_charge(self, data: dict) -> Any:
"""Attempt to a customer and return an invoice.
Args:
dict:
See docs linked in the header for param options.
Returns:
A dictionary containing an invoice, or an error message
indicating what went wrong.
"""
return self._post('/create/charge', data)
def get_charge(self, charge_id: int) -> Any:
"""Get a single charge using the charge ID.
Args:
charge_id: these can be found in each invoice.
Returns:
A dictionary containing charge information, or an error
message indicating what went wrong.
"""
return self._get(str.format('/charge/{0}', charge_id))
def get_cards(self, customer_id: int) -> Any:
"""Get a list of cards associated with a customer.
Args:
customer_id: these can be obtained via get_customers()
Returns:
A list of dicts containing card information, or an error
message indicating what went wrong.
"""
return self._get(str.format('/cards/{0}', customer_id))
def get_card(self, card_id: int) -> Any:
"""Get a single card by ID.
Args:
customer_id: these can be via get_customers()
Returns:
A list of dicts containing card information, or an error
message indicating what went wrong.
"""
return self._get(str.format('/card/{0}', card_id))
def create_card(self, data: dict) -> Any:
"""Create a payment method and add it to an existing customer.
Args:
data: See docs linked in the header for param options.
Returns:
A dict containing card information, or an error
message indicating what went wrong.
"""
return self._post('/create/card', data)
def delete_card(self, card_id: int) -> Any:
"""Delete an existing card by its ID number.
Args:
card_id: this can be found via the get_cards() method.
Returns:
A dictionary with {'status: 'success' or 'fail'}
or an error message indicating what went wrong.
"""
data = dict([('id', card_id)])
return self._post('/delete/card', data)
def get_promos(self) -> Any:
"""Return a list of all promos on file."""
return self._get('/promo')
def get_promo(self, promo_id: int) -> Any:
"""Get a single promo by ID.
Args:
promo_id: these can be obtained via get_customers()
Returns:
A dict containing promo information, or an error
message indicating what went wrong.
"""
return self._get(str.format('/promo/{0}', promo_id))
def create_promo(self, data: dict) -> Any:
"""Create a promo code to use with subscriptions.
Args:
data: See docs linked in the header for param options.
Returns:
A dict containing promo information, or an error
message indicating what went wrong.
"""
return self._post('/create/promo', data)
def delete_promo(self, promo_id: int) -> Any:
"""Delete an existing promo by its ID number.
Args:
promo_id: this can be found via the get_promos() method.
Returns:
A dictionary with {'status: 'success' or 'fail'}
or an error message indicating what went wrong.
"""
data = dict([('id', promo_id)])
return self._post('/delete/promo', data)
def get_email_template(self, template_id: int) -> Any:
"""Get the data for an email template when given an ID number.
Args:
template_id: Pass in a known template ID.
You can find these on the paywhirl app template page.
Returns:
A dictionary containing information about the selected
template, or an error message indicating what went wrong.
"""
return self._get(str.format('/email/{0}', template_id))
def send_email(self, data: dict) -> Any:
"""Send a system generated email based on one of your pre-
defined templates on your paywhirl account page
Args:
see api.paywhirl.com, the list depends on what
email templates you have available
Returns:
either a string with "status" => "success" or an error message indicating
the need for another parameter
"""
return self._post('/send-email', data)
def get_account(self) -> Any:
"""Get a dictionary containing your account information."""
return self._get('/account')
def get_stats(self) -> Any:
"""Get invoice and revenue statistics about your account."""
return self._get('/stats')
def get_shipping_rules(self) -> Any:
"""Get a list of shipping rules in dict format."""
return self._get('/shipping/')
def get_shipping_rule(self, shipping_rule_id: int) -> Any:
"""Get the data for a shipping rule when given an ID number.
Args:
shipping_rule_id: Pass in a known template ID.
You can find these using the get_shipping_rules() method.
Returns:
A dictionary containing information about the selected
rule, or an error message indicating what went wrong.
"""
return self._get(str.format('/shipping/{0}', shipping_rule_id))
def get_tax_rules(self) -> Any:
"""Get a list of all tax rules created by your account."""
return self._get('/tax')
def get_tax_rule(self, rule_id: int) -> Any:
"""Get the data for a tax rule when given an ID number.
Args:
rule_id: Pass in a known tax rule ID.
You can find these using the get_tax_rules() method.
Returns:
A dictionary containing information about the selected
rule, or an error message indicating what went wrong.
"""
return self._get(str.format('/tax/{0}', rule_id))
def get_multi_auth_token(self, data: dict) -> Any:
"""Get a MultiAuth token to use to automatically
login a customer to a widget.
Args:
data: See docs linked in the header for param options.
Returns:
A dict containing a multiauth token, or an error
message indicating what went wrong.
"""
return self._post('/multiauth', data)
def _post(self, endpoint: str, params: Any = None) -> Any:
if params is None:
params = {}
url = (self._api_base + '/' + endpoint)
headers = {'api_key': self._api_key, 'api_secret': self._api_secret}
print(url, headers)
resp = requests.post(url, headers=headers, params=params)
if resp.status_code == requests.codes['ok']:
ret = resp.json()
resp.close()
return ret
return resp.status_code
def _get(self, endpoint: str, params: Any = None) -> Any:
if params is None:
params = {}
url = self._api_base + '/' + endpoint
headers = {'api_key': self._api_key, 'api_secret': self._api_secret}
print(url, headers)
resp = requests.get(url, headers=headers, params=params)
if resp.status_code == requests.codes['ok']:
ret = resp.json()
resp.close()
return ret
return resp.status_code