示例#1
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))
示例#2
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))
示例#3
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))
示例#4
0
    def create_profile(
            self,
            profile: WebExpProfile,
            request_id: str = None) -> PaypalApiResponse[WebExpProfile]:
        """Calls the paypal Api to create a WebExp Profile
        
        Arguments:
            profile {WebExpProfile} -- The profile information.
        
        Keyword Arguments:
            request_id {str} -- Paypal request id for idempotency (default: {None})
        
        Returns:
            PaypalApiResponse[WebExpProfile] -- An api response with the profile
        """
        headers = {'PayPal-Request-Id': request_id} if request_id else dict()
        api_response = self._session.post(self._base_url,
                                          json.dumps(profile.to_dict()),
                                          headers=headers)

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

        return PaypalApiResponse(
            False, api_response,
            WebExpProfile.serialize_from_json(api_response.json()))
示例#5
0
    def resend_event_notification(
            self,
            event_id: str,
            webhooks_ids: List[str] = []) -> PaypalApiResponse[WebhookEvent]:
        """Calls the paypal API to resend notification events
        
        Arguments:
            event_id {str} -- The webhook event id
        
        Keyword Arguments:
            webhooks_ids {List[str]} -- webhooks notification to resend (all pending if empty) (default: {[]})
        
        Returns:
            PaypalApiResponse[WebhookEvent] -- A response with the webhooks events.
        """
        body = {'webhook_ids': webhooks_ids or []}
        api_response = self._session.post(parse_url(self._base_url, event_id),
                                          json.dumps(body))

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

        return PaypalApiResponse.success(
            api_response,
            WebhookEvent.serialize_from_json(api_response.json()))
示例#6
0
    def simulate_webhook_event(
            self, webhook_id: str, url: str, event_type: str,
            resource_version: str) -> PaypalApiResponse[WebhookEvent]:
        """Calls the paypal API to send a webhook simulation
        
        Arguments:
            webhook_id {str} -- Webhook id, if ommited url is required
            url {str} -- endpoint url, if ommited id is required 
            event_type {str} -- The event name, one per request
            resource_version {str} -- event type version
        
        Returns:
            PaypalApiResponse[WebhookEvent] -- A response with the webhooks events.
        """
        body = {'event_type': event_type}

        if body['url'] != None:
            body['url'] = url
        if body['webhook_id'] != None:
            body['webhooks_id'] = webhook_id
        if body['resource_version'] != None:
            body['resource_version'] = resource_version

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

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

        return PaypalApiResponse.success(
            api_response,
            WebhookEvent.serialize_from_json(api_response.json()))
示例#7
0
    def void_payment(self,
                     authorization_id: str,
                     auth_assertion_token: str = None) -> PaypalApiResponse:
        """Calls the paypal API to void an authorized payment, by ID. 
           See the api docs for a full set of rules.
        
        Arguments:
            authorization_id {str} -- The authorization id

        Keyword Arguments:
            auth_assertion_token {str} -- PayPal-Auth-Assertion see: https://developer.paypal.com/docs/api/payments/v2/docs/api/reference/api-requests/#paypal-auth-assertion (default: {None})
            
        Returns:
            PaypalApiResponse -- An api response with the authorization details.
        """
        api_response = None
        url = parse_url(self._base_url, authorization_id, 'void')

        if auth_assertion_token:
            api_response = self._session.post(
                url,
                None,
                headers={'PayPal-Auth-Assertion': auth_assertion_token})
        else:
            api_response = self._session.post(url, None)

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

        return PaypalApiResponse(False, api_response)
示例#8
0
    def _execute_subscription_action(self,
                                     subscription_id: str,
                                     action_name: str,
                                     reason: str = None) -> PaypalApiResponse:
        """Executes a generic and simple subscription action call to the Paypal API
        
        Arguments:
            subscription_id {str} -- The subscription id
            action_name {str} -- API URL action name
        
        Keyword Arguments:
            reason {str} -- A comment or reason if needed (default: {None})
        
        Returns:
            PaypalApiResponse -- [description]
        """
        body = json.dumps({'reason': reason}) if reason else None
        url = parse_url(self._base_url, subscription_id, action_name)

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

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

        return PaypalApiResponse.success(api_response)
示例#9
0
    def update_quantity_in_subscription(
        self, subscription_id: str, update: SubscriptionQuantityUpdateRequest
    ) -> PaypalApiResponse[Subscription]:
        """Calls the paypal API to updates the quantity of the product or service in a subscription. 
           this method can also be used to to switch the plan and update the shipping_amount, 
           shipping_address values for the subscription. This type of update requires the buyer's consent.
        
        Arguments:
            subscription_id {str} -- the subscription id
            update {SubscriptionQuantityUpdateRequest} -- update request info.
        
        Returns:
            PaypalApiResponse[Subscription] -- Response status with subscription details
        """
        body = json.dumps(update.to_dict())
        url = parse_url(self._base_url, subscription_id, 'revise')

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

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

        return PaypalApiResponse.success(
            api_response,
            Subscription.serialize_from_json(api_response.json()))
示例#10
0
    def update_subscription(
            self, subscription_id: str,
            update_request: List[PatchUpdateRequest]) -> PaypalApiResponse:
        """Patch request to update a subscription. See the docs for details 
        
        Arguments:
            subscription_id {str} -- id of the subscription to be updated.
            update_request {PatchUpdateRequest} -- request object with update info.
        
        Returns:
            PaypalApiResponse -- Response obj with operation status
        """
        url = parse_url(self._base_url, subscription_id)
        body = json.dumps([{
            'op': x.operation,
            'value': x.value,
            'path': x.path
        } for x in update_request])

        api_response = self._session.patch(url, body)

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

        return PaypalApiResponse.success(api_response)
示例#11
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))
示例#12
0
    def create_batch_payout(self, sender_batch_header: SenderBatchHeader, items: List[PayoutItem], request_id: str = None) -> PaypalApiResponse[PayoutHeader]:
        """Calls the paypal API to create a batch payout to one or more recipients
        
        Arguments:
            sender_batch_header {SenderBatchHeader} -- Sender-provided payout header for a payout request.
            items {List[PayoutItem]} -- A list of individual payout items.
        
        Keyword Arguments:
            request_id {str} -- Request id for idempotence (default: {None})
        
        Returns:
            PaypalApiResponse[PayoutHeader] -- A response with the generated header.
        """
        headers = dict()
        url = self._base_url

        if request_id:
            headers['PayPal-Request-Id'] = request_id
        
        body = json.dumps({ sender_batch_header.to_dict(), items.to_dict() })
        api_response = self._session.post(url, body, headers = headers) if headers else self._session.post(url, body)

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

        j_data = api_response.json().get('batch_header')

        return PaypalApiResponse.success(api_response, PayoutHeader.serialize_from_json(j_data) if j_data else None)
示例#13
0
    def record_invoice_refund(
            self, invoice_id: str,
            refund_detail: RefundDetail) -> PaypalApiResponse:
        """Calls the API to record a refund for the invoice.
           If all payments are refunded, the invoice is marked as REFUNDED. 
           Otherwise, the invoice is marked as PARTIALLY REFUNDED.
        
        Arguments:
            invoice_id {str} -- id of the invoice
            payment_detail {RefundDetail} -- payment details
        
        Returns:
            PaypalApiResponse -- Api operation response status containing the response
        """
        url = parse_url(self._base_url, 'invoices', invoice_id, 'refunds')
        body = json.dumps(
            {k: v
             for k, v in refund_detail.to_dict().items() if v != None})

        response = self._session.post(url, body)

        if response.status_code != 204:
            return PaypalApiResponse.error(response)

        return PaypalApiResponse.success(response)
示例#14
0
    def generate_qr_code(self,
                         invoice_id: str,
                         width: int = 500,
                         height: int = 500,
                         action: str = 'pay') -> PaypalApiResponse:
        """ Calls the paypal API to generate a QR code for an invoice. 
            The QR code is a PNG image in Base64-encoded format that corresponds to the invoice ID. 
            You can generate a QR code for an invoice and add it to a paper or PDF invoice. 
            When customers use their mobile devices to scan the QR code, they are redirected to the PayPal mobile payment flow where they can view the invoice and pay online with PayPal or a credit card.
            Before you get a QR code, you must create an invoice and send an invoice to move the invoice from a draft to payable state. 
            Do not include an email address if you do not want the invoice emailed.
        
        Arguments:
            invoice_id {str} -- existing invoice id
        
        Keyword Arguments:
            width {int} -- The width, in pixels, of the QR code image. Value is from 150 to 500 (default: {500})
            height {int} -- The height, in pixels, of the QR code image. Value is from 150 to 500 (default: {500})
            action {str} -- The type of URL for which to generate a QR code. Valid values are 'pay' and 'details'. (default: {'pay'})
        
        Returns:
            PaypalApiResponse -- Api operation response status containing the response
        """
        url = parse_url(self._base_url, 'invoices', invoice_id,
                        'generate-qr-code')
        body = json.dumps({'width': width, 'height': height, 'action': action})
        response = self._session.post(url, body)

        if response.status_code != 200:
            return PaypalApiResponse.error(response)

        return PaypalApiResponse.success(response)
示例#15
0
    def send_invoice(
            self,
            invoice_id: str,
            subject: str,
            note: str,
            send_to_invoicer: bool,
            send_to_recipient: bool,
            additional_recipients: List[str] = [],
            paypal_request_id: str = None) -> PaypalApiResponse[Invoice]:
        """Calls the paypal API to send or schedule an invoice, by ID, to be sent to a customer. The action depends on the invoice issue date:
            If the invoice issue date is current or in the past, sends the invoice immediately.
            If the invoice issue date is in the future, schedules the invoice to be sent on that date.
            To suppress the merchant's email notification, set the send_to_invoicer body parameter to false. 
            To send the invoice through a share link and not through PayPal, set the send_to_recipient parameter to false in the notification object. 
            The send_to_recipient parameter does not apply to a future issue date because the invoice is scheduled to be sent through PayPal on that date.
        
        Arguments:
            invoice_id {str} -- invoice id
            subject {str} --  The subject of the email that is sent as a notification to the recipient.
            note {str} -- A note to the payer.
            send_to_invoicer {bool} -- Indicates whether to send a copy of the email to the merchant.
            send_to_recipient {bool} -- Indicates whether to send a copy of the email to the recipient.

        Keyword Arguments:
            paypal_request_id {str} -- Paypal request id for idempotence. (default: {None})
            additional_recipients {List[str]} -- An array of one or more CC: emails to which notifications are sent. (default: {[]})

        Returns:
            PaypalApiResponse -- Api operation response status with parsed objects within
        """
        response = None
        url = parse_url(self._base_url, 'invoices', invoice_id, 'send')

        body = json.dumps({
            'subject': subject,
            'note': note,
            'send_to_invoicer': send_to_invoicer,
            'send_to_recipient': send_to_recipient
        })

        if additional_recipients:
            body['additional_recipients'] = [{
                'email_address': x
            } for x in additional_recipients]

        if paypal_request_id:
            response = self._session.post(
                url, body, headers={'PayPal-Request-Id': paypal_request_id})
        else:
            response = self._session.post(url, body)

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

        if response.status_code == 200:
            return PaypalApiResponse.success(
                response, Invoice.serialize_from_json(response.json()))

        return PaypalApiResponse.success(response)
示例#16
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()))
示例#17
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()))
示例#18
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))
示例#19
0
 def delete_template(self, template_id: str) -> PaypalApiResponse:
     """Deletes a template by id
     
     Arguments:
         template_id {str} -- The id of the template to be deleted
     
     Returns:
         PaypalApiResponse -- API Response.
     """
     response = self._session.delete(parse_url(self._base_url, template_id))
     return PaypalApiResponse.success(
         response
     ) if response.status_code == 204 else PaypalApiResponse.error(response)
示例#20
0
    def show_payout_item_details(self, payout_item_id: str) -> PaypalApiResponse[PayoutItem]:
        """Calls the paypal API to show a payout item details
        
        Arguments:
            payout_item_id {str} -- Desired payout item id
        
        Returns:
            PaypalApiResponse[PayoutItem] -- Response with the item data if successful
        """        
        api_response = self._session.get(parse_url(self._base_url, payout_item_id))

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

        return PaypalApiResponse.success(api_response, PayoutItem.serialize_from_json(api_response.json()))
示例#21
0
    def show_refund_details(self, refund_id: str) -> PaypalApiResponse[Refund]:
        """Calls the paypal API to show details for an authorized payment, by ID.
        
        Arguments:
            refund_id {str} -- The refund id
        
        Returns:
            PaypalApiResponse[Refund] -- An api response with the refund details
        """
        api_response = self._session.get(parse_url(self._base_url, refund_id))

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

        return PaypalApiResponse(False, api_response, Refund.serialize_from_json(api_response.json()))
示例#22
0
    def _process_simple_response(self,
                                 api_response) -> PaypalApiResponse[Webhook]:
        """Process a simple webhook response
        
        Arguments:
            api_response {[type]} -- The http response obj from the request lib
        
        Returns:
            PaypalApiResponse[Webhook] -- Paypal response status with webhook object.
        """
        if api_response.status_code // 100 != 2:
            return PaypalApiResponse.error(api_response)

        return PaypalApiResponse.success(
            api_response, Webhook.serialize_from_json(api_response.json()))
示例#23
0
 def delete_webhook(self, webhook_id: str) -> PaypalApiResponse:
     """Calls the paypal API to delete a webhook by id.
     
     Arguments:
         webhook_id {str} -- Id of the Webhook to be deleted. 
     
     Returns:
         PaypalApiResponse -- Paypal response status
     """
     api_response = self._session.delete(
         parse_url(self._base_url, webhook_id))
     return PaypalApiResponse.error(
         api_response
     ) if api_response.status_code // 100 != 2 else PaypalApiResponse.success(
         api_response)
示例#24
0
    def show_payout_batch_details(
        self, payout_batch_id: str, page: int = 1, page_size: int = 1000, 
        fields: str = None, total_required: bool = True) -> PaypalApiResponse[PagedPayout]:
        
        url = parse_url(self._base_url, payout_batch_id)
        params = { 'page': page,  'page_size': page_size, 'total_required': total_required }

        if fields:
            params['fields'] = fields
        
        api_response = self._session.get(url, params)

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

        return PaypalApiResponse.success(api_response, PagedPayout.serialize_from_json(api_response.json()))
示例#25
0
    def create_draft_invoice(self, invoice: Invoice) -> PaypalApiResponse:
        """Calls the paypal API to generate an invoice draft.
        
        Arguments:            
            invoice {Invoice} -- the invoice information

        Returns:
            PaypalApiResponse -- The paypal API response
        """
        response = self._session.post(parse_url(self._base_url, 'invoices'),
                                      json.dumps(invoice.to_dict()))

        if response.status_code != 201:
            return PaypalApiResponse.error(response)

        return PaypalApiResponse.success(response)
示例#26
0
    def update_tracking_info_by_entity(
            self, tracker: Tracker) -> PaypalApiResponse[Tracker]:
        """Updates tracking info in the api
        
        Arguments:
            transaction_id {str} -- The id of the transaction
            tracking_number {int} -- The tracking number

        Keyword Arguments:
            mappings with the fields to be updated, see the docs for the names & values.
        
        Returns:
            PaypalApiResponse[Tracker] -- Response container with the response status
        """
        body = tracker.json_data
        url = tracker.update_link

        for item in tracker.to_dict().items():
            key = item.key
            if key in body.keys():
                body[key] = item.value

        api_response = self._execute_action_link(url, body)
        error = api_response.status_code != 204
        return PaypalApiResponse(error, api_response)
示例#27
0
    def show_template_details(self,
                              template_id: str) -> PaypalApiResponse[Template]:
        """Shows the details for a template by it's id
        
        Arguments:
            template_id {str} -- The template id
        
        Returns:
            PaypalApiResponse[Template] -- Response status with a Template object
        """
        response = self._session.get(parse_url(self._base_url, template_id))

        if response.status_code != 200:
            return PaypalApiResponse.error(response)

        return PaypalApiResponse.success(
            response, Invoice.serialize_from_json(response.json()))
示例#28
0
    def show_order_details(self, order_id: str) -> PaypalApiResponse[Order]:
        """Calls the api to retrieve the order details
        
        Arguments:
            order_id {str} -- The id of the order.
        
        Returns:
            PaypalApiResponse -- API operation response with the order if successful
        """
        api_response = self._session.get(parse_url(self._base_url, order_id))

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

        return PaypalApiResponse(
            False, api_response,
            Order.serialize_from_json(api_response.json()))
示例#29
0
    def send_invoice_reminder(
            self,
            invoice_id: str,
            subject: str,
            note: str,
            send_to_invoicer: bool,
            send_to_recipient: bool,
            additional_recipients: List[str] = []
    ) -> PaypalApiResponse[Invoice]:
        """Calls the paypal API to sends a reminder to the payer about an invoice, by ID. 
           In the JSON request body, include a notification object that defines the subject 
           of the reminder and other details. 
        
        Arguments:
            invoice_id {str} -- invoice id
            subject {str} --  The subject of the email that is sent as a notification to the recipient.
            note {str} -- A note to the payer.
            send_to_invoicer {bool} -- Indicates whether to send a copy of the email to the merchant.
            send_to_recipient {bool} -- Indicates whether to send a copy of the email to the recipient.
            additional_recipients {List[str]} -- An array of one or more CC: emails to which notifications are sent.
             If ignored, a notification is sent to all CC: email addresses that are part of the invoice.

        Returns:
            PaypalApiResponse -- Api operation response status with parsed objects within
        """
        url = parse_url(self._base_url, 'invoices', invoice_id, 'remind')

        body = json.dumps({
            'subject': subject,
            'note': note,
            'send_to_invoicer': send_to_invoicer,
            'send_to_recipient': send_to_recipient
        })

        if additional_recipients:
            body['additional_recipients'] = [{
                'email_address': x
            } for x in additional_recipients]

        response = self._session.post(url, body)

        if response.status_code != 204:
            return PaypalApiResponse.error(response)

        return PaypalApiResponse.success(response)
示例#30
0
    def show_event_notification_details(
            self, event_id: str) -> PaypalApiResponse[WebhookEvent]:
        """Calls the API to get the details for a webhook event notification.
        
        Arguments:
            event_id {str} -- The webhook notification event id
        
        Returns:
            PaypalApiResponse[WebhookEvent] -- Paypal response status with event obj.
        """
        api_response = self._session.get(parse_url(self._base_url, event_id))

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

        return PaypalApiResponse.success(
            api_response,
            WebhookEvent.serialize_from_json(api_response.json()))