Ejemplo n.º 1
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 {})
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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()
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
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)
Ejemplo n.º 9
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)
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
 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)
Ejemplo n.º 12
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)
Ejemplo n.º 13
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()
Ejemplo n.º 14
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)
Ejemplo n.º 15
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
Ejemplo n.º 16
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)
Ejemplo n.º 17
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)
Ejemplo n.º 18
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)
Ejemplo n.º 19
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)