def session_url(path, options=None, api=None): api = api or default_api() if api.mode == "live": path = util.join_url("https://www.paypal.com", path) else: path = util.join_url("https://www.sandbox.paypal.com", path) return util.join_url_params(path, options or {})
def execute(cls, payment_token, params=None, api=None): api = api or default_api() params = params or {} url = util.join_url(cls.path, payment_token, 'agreement-execute') return Resource(api.post(url, params), api=api)
def get(cls, options=None, api=None): options = options or {} if isinstance(options, string_types): options = {'access_token': options} options = util.merge_dict({'schema': 'openid'}, options) api = api or default_api() return cls.post(cls.path, options, api=api)
def logout_url(options=None, api=None): api = api or default_api() options = util.merge_dict( { 'logout': 'true', 'redirect_uri': redirect_uri(api) }, options or {}) return session_url(end_session_path, options, api=api)
def search(cls, params=None, api=None): api = api or default_api() params = params or {} path = "v1/invoicing" url = util.join_url(path, 'search') return Resource(api.post(url, params), api=api)
def find(cls, resource_id, api=None, refresh_token=None): """Locate resource e.g. payment with given id Usage:: >>> payment = Payment.find("PAY-1234") """ api = api or default_api() url = util.join_url(cls.path, str(resource_id)) return cls(api.get(url, refresh_token=refresh_token), api=api)
def __init__(self, attributes=None, api=None): attributes = attributes or {} self.__dict__['api'] = api or default_api() super(Resource, self).__setattr__('__data__', {}) super(Resource, self).__setattr__('error', None) super(Resource, self).__setattr__('headers', {}) super(Resource, self).__setattr__('header', {}) super(Resource, self).__setattr__('request_id', None) self.merge(attributes)
def authorize_url(options=None, api=None): api = api or default_api() options = util.merge_dict( { 'response_type': 'code', 'scope': 'openid', 'client_id': client_id(api), 'redirect_uri': redirect_uri(api) }, options or {}) return session_url(start_session_path, options, api=api)
def post(cls, action, options=None, headers=None, api=None): api = api or default_api() url = util.join_url(endpoint(api), action) body = util.urlencode(options or {}) headers = util.merge_dict( { 'User-Agent': cls.user_agent, 'Content-Type': 'application/x-www-form-urlencoded' }, headers or {}) data = api.http_call(url, 'POST', data=body, headers=headers) return cls(data, api=api)
def get_qr_code(self, height=500, width=500, api=None): # height and width have default value of 500 as in the APIs api = api or default_api() # Construct url similar to # /invoicing/invoices/<INVOICE-ID>/qr-code?height=<HEIGHT>&width=<WIDTH> endpoint = util.join_url(self.path, str(self['id']), 'qr-code') image_attributes = [('height', height), ('width', width)] url = util.join_url_params(endpoint, image_attributes) return Resource(self.api.get(url), api=api)
def create_with_refresh_token(cls, options=None, api=None): options = options or {} api = api or default_api() if isinstance(options, string_types): options = {'refresh_token': options} options = util.merge_dict( { 'grant_type': 'refresh_token', 'client_id': client_id(api), 'client_secret': client_secret(api) }, options) return cls.post(cls.path, options, api=api)
def create(cls, options=None, api=None): options = options or {} api = api or default_api() if isinstance(options, string_types): options = {'code': options} options = util.merge_dict( { 'grant_type': 'authorization_code', 'client_id': client_id(api), 'client_secret': client_secret(api) }, options) return cls.post(cls.path, options, api=api)
def search_transactions(self, start_date, end_date, api=None): if not start_date or not end_date: raise exceptions.MissingParam( "Search transactions needs valid start_date and end_date.") api = api or default_api() # Construct url similar to # /billing-agreements/I-HT38K76XPMGJ/transactions?start-date=2014-04-13&end-date=2014-04-30 endpoint = util.join_url(self.path, str(self['id']), 'transactions') date_range = [('start_date', start_date), ('end_date', end_date)] url = util.join_url_params(endpoint, date_range) return Resource(self.api.get(url), api=api)
def all(cls, params=None, api=None): """Get list of payments as on https://developer.paypal.com/docs/api/#list-payment-resources Usage:: >>> payment_history = Payment.all({'count': 2}) """ api = api or default_api() if params is None: url = cls.path else: url = util.join_url_params(cls.path, params) try: response = api.get(url) return cls.list_class(response, api=api) except AttributeError: # To handle the case when response is JSON Array if isinstance(response, list): new_resp = [cls.list_class(elem, api=api) for elem in response] return new_resp
def endpoint(api=None): api = api or default_api() return api.options.get("openid_endpoint", api.endpoint)
def redirect_uri(api=None): api = api or default_api() return api.options.get("openid_redirect_uri")
def client_secret(api=None): api = api or default_api() return api.options.get("openid_client_secret", api.client_secret)
def next_invoice_number(cls, api=None): api = api or default_api() url = util.join_url(cls.path, 'next-invoice-number') return Resource(api.post(url), api=api)
def get_event_types(self, api=None): """Get the list of events types that are subscribed to a webhook """ api = api or default_api() url = util.join_url(self.path, str(self['id']), 'event-types') return Resource(self.api.get(url), api=api)