Beispiel #1
0
    def dispute_details_from_entity(
        self,
        dispute: Dispute,
        response_type: ResponseType = ResponseType.MINIMAL
    ) -> PaypalApiResponse[Dispute]:
        """Performs an API call to show the details for a dispute, by ID. 
        
        Arguments:
            dispute_id {str} -- Dispute identifier
        
        Keyword Arguments:
            response_type {ResponseType} -- The preferred response type (default: {ResponseType.MINIMAL})
        
        Returns:
            PaypalApiResponse[Dispute] -- [description]
        """
        url = dispute.read_link
        headers = {'Prefer': response_type.as_header_value()}
        api_response = self._execute_action_link(url, None, headers=headers)

        if api_response.status_code != 200:
            return PaypalApiResponse(True, api_response)

        return PaypalApiResponse(
            False, api_response,
            Dispute.serialize_from_json(api_response.json(), response_type))
Beispiel #2
0
    def list_plans(
            self, product_id: str = None, plan_ids: List[str] = [], 
            page_size: int = 10, page: int = 1, total_required: bool = True, 
            response_type: ResponseType = ResponseType.MINIMAL
        ) -> PaypalPage[Plan]:
        """Calls the Paypal API to list different plan details in a page
        
        Keyword Arguments:
            product_id {str} -- product id to query (default: {None})
            plan_ids {List[str]} -- list of desired plan ids (10 supported) (default: {[]})
            page_size {int} -- size of the page (default: {10})
            page {int} -- desired page (default: {1})
            total_required {bool} -- flag to show the total count in the response (default: {True})
            response_type {ResponseType} -- response type (default: {ResponseType.MINIMAL})
        
        Returns:
            PaypalPage[Plan] -- Page with plan details
        """
        url = self._base_url
        params = { 'page_size': page_size, 'page': page, 'total_required': total_required }

        if product_id:
            params['product_id'] = product_id
        if plan_ids:
            params['plan_ids'] = ','.join(plan_ids)

        api_response = self._session.get(url, params, headers = { 'Prefer': response_type.as_header_value() })

        if api_response.status_code // 100 != 2:
            return PaypalPage.error(api_response)
        
        return PaypalPage.full_parse_success(api_response, Plan, 'plans', response_type)
Beispiel #3
0
    def create_subscription(
        self,
        subscription: Subscription,
        request_id: str = None,
        response_type: ResponseType = ResponseType.MINIMAL
    ) -> PaypalApiResponse[Subscription]:
        """Calls the paypal API to create a subscription
        
        Arguments:
            subscription {Subscription} -- The subscription to be made (use the create factory method)
        
        Keyword Arguments:
            request_id {str} -- Paypal request id for idempotence (default: {None})
            response_type {ResponseType} -- Peferred response type (default: {ResponseType.MINIMAL})
        
        Returns:
            PaypalApiResponse[Subscription] -- an API response object with the subscription info
        """
        url = self._base_url
        body = json.dumps(subscription.to_dict())
        headers = {'Prefer': response_type.as_header_value()}

        if request_id:
            headers['PayPal-Request-Id'] = request_id

        api_response = self._session.post(url, body, headers=headers)

        if api_response.status_code // 100 != 2:
            return PaypalApiResponse.error(api_response)

        return PaypalApiResponse.success(
            api_response,
            Subscription.serialize_from_json(api_response.json(),
                                             response_type))
Beispiel #4
0
    def add_trackers(
        self,
        trackers: List[Tracker],
        response_type: ResponseType = ResponseType.MINIMAL
    ) -> PaypalApiResponse[Tracker]:
        """Adds trackers to a transaction
        
        Arguments:
            trackers {List[Tracker]} -- [description]
        
        Returns:
            PaypalApiResponse[Tracker] -- [description]
        """
        body = []
        url = parse_url(self._base_url, 'trackers-batch')
        headers = {'Prefer': response_type.as_header_value()}

        for t in trackers:
            b = t.to_dict()
            b['shipment_date'] = b.pop('_shipment_date', None)
            b['last_updated_time'] = b.pop('_last_updated_time', None)
            self._clean_dictionary(b, _TRACKER_PROPERTIES)
            body.append(b)

        api_response = self._session.post(url,
                                          json.dumps(body),
                                          headers=headers)

        if api_response.status_code != 200:
            return PaypalApiResponse(True, api_response)
        return PaypalApiResponse(
            False, api_response,
            Tracker.serialize_from_json(api_response.json(), response_type))
Beispiel #5
0
    def show_tracking_info(
        self,
        transaction_id: str,
        tracking_number: int,
        response_type: ResponseType = ResponseType.MINIMAL
    ) -> PaypalApiResponse[Tracker]:
        """Gets the tracking info for a given tracker key
        
        Arguments:
            transaction_id {str} -- The id of the transaction
            tracking_number {int} -- The tracking number

        Keyword Arguments:
            response_type {ResponseType} -- [description] (default: {ResponseType.MINIMAL})
        
        Returns:
            PaypalApiResponse[Tracker] -- Response container with a Tracker instance including the required information
        """
        headers = {'Prefer': response_type.as_header_value()}
        url = parse_url(self._base_url, f'{transaction_id}-{tracking_number}')
        api_response = self._session.get(url, None, headers=headers)

        if api_response.status_code != 200:
            return PaypalApiResponse(True, api_response)
        return PaypalApiResponse(
            False, api_response,
            Tracker.serialize_from_json(api_response.json(), response_type))
Beispiel #6
0
    def capture_authorized_payment(
        self,
        authorization_id: str,
        invoice_id: str = None,
        note_to_payer: str = None,
        instruction: PaymentInstruction = None,
        amount: Money = None,
        final_capture: bool = False,
        request_id: str = None,
        response_type: ResponseType = ResponseType.MINIMAL
    ) -> PaypalApiResponse[Capture]:
        """Calls the paypal API to capture an authorized payment, by ID.
        
        Arguments:
            authorization_id {str} -- authorization id
            invoice_id {str} -- associated invoice
            note_to_payer {str} -- informational notes about the settlement. Appears in the payer's transaction history and received emails.
        
        Keyword Arguments:
            instruction {PaymentInstruction} -- Any additional payment instructions for PayPal for Partner customers (default: {None})
            amount {Money} -- The amount to capture. If none the full amount will be captured (default: {None}).
            final_capture {bool} -- dictates whether you can make additional captures against the authorized payment (default: {False}).
            request_id {str} -- Paypal request id for idempotence (default: {None})
            response_type {ResponseType} -- desired response type (default: {ResponseType.MINIMAL})
        
        Returns:
            PaypalApiResponse[Authorization] -- An api response with the authorization details
        """
        body = dict()
        url = parse_url(self._base_url, authorization_id, 'capture')
        headers = {'Prefer': response_type.as_header_value()}

        if request_id:
            headers['PayPal-Request-Id'] = request_id

        if invoice_id:
            body['invoice_id'] = invoice_id
        if note_to_payer:
            body['note_to_payer'] = note_to_payer
        if final_capture:
            body['final_capture'] = final_capture
        if instruction:
            body['instruction'] = instruction
        if amount:
            body['amount'] = amount.to_dict()

        api_response = self._session.post(url,
                                          json.dumps(body),
                                          headers=headers)

        if api_response.status_code != 201:
            return PaypalApiResponse(True, api_response)

        return PaypalApiResponse(
            False, api_response,
            Capture.serialize_from_json(api_response.json()))
Beispiel #7
0
    def refund_capture(
        self,
        capture_id: str,
        invoice_id: str,
        note_to_payer: str,
        amount: Money = None,
        request_id: str = None,
        auth_assertion_token: str = None,
        response_type: ResponseType = ResponseType.MINIMAL
    ) -> PaypalApiResponse[Refund]:
        """Calls the api to refund a capture
        
        Arguments:
            capture_id {str} -- capture identifier
            invoice_id {str} -- invoice related capture
            note_to_payer {str} -- notes to the customer
        
        Keyword Arguments:
            amount {Money} -- amount to be refunded if None then 'captured amount - previous refunds' (default: {None})
            request_id {str} -- request id for idempotence (default: {None})
            auth_assertion_token {str} -- auth assertion token. See paypal header docs (default: {None})
            response_type {ResponseType} -- response type. See paypal header docs (default: {ResponseType.MINIMAL})
        
        Returns:
            PaypalApiResponse[Refund] -- api response with refund details
        """

        body = dict()
        url = parse_url(self._base_url, capture_id, 'refund')

        headers = {'Prefer': response_type.as_header_value()}

        if request_id:
            headers['PayPal-Request-Id'] = request_id
        if auth_assertion_token:
            headers['PayPal-Auth-Assertion'] = auth_assertion_token

        if invoice_id:
            body['invoice_id'] = invoice_id
        if note_to_payer:
            body['note_to_payer'] = note_to_payer
        if amount:
            body['amount'] = amount.to_dict()

        api_response = self._session.post(url,
                                          json.dumps(body),
                                          headers=headers)

        if api_response.status_code != 201:
            return PaypalApiResponse(True, api_response)

        return PaypalApiResponse(
            False, api_response,
            Refund.serialize_from_json(api_response.json()))
Beispiel #8
0
    def capture_payment_for_order(
        self,
        order_id: str,
        payment_source: PaymentSource = None,
        request_id: str = None,
        client_metadata_id: str = None,
        auth_assertion: str = None,
        response_type: ResponseType = ResponseType.MINIMAL
    ) -> PaypalApiResponse[Order]:
        """Calls the API to Capture a payment for an order. 
           To successfully authorize payment for an order, 
           the buyer must first approve the order 
           or a valid payment_source must be provided in the request.
        
        Arguments:
            order_id {str} -- The order id
        
        Keyword Arguments:
            payment_source {PaymentSource} -- Source of payment for the order if the user wasn't redirected in the order creation to approve the payment. (default: {None})
            request_id {str} -- Request id for idempotence (default: {None})
            client_metadata_id {str} -- Verifies that the payment originates from a valid, user-consented device and application. Must be included in order to be eligible for PayPal Seller Protection. (default: {None})
            auth_assertion {str} -- A JWT assertion that identifies the merchant (default: {None})
            response_type {ResponseType} --  (default: {ResponseType.MINIMAL})
        
        Returns:
            PaypalApiResponse[Order] -- API operation response with the order if successful
        """
        url = parse_url(self._base_url, order_id, 'capture')

        headers = {'Prefer': response_type.as_header_value()}

        if request_id:
            headers['PayPal-Request-Id'] = request_id
        if auth_assertion:
            headers['PayPal-Auth-Assertion'] = auth_assertion
        if client_metadata_id:
            headers['PayPal-Client-Metadata-Id'] = client_metadata_id

        if payment_source:
            api_response = self._session.post(url,
                                              json.dumps(
                                                  payment_source.to_dict()),
                                              headers=headers)
        else:
            api_response = self._session.post(url, None)

        if api_response.status_code // 100 != 2:
            return PaypalApiResponse(True, api_response)

        return PaypalApiResponse(
            False, api_response,
            Order.serialize_from_json(api_response.json(), response_type))
Beispiel #9
0
    def list_plans_from_link(self, link: ActionLink, response_type: ResponseType = ResponseType.MINIMAL) -> PaypalPage[Plan]:
        """Calls the Paypal API to list different plan details in a page

        Arguments:
            link {ActionLink} -- Page link

        Keyword Arguments:
            response_type {ResponseType} -- response type (default: {ResponseType.MINIMAL})
        
        Returns:
            PaypalPage[Plan] -- Page with plan details
        """
        url = link.href
        api_response = self._session.get(url, headers = { 'Prefer': response_type.as_header_value() })

        if api_response.status_code // 100 != 2:
            return PaypalPage.error(api_response)
        
        return PaypalPage.full_parse_success(api_response, Plan, 'plans', response_type)
Beispiel #10
0
    def create_product(self, product: Product, response_type: ResponseType = ResponseType.MINIMAL) -> PaypalApiResponse[Product]:
        """Performs an API call to create a new product
        
        Arguments:
            product_name {str} -- The product info
            product_type {ProductType} -- the response type (Minimal or Representation)
        """
        url = self._base_url
        headers = { 'Prefer': response_type.as_header_value() }
        
        body = product.to_dict()
        body['create_time'] = body.pop('_create_time', None)
        body['update_time'] = body.pop('_update_time', None)
        self._clean_dictionary(body, _PRODUCT_PROPERTIES)

        api_response = self._session.post(url, json.dumps(body), headers = headers)
        
        if api_response.status_code != 201:
            return PaypalApiResponse(True, api_response)
        return PaypalApiResponse(False, api_response, Product.serialize_from_json(api_response.json(), response_type))
Beispiel #11
0
    def product_details_from_entity(self, product: Product, response_type: ResponseType=ResponseType.MINIMAL) -> PaypalApiResponse[Product]:
        """Calls the API to get the details for a given product
        
        Arguments:
            product {Product} -- The product entity with the HATEOAS link
        
        Keyword Arguments:
            response_type {[type]} -- Response representation (default: {ResponseType.MINIMAL})
        
        Returns:
            PaypalApiResponse[Product] -- Response status with data
        """
        url = product.read_link
        headers = { 'Prefer': response_type.as_header_value() }        
        api_response = self._session.get(url, None, headers = headers)

        if api_response.status_code != 200:
            return PaypalApiResponse(True, api_response)

        return PaypalApiResponse(False, api_response, Product.serialize_from_json(api_response.json(), response_type))
Beispiel #12
0
    def create_order(
        self,
        order: Order,
        request_id: str = None,
        partner_attr_id: str = None,
        response_type: ResponseType = ResponseType.MINIMAL
    ) -> PaypalApiResponse[Order]:
        """Calls the paypal Api to create an order
        
        Arguments:
            order {Order} -- The order information.
        
        Keyword Arguments:
            request_id {str} -- Paypal request id for idempotency (default: {None})
            partner_attr_id {str} --  Identifies the caller as a PayPal partner. To receive revenue attribution. (default: {None})
            response_type {ResponseType} -- Response type to be handled (default: {ResponseType.MINIMAL})
        
        Returns:
            PaypalApiResponse[Order] -- An api response with the order
        """
        url = self._base_url

        headers = {'Prefer': response_type.as_header_value()}

        if request_id:
            headers['PayPal-Request-Id'] = request_id
        if partner_attr_id:
            headers['PayPal-Partner-Attribution-Id'] = partner_attr_id

        api_response = self._session.post(url,
                                          json.dumps(order.to_dict()),
                                          headers=headers)

        if api_response.status_code // 100 != 2:
            return PaypalApiResponse(True, api_response)

        return PaypalApiResponse(
            False, api_response,
            Order.serialize_from_json(api_response.json(), response_type))
Beispiel #13
0
    def tracking_info_by_entity(
        self,
        tracker: Tracker,
        response_type: ResponseType = ResponseType.MINIMAL
    ) -> PaypalApiResponse[Tracker]:
        """Gets the tracking info for a given tracking entity
        
        Arguments:
            tracker {Tracker} -- a tracking entity
        
        Returns:
            PaypalApiResponse[Tracker] -- Response container with a Tracker instance including the required information
        """
        url = tracker.read_link
        headers = {'Prefer': response_type.as_header_value()}
        api_response = self._execute_action_link(url, None, headers=headers)

        if api_response.status_code != 200:
            return PaypalApiResponse(True, api_response)
        return PaypalApiResponse(
            False, api_response,
            Tracker.serialize_from_json(api_response.json(), response_type))
Beispiel #14
0
    def reauthorize_payment(
        self,
        authorization_id: str,
        amount: Money,
        request_id: str = None,
        response_type: ResponseType = ResponseType.MINIMAL
    ) -> PaypalApiResponse[Authorization]:
        """Calls the paypal API to reauthorize an authorized payment, by ID. 
           See the api docs for a full set of rules.
        
        Arguments:
            authorization_id {str} -- The authorization id
            amount {Money} -- The amount to reauthorize for an authorized payment.
        Returns:
            PaypalApiResponse[Authorization] -- An api response with the authorization details
        """
        body = dict()
        url = parse_url(self._base_url, authorization_id, 'reauthorize')
        headers = {'Prefer': response_type.as_header_value()}

        if request_id:
            headers['PayPal-Request-Id'] = request_id

        if amount:
            body['amount'] = amount.to_dict()

        api_response = self._session.post(url,
                                          json.dumps(body),
                                          headers=headers)

        if api_response.status_code != 201:
            return PaypalApiResponse(True, api_response)

        return PaypalApiResponse(
            False, api_response,
            Authorization.serialize_from_json(api_response.json()))