Created
July 29, 2019 07:55
-
-
Save bird-in-hat/588d73ef55bc9e604a8be9bc013d5589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from paypalrestsdk.resource import List, Find, Create, Post, Update, Replace, Resource | |
from paypalrestsdk.api import default as default_api | |
import paypalrestsdk.util as util | |
from paypalrestsdk.exceptions import MissingParam | |
class Sale(Find, Post): | |
"""Sale class wrapping the REST v1/payments/sale endpoint | |
Usage:: | |
>>> sale = Sale.find("<SALE_ID>") | |
>>> refund = sale.refund({"amount": {"total": "1.00", "currency": "USD"}}) | |
>>> refund.success() # return True or False | |
""" | |
path = "v1/payments/sale" | |
def refund(self, attributes): | |
return self.post('refund', attributes, Refund) | |
Sale.convert_resources['sales'] = Sale | |
Sale.convert_resources['sale'] = Sale | |
class Product(Create, Find): | |
"""A product captures goods or services offered for a subscription. | |
The <ID> is either merchant-provided or system-generated and begins with PROD-xxxx. | |
https://developer.paypal.com/docs/subscriptions/integrate/#1-create-a-product | |
""" | |
path = "/v1/catalogs/products" | |
Product.convert_resources['product'] = Product | |
Product.convert_resources['products'] = Product | |
class Plan(Create, Post, Find): | |
"""Merchants can create subscription payments i.e. planned sets of | |
future recurring payments at periodic intervals. Plans specify | |
number of payments, their frequency and other details. Acts as a template | |
for Subscription, one Plan can be used to create multiple subscriptions. | |
Wraps the /v1/billing/plans endpoint | |
https://developer.paypal.com/docs/subscriptions/full-integration/#plan-management | |
https://developer.paypal.com/docs/api/subscriptions/v1/#plans | |
Usage:: | |
>>> plans = Plan.find("PLAN_ID") | |
""" | |
path = "/v1/billing/plans" | |
def suspend(self): | |
"""Suspend a billing plan""" | |
return self.post("deactivate", attributes=None, cls=self) | |
def reactivate(self): | |
"""Activate a suspended plan""" | |
return self.post("activate", attributes=None, cls=self) | |
# def update_price(self, pricing_schemes_list): | |
# # https://developer.paypal.com/docs/subscriptions/full-integration/plan-management/#update-pricing | |
# return self.post("update-pricing-schemes", attributes=pricing_schemes_list, cls=Plan) | |
Plan.convert_resources['plan'] = Plan | |
Plan.convert_resources['plans'] = Plan | |
class Subscription(Create, Find, Replace, Post): | |
"""After plan is created and activated, the subscription | |
resource can be used to have customers agree to subscribe to plan. | |
Wraps the /v1/billing/subscriptions endpoint | |
https://developer.paypal.com/docs/subscriptions/full-integration/subscription-management/ | |
Usage:: | |
>>> subscription = Subscription.find("<SUBSCRIPTION_ID>") | |
""" | |
path = "/v1/billing/subscriptions" | |
def refresh(self): | |
return self.find(self.id) | |
def cancel(self, attributes=None): | |
return self.post('cancel', attributes, self) | |
def suspend(self, attributes=None): | |
return self.post('suspend', attributes, self) | |
def reactivate(self, attributes=None): | |
"""Activate a suspended(but not cancelled) subscription.""" | |
return self.post('activate', attributes, self) | |
def search_transactions(self, start_time, end_time, api=None): | |
if not start_time or not end_time: | |
raise MissingParam("Search transactions needs valid start_time and end_time.") | |
api = api or default_api() | |
# https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions | |
# /transactions?start_time=2018-01-21T07:50:20.940Z&end_time=2018-08-21T07:50:20.940Z | |
endpoint = util.join_url(self.path, str(self['id']), 'transactions') | |
date_range = [('start_time', start_time), ('end_time', end_time)] | |
url = util.join_url_params(endpoint, date_range) | |
return Resource(self.api.get(url), api=api) | |
Subscription.convert_resources['subscription'] = Subscription | |
Subscription.convert_resources['subscriptions'] = Subscription |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://developer.paypal.com/docs/subscriptions/
https://developer.paypal.com/docs/api/subscriptions/v1/