def session_url(path, options=None):
    options = options or {}
    if api.default().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)
Exemplo n.º 2
0
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 {})
Exemplo n.º 3
0
 def test_join_url(self):
   url = util.join_url("payment", "1")
   self.assertEqual(url, "payment/1")
   url = util.join_url("payment/", "1")
   self.assertEqual(url, "payment/1")
   url = util.join_url("payment", "/1")
   self.assertEqual(url, "payment/1")
   url = util.join_url("payment/", "/1")
   self.assertEqual(url, "payment/1")
Exemplo n.º 4
0
    def post(self,
             name,
             attributes=None,
             cls=Resource,
             fieldname='id',
             refresh_token=None):
        """Constructs url with passed in headers and makes post request via
        post method in api class.

        Usage::

            >>> payment.post("execute", {'payer_id': '1234'}, payment)  # return True or False
            >>> sale.post("refund", {'payer_id': '1234'})  # return Refund object
        """
        attributes = attributes or {}
        url = util.join_url(self.path, str(self[fieldname]), name)
        if not isinstance(attributes, Resource):
            attributes = Resource(attributes, api=self.api)
        new_attributes = self.api.post(url, attributes.to_dict(),
                                       attributes.http_headers(),
                                       refresh_token)

        if isinstance(cls, Resource):
            cls.error = None
            cls.merge(new_attributes)
            return self.success()
        else:
            return cls(new_attributes, api=self.api)
Exemplo n.º 5
0
    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 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)
Exemplo n.º 7
0
    def search(cls, params=None, api=None):
        api = api or default_api()
        params = params or {}

        url = util.join_url(cls.path, 'search')

        return Resource(api.post(url, params), api=api)
Exemplo n.º 8
0
 def delete(self, action, headers=None, refresh_token=None):
     """Make DELETE request
     """
     return self.request(util.join_url(self.endpoint, action),
                         'DELETE',
                         headers=headers or {},
                         refresh_token=refresh_token)
Exemplo n.º 9
0
 def replace(self, attributes=None, refresh_token=None):
     attributes = attributes or self.to_dict()
     url = util.join_url(self.path, str(self['id']))
     new_attributes = self.api.patch(url, attributes, self.http_headers(), refresh_token)
     self.error = None
     self.merge(new_attributes)
     return self.success()
Exemplo n.º 10
0
    def get_user_info(self, api=None):

        api = api or default_api()
        endpoint = util.join_url(self.path, 'userinfo')
        attributes = [('schema', "paypalv1.1")]
        url = util.join_url_params(endpoint, attributes)
        return Resource(api.get(url), api=api)
Exemplo n.º 11
0
    def search(cls, params=None, api=None):
        api = api or default_api()
        params = params or {}

        url = util.join_url(cls.path, 'search')

        return Resource(api.post(url, params), api=api)
    def put(self, action, params=None, headers=None, refresh_token=None):
        """Make PUT request

        Usage::

            >>> api.put("v1/invoicing/invoices/INV2-RUVR-ADWQ", { 'id': 'INV2-RUVR-ADWQ', 'status': 'DRAFT'})
        """
        return self.request(util.join_url(self.endpoint, action), 'PUT', body=params or {}, headers=headers or {}, refresh_token=refresh_token)
    def patch(self, action, params=None, headers=None, refresh_token=None):
        """Make PATCH request

        Usage::

            >>> api.patch("v1/payments/billing-plans/P-5VH69258TN786403SVUHBM6A", { 'op': 'replace', 'path': '/merchant-preferences'})
        """
        return self.request(util.join_url(self.endpoint, action), 'PATCH', body=params or {}, headers=headers or {}, refresh_token=refresh_token)
Exemplo n.º 14
0
 def post(cls, action, options=None, headers=None):
     url = util.join_url(endpoint(), 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.default().http_call(url, 'POST', data=body, headers=headers)
     return cls(data)
Exemplo n.º 15
0
 def post(cls, action, options=None, headers=None):
     url = util.join_url(endpoint(), 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.default().http_call(url, 'POST', data=body, headers=headers)
     return cls(data)
Exemplo n.º 16
0
    def put(self, action, params=None, headers=None, refresh_token=None):
        """Make PUT request

        Usage::

            >>> api.put("v1/invoicing/invoices/INV2-RUVR-ADWQ", { 'id': 'INV2-RUVR-ADWQ', 'status': 'DRAFT'})
        """
        return self.request(util.join_url(self.endpoint, action), 'PUT', body=params or {}, headers=headers or {}, refresh_token=refresh_token)
Exemplo n.º 17
0
    def patch(self, action, params=None, headers=None, refresh_token=None):
        """Make PATCH request

        Usage::

            >>> api.patch("v1/payments/billing-plans/P-5VH69258TN786403SVUHBM6A", { 'op': 'replace', 'path': '/merchant-preferences'})
        """
        return self.request(util.join_url(self.endpoint, action), 'PATCH', body=params or {}, headers=headers or {}, refresh_token=refresh_token)
    def get(self, action, headers=None, refresh_token=None):
        """Make GET request

        Usage::

            >>> api.get("v1/payments/payment?count=1")
            >>> api.get("v1/payments/payment/PAY-1234")
        """
        return self.request(util.join_url(self.endpoint, action), 'GET', headers=headers or {}, refresh_token=refresh_token)
Exemplo n.º 19
0
 def get_token_hash(self):
   self.validate_token_hash()
   if self.token_hash == None :
     self.token_request_at = datetime.datetime.now()
     self.token_hash = self.http_call(util.join_url(self.token_endpoint, "/v1/oauth2/token"), "POST",
       body = "grant_type=client_credentials",
       headers = { "Authorization": ("Basic %s" % self.basic_auth()),
         "Accept": "application/json", "User-Agent": self.user_agent } )
   return self.token_hash
Exemplo n.º 20
0
    def get(self, action, headers=None, refresh_token=None):
        """Make GET request

        Usage::

            >>> api.get("v1/payments/payment?count=1")
            >>> api.get("v1/payments/payment/PAY-1234")
        """
        return self.request(util.join_url(self.endpoint, action), 'GET', headers=headers or {}, refresh_token=refresh_token)
Exemplo n.º 21
0
    def find(cls, resource_id, api=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), api=api)
Exemplo n.º 22
0
    def post(self, action, params=None, headers=None, refresh_token=None):
        """Make POST request

        Usage::

            >>> api.post("v1/payments/payment", { 'indent': 'sale' })
            >>> api.post("v1/payments/payment/PAY-1234/execute", { 'payer_id': '1234' })

        """
        return self.request(util.join_url(self.endpoint, action), 'POST', body=params or {}, headers=headers or {}, refresh_token=refresh_token)
Exemplo n.º 23
0
    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)
Exemplo n.º 24
0
 def get_token_hash(self):
   self.validate_token_hash()
   if self.token_hash == None :
     self.token_request_at = datetime.datetime.now()
     self.token_hash = self.http_call(util.join_url(self.token_endpoint, "/v1/oauth2/token"), "POST",
       body = "grant_type=client_credentials",
       headers = { "Authorization": ("Basic %s" % self.basic_auth()),
         "Content-Type": "application/x-www-form-urlencoded",
         "Accept": "application/json", "User-Agent": self.user_agent } )
   return self.token_hash
    def post(self, action, params=None, headers=None, refresh_token=None):
        """Make POST request

        Usage::

            >>> api.post("v1/payments/payment", { 'indent': 'sale' })
            >>> api.post("v1/payments/payment/PAY-1234/execute", { 'payer_id': '1234' })

        """
        return self.request(util.join_url(self.endpoint, action), 'POST', body=params or {}, headers=headers or {}, refresh_token=refresh_token)
Exemplo n.º 26
0
 def post(self, name, attributes={}, cls=Resource):
     url = util.join_url(self.path, str(self['id']), name)
     if not isinstance(attributes, Resource):
         attributes = Resource(attributes)
     new_attributes = api.default().post(url, attributes.to_dict(), attributes.http_headers())
     if isinstance(cls, Resource):
         cls.error = None
         cls.merge(new_attributes)
         return self.success()
     else:
         return cls(new_attributes)
Exemplo n.º 27
0
 def post(self, name, attributes = {}, klass = Resource):
   url = util.join_url(self.path, str(self['id']), name)
   if not isinstance(attributes, Resource):
     attributes = Resource(attributes)
   new_attributes = api.default().post(url, attributes.to_dict(), attributes.http_headers())
   if isinstance(klass, Resource):
     klass.error = None
     klass.merge(new_attributes)
     return self.success()
   else:
     return klass(new_attributes)
Exemplo n.º 28
0
    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)
Exemplo n.º 29
0
    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)
Exemplo n.º 30
0
    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)
Exemplo n.º 31
0
    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)
Exemplo n.º 32
0
    def delete(self):
        """Deletes a resource e.g. credit_card

        Usage::

            >>> credit_card.delete()
        """
        url = util.join_url(self.path, str(self['id']))
        new_attributes = self.api.delete(url)
        self.error = None
        self.merge(new_attributes)
        return self.success()
Exemplo n.º 33
0
    def delete(self):
        """Deletes a resource e.g. credit_card

        Usage::

            >>> credit_card.delete()
        """
        url = util.join_url(self.path, str(self['id']))
        new_attributes = self.api.delete(url)
        self.error = None
        self.merge(new_attributes)
        return self.success()
Exemplo n.º 34
0
    def get_token_hash(self,
                       authorization_code=None,
                       refresh_token=None,
                       headers=None):
        """Generate new token by making a POST request

            1. By using client credentials if validate_token_hash finds
            token to be invalid. This is useful during web flow so that an already
            authenticated user is not reprompted for login
            2. Exchange authorization_code from mobile device for a long living
            refresh token that can be used to charge user who has consented to future
            payments
            3. Exchange refresh_token for the user for a access_token of type Bearer
            which can be passed in to charge user

        """
        path = "/v1/oauth2/token"
        payload = "grant_type=client_credentials"

        if authorization_code is not None:
            payload = "grant_type=authorization_code&response_type=token&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code=" + \
                authorization_code

        elif refresh_token is not None:
            payload = "grant_type=refresh_token&refresh_token=" + refresh_token

        else:
            self.validate_token_hash()
            if self.token_hash is not None:
                # return cached copy
                return self.token_hash

        token = self.http_call(
            util.join_url(self.token_endpoint, path),
            "POST",
            data=payload,
            headers=util.merge_dict(
                {
                    "Authorization": ("Basic %s" % self.basic_auth()),
                    "Content-Type": "application/x-www-form-urlencoded",
                    "Accept": "application/json",
                    "User-Agent": self.user_agent
                }, headers or {}),
            timeout=__default_timeout__)

        if refresh_token is None and authorization_code is None:
            # cache token for re-use in normal case
            self.token_request_at = datetime.datetime.now()
            self.token_hash = token
        return token
Exemplo n.º 35
0
    def get_token_hash(self, authorization_code=None, refresh_token=None, headers=None):
        """Generate new token by making a POST request

            1. By using client credentials if validate_token_hash finds
            token to be invalid. This is useful during web flow so that an already
            authenticated user is not reprompted for login
            2. Exchange authorization_code from mobile device for a long living
            refresh token that can be used to charge user who has consented to future
            payments
            3. Exchange refresh_token for the user for a access_token of type Bearer
            which can be passed in to charge user

        """
        path = "/v1/oauth2/token"
        payload = "grant_type=client_credentials"

        if authorization_code is not None:
            payload = "grant_type=authorization_code&response_type=token&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code=" + \
                authorization_code

        elif refresh_token is not None:
            payload = "grant_type=refresh_token&refresh_token=" + refresh_token

        else:
            self.validate_token_hash()
            if self.token_hash is not None:
                # return cached copy
                return self.token_hash

        token = self.http_call(
            util.join_url(self.token_endpoint, path), "POST",
            data=payload,
            headers=util.merge_dict({
                "Authorization": ("Basic %s" % self.basic_auth()),
                "Content-Type": "application/x-www-form-urlencoded",
                "Accept": "application/json", "User-Agent": self.user_agent
            }, headers or {}))

        if refresh_token is None and authorization_code is None:
            # cache token for re-use in normal case
            self.token_request_at = datetime.datetime.now()
            self.token_hash = token
        return token
Exemplo n.º 36
0
    def post(self, name, attributes=None, cls=Resource, fieldname='id'):
        """Constructs url with passed in headers and makes post request via
        post method in api class.

        Usage::

            >>> payment.post("execute", {'payer_id': '1234'}, payment)  # return True or False
            >>> sale.post("refund", {'payer_id': '1234'})  # return Refund object
        """
        attributes = attributes or {}
        url = util.join_url(self.path, str(self[fieldname]), name)
        if not isinstance(attributes, Resource):
            attributes = Resource(attributes, api=self.api)
        new_attributes = self.api.post(url, attributes.to_dict(), attributes.http_headers())
        if isinstance(cls, Resource):
            cls.error = None
            cls.merge(new_attributes)
            return self.success()
        else:
            return cls(new_attributes, api=self.api)
Exemplo n.º 37
0
 def delete(self):
     url = util.join_url(self.path, str(self['id']))
     new_attributes = api.default().delete(url)
     self.error = None
     self.merge(new_attributes)
     return self.success()
Exemplo n.º 38
0
 def delete_external_refund(self, transactionId):
     # /invoicing/invoices/<INVOICE-ID>/refund-records/<TRANSACTION-ID>
     endpoint = util.join_url(self.path, str(self['id']), 'refund-records',
                              str(transactionId))
     return Resource(self.api.delete(endpoint), api=self.api)
Exemplo n.º 39
0
 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)
Exemplo n.º 40
0
 def find(klass, resource_id):
     url = util.join_url(klass.path, str(resource_id))
     return klass(api.default().get(url))
Exemplo n.º 41
0
 def delete(self, action, headers = {}):
   return self.request(util.join_url(self.endpoint, action), 'DELETE', headers = headers)
Exemplo n.º 42
0
 def put(self, action, params=None, headers=None, refresh_token=None):
     """Make PUT request
     """
     return self.request(util.join_url(self.endpoint, action), 'PUT', body=params or {}, headers=headers or {}, refresh_token=refresh_token)
Exemplo n.º 43
0
 def find(cls, resource_id):
     url = util.join_url(cls.path, str(resource_id))
     return cls(api.default().get(url))
Exemplo n.º 44
0
 def find(klass, resource_id):
   url = util.join_url(klass.path, str(resource_id))
   return klass(api.default().get(url))
Exemplo n.º 45
0
 def post(self, action, params=None, headers=None):
     params = params or {}
     headers = headers or {}
     return self.request(util.join_url(self.endpoint, action), 'POST', body=json.dumps(params), headers=headers)
Exemplo n.º 46
0
 def delete(self):
     url = util.join_url(self.path, str(self['id']))
     new_attributes = api.default().delete(url)
     self.error = None
     self.merge(new_attributes)
     return self.success()
Exemplo n.º 47
0
def session_url(path, options={}):
    if api.default().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)
Exemplo n.º 48
0
 def delete(self, action, headers=None, refresh_token=None):
     """Make DELETE request
     """
     return self.request(util.join_url(self.endpoint, action), 'DELETE', headers=headers or {}, refresh_token=refresh_token)
Exemplo n.º 49
0
 def get(self, action, headers = {}):
   return self.request(util.join_url(self.endpoint, action), 'GET', headers = headers)
Exemplo n.º 50
0
 def get(self, action, headers=None):
     headers = headers or {}
     return self.request(util.join_url(self.endpoint, action), 'GET', headers=headers)
Exemplo n.º 51
0
 def post(self, action, params = {}, headers = {}):
   return self.request(util.join_url(self.endpoint, action), 'POST', body= json.dumps(params), headers = headers )
Exemplo n.º 52
0
 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)
 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)