Example #1
0
    def serialize_from_json(cls: Type[T], json_data: dict, response_type: ResponseType = ResponseType.MINIMAL) -> T:
        unit_amount, tax = None, None

        if 'unit_amount' in json_data.keys():
            unit_amount = Money.serialize_from_json(json_data['unit_amount'], response_type)
            
        if 'tax' in json_data.keys():
            tax = Money.serialize_from_json(json_data['tax'], response_type)
        
        return cls(
            json_data.get('name'), unit_amount, tax, json_data.get('quantity'),
            json_data.get('category'), json_data.get('description'), json_data.get('sku'),
            json_response= json_data, response_type = response_type
        )
Example #2
0
    def serialize_from_json(cls: Type[T], json_data: dict, response_type: ResponseType = ResponseType.MINIMAL) -> T:
        payable_amount, converted_amount, exchange_rate = None, None, None

        if 'payable_amount' in json_data.keys():
            payable_amount = Money.serialize_from_json(json_data['payable_amount', response_type])
        if 'converted_amount' in json_data.keys():
            converted_amount = Money.serialize_from_json(json_data['converted_amount', response_type])
        if 'exchange_rate' in json_data.keys():
            exchange_rate = ExchangeRate.serialize_from_json(json_data['exchange_rate', response_type])
        
        return cls(
            payable_amount, converted_amount, exchange_rate, 
            json_response = json_data, response_type = response_type
        )
Example #3
0
    def serialize_from_json(
            cls: Type[T],
            json_data: dict,
            response_type: ResponseType = ResponseType.MINIMAL) -> T:
        from_amount, to_amount = None, None

        if 'to_amount' in json_data.keys():
            to_amount = Currency.serialize_from_json(json_data['to_amount'],
                                                     response_type)
        if 'from_amount' in json_data.keys():
            from_amount = Currency.serialize_from_json(
                json_data['from_amount'], response_type)

        return cls(from_amount, to_amount, json_data.get('exchange_rate'))
Example #4
0
    def serialize_from_json(
            cls: Type[T],
            json_data: dict,
            response_type: ResponseType = ResponseType.MINIMAL) -> T:
        amount, seller_protection, status_details = None, None, None

        if 'amount' in json_data.keys():
            amount = Money.serialize_from_json(json_data['amount'],
                                               response_type)
        if 'seller_protection' in json_data.keys():
            seller_protection = SellerProtection.serialize_from_json(
                json_data['seller_protection'], response_type)
        if 'status_details' in json_data.keys():
            status_details = AuthStatusDetail.serialize_from_json(
                json_data['status_details'], response_type)

        return cls(json_data.get('id'),
                   json_data.get('status'),
                   amount,
                   status_details,
                   json_data.get('invoice_id'),
                   json_data.get('custom_id'),
                   seller_protection,
                   json_response=json_data,
                   response_type=response_type)
Example #5
0
 def serialize_from_json(
         cls: Type[T],
         json_data: dict,
         response_type: ResponseType = ResponseType.MINIMAL) -> T:
     return cls(Money.serialize_from_json(json_data['tax_amount']),
                json_response=json_data,
                response_type=response_type)
Example #6
0
    def accept_claim(self,
                     dispute_id: str,
                     refund_amount: Money = None,
                     **kwargs) -> PaypalApiBulkResponse[ActionLink]:
        """Calls the paypal API to accept a claim & close the dispute in favor of the customer
        
        Arguments:
            dispute_id {str} -- [description]

        Keyword Arguments:
            note {str} -- Notes & comments
            accept_claim_reason {str} -- Reason to accept the claim (default REASON_NOT_SET)
            invoice_id {str} -- The merchant-provided ID of the invoice for the refund. 
            return_shipping_address {PaypalPortableAddress} --  The return address for the item.
            refund_amount {Money} -- To accept a customer's claim, the amount that the merchant agrees to refund the customer.
        Returns:
            PaypalApiBulkResponse[ActionLink] -- action links related to the dispute
        """
        if refund_amount:
            kwargs['refund_amount'] = refund_amount.to_dict()
            self._remove_null_entries(kwargs['refund_amount'])

        if 'return_shipping_address' in kwargs.keys() and isinstance(
                kwargs['return_shipping_address'], PaypalPortableAddress):
            kwargs['return_shipping_address'] = kwargs[
                'return_shipping_address'].to_dict()
            self._remove_null_entries(kwargs['return_shipping_address'])

        return self._execute_basic_dispute_action(
            parse_url(self._base_url, dispute_id, 'accept-claim'), kwargs)
Example #7
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()))
Example #8
0
    def serialize_from_json(
            cls: Type[T],
            json_data: dict,
            response_type: ResponseType = ResponseType.MINIMAL) -> T:
        amount = Money.serialize_from_json(json_data['amount_refunded'])

        return cls(json_data['outcome_code'],
                   amount,
                   json_response=json_data,
                   response_type=response_type)
Example #9
0
    def serialize_from_json(cls: Type[T], json_data: dict, response_type: ResponseType = ResponseType.MINIMAL) -> T:
        breakdown = { 
            'item_total': None, 'shipping': None, 'handling': None, 
            'tax_total': None, 'insurance': None, 'shipping_discount': None, 
            'discount': None
        }

        breakdown = { k : Money.serialize_from_json(json_data[k], response_type) if k in json_data.keys() else None for k in breakdown }

        return cls(**breakdown, json_response = json_data, response_type = response_type)
Example #10
0
    def serialize_from_json(
            cls: Type[T],
            json_data: dict,
            response_type: ResponseType = ResponseType.MINIMAL) -> T:
        errors, amount, payout_item, payout_item_fee, currency_conversion, alternate_notification_method = None, None, None, None, None, None

        if 'errors' in json_data.keys():
            errors = PayoutError.serialize_from_json(json_data['errors'])
        if 'amount' in json_data.keys():
            amount = Currency.serialize_from_json(json_data['amount'],
                                                  response_type)
        if 'payout_item' in json_data.keys():
            payout_item = PayoutItemDetail.serialize_from_json(
                json_data['payout_item'], response_type)
        if 'payout_item_fee' in json_data.keys():
            payout_item_fee = Currency.serialize_from_json(
                json_data['payout_item_fee'], response_type)
        if 'currency_conversion' in json_data.keys():
            currency_conversion = CurrencyConversion.serialize_from_json(
                json_data['currency_conversion'], response_type)
        if 'alternate_notification_method' in json_data.keys():
            alternate_notification_method = PaypalPhoneDetail.serialize_from_json(
                json_data['alternate_notification_method'], response_type)

        return cls(json_data.get('payout_item_id'),
                   json_data.get('transaction_id'),
                   json_data.get('activity_id'),
                   json_data.get('transaction_status'),
                   payout_item_fee,
                   json_data.get('payout_batch_id'),
                   json_data.get('sender_batch_id'),
                   payout_item,
                   currency_conversion,
                   errors,
                   json_data.get('recipient_type'),
                   amount,
                   json_data.get('note'),
                   json_data.get('receiver'),
                   json_data.get('sender_item_id'),
                   json_data.get('recipient_wallet'),
                   alternate_notification_method,
                   json_response=json_data,
                   response_type=response_type)
Example #11
0
 def serialize_from_json(
         cls: Type[T],
         json_data: dict,
         response_type: ResponseType = ResponseType.MINIMAL) -> T:
     args = {**json_data}
     if 'incentive_amount' in json_data.keys():
         args['incentive_amount'] = Money.serialize_from_json(
             json_data['incentive_amount'])
     return cls(**args,
                json_response=json_data,
                response_type=response_type)
Example #12
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()))
Example #13
0
    def serialize_from_json(cls: Type[T], json_data: dict, response_type: ResponseType = ResponseType.MINIMAL) -> T:
        platform_fees = []
        gross_amount, paypal_fee, net_amount, receivable_amount, exchange_rate = None, None, None, None, None

        if 'gross_amount' in json_data.keys():
            gross_amount = Money.serialize_from_json(json_data['gross_amount'], response_type)
        if 'paypal_fee' in json_data.keys():
            paypal_fee = Money.serialize_from_json(json_data['paypal_fee'], response_type)
        if 'net_amount' in json_data.keys():
            net_amount = Money.serialize_from_json(json_data['net_amount'], response_type)
        if 'receivable_amount' in json_data.keys():
            receivable_amount = Money.serialize_from_json(json_data['receivable_amount'], response_type)
        
        if 'platform_fees' in json_data.keys():
            platform_fees = [PlatformFee.serialize_from_json(x, response_type) for x in json_data['platform_fees']]
        
        return cls(
            gross_amount, paypal_fee, net_amount, receivable_amount, 
            exchange_rate, platform_fees, json_response = json_data, 
            response_type = response_type
        )
Example #14
0
 def serialize_from_json(
         cls: Type[T],
         json_data: dict,
         response_type: ResponseType = ResponseType.MINIMAL) -> T:
     args = {
         **json_data,
         **{
             k: Money.serialize_from_json(v)
             for k, v in json_data.items() if k in cls._MONEY_TYPES
         }
     }
     return cls(**args,
                json_response=json_data,
                response_type=response_type)
Example #15
0
    def serialize_from_json(cls: Type[T], json_data: dict, response_type: ResponseType = ResponseType.MINIMAL) -> T:
        platform_fees, net_amount_breakdown = [], None
        gross_amount, paypal_fee, net_amount, total_refunded_amount = None, None, None, None

        if 'gross_amount' in json_data.keys():
            gross_amount = Money.serialize_from_json(json_data['gross_amount'], response_type)
        if 'paypal_fee' in json_data.keys():
            paypal_fee = Money.serialize_from_json(json_data['paypal_fee'], response_type)
        if 'net_amount' in json_data.keys():
            net_amount = Money.serialize_from_json(json_data['net_amount'], response_type)
        if 'total_refunded_amount' in json_data.keys():
            total_refunded_amount = Money.serialize_from_json(json_data['total_refunded_amount'], response_type)
        if 'net_amount_breakdown' in json_data.keys():
            net_amount_breakdown = RefundNetAmtBreakdown.serialize_from_json(json_data['net_amount_breakdown'], response_type)

        if 'platform_fees' in json_data.keys():
            platform_fees = [PlatformFee.serialize_from_json(x, response_type) for x in json_data['platform_fees']]
        
        return cls(
            gross_amount, paypal_fee, net_amount, total_refunded_amount, 
            net_amount_breakdown, platform_fees, json_response = json_data, 
            response_type = response_type
        )
Example #16
0
    def serialize_from_json(cls: Type[T], json_data: dict, response_type: ResponseType = ResponseType.MINIMAL) -> T:
        amount, status_details, seller_breakdown = None, None, None
        
        if 'amount' in json_data.keys():
            amount = Money.serialize_from_json(json_data['amount'], response_type)
        if 'status_details' in json_data.keys():
            status_details = RefundStatusDetail.serialize_from_json(json_data['status_details'], response_type)
        if 'seller_breakdown' in json_data.keys():
            seller_breakdown = SellerRefundBreakdown.serialize_from_json(json_data['seller_breakdown'], response_type)

        return cls(
            json_data.get('id'), json_data.get('status'), json_data.get('invoice_id'), 
            amount, json_data.get('note_to_payer'), status_details, seller_breakdown,
            json_response = json_data, response_type = response_type
        )
Example #17
0
    def serialize_from_json(
            cls: Type[T],
            json_data: dict,
            response_type: ResponseType = ResponseType.MINIMAL) -> T:
        amount, shipping = None, None

        if 'amount' in json_data.keys():
            amount = Money.serialize_from_json(json_data['amount'])
        if 'shipping_info' in json_data.keys():
            shipping = ShippingCost.serialize_from_json(
                json_data['shipping_info'])

        return cls(json_data['pd_type'],
                   json_data['payment_id'],
                   json_data['method'],
                   amount,
                   shipping,
                   json_response=json_data,
                   response_type=response_type)
Example #18
0
    def serialize_from_json(
            cls: Type[T],
            json_data: dict,
            response_type: ResponseType = ResponseType.MINIMAL) -> T:
        paid_amount, transactions = None, list()

        if 'paid_amount' in json_data.keys():
            paid_amount = Money.serialize_from_json(json_data['paid_amount'],
                                                    response_type)
        if 'transactions' in json_data.keys():
            transactions = [
                PaymentDetail.serialize_from_json(x, response_type)
                for x in json_data['transactions']
            ]

        return cls(paid_amount,
                   transactions,
                   json_response=json_data,
                   response_type=response_type)
Example #19
0
    def make_return_offer(
        self,
        dispute_id: str,
        note: str,
        offer_type: str,
        invoice_id: str = None,
        offer_amt: Money = None,
        return_addr: PaypalPortableAddress = None
    ) -> PaypalApiBulkResponse[ActionLink]:
        """Calls the API to make an offer to the other party to resolve a dispute
        
        Arguments:
            dispute_id {str} -- The dispute identifier
            note {str} -- Customer notes about the escalation
            offer_type {str} -- The merchant-proposed offer type for the dispute

        Keyword Arguments:
            offer_amt {Money} --  The amount proposed to resolve the dispute. 
            invoice_id {str} -- The merchant-provided ID of the invoice for the refund. 
            return_shipping_address {PaypalPortableAddress} --  The return address for the item.
            refund_amount {Money} -- To accept a customer's claim, the amount that the merchant agrees to refund the customer.

        Returns:
            PaypalApiBulkResponse[ActionLink] -- action links related to the dispute
        """
        body = {'note': note, 'offer_type': offer_type}

        if invoice_id:
            body['invoice_id'] = invoice_id

        if offer_amt:
            body['offer_amount'] = offer_amt.to_dict()
            self._remove_null_entries(body['offer_amount'])

        if return_addr:
            body['return_address'] = return_addr.to_dict()
            self._remove_null_entries(body['return_address'])

        return self._execute_basic_dispute_action(
            parse_url(self._base_url, dispute_id, 'make-offer'), body)
Example #20
0
    def serialize_from_json(
            cls: Type[T],
            json_data: dict,
            response_type: ResponseType = ResponseType.MINIMAL) -> T:
        amount, recipient_name = None, None

        if 'amount' in json_data.keys():
            amount = Currency.serialize_from_json(json_data['amount'],
                                                  response_type)
        if 'recipient_name' in json_data.keys():
            recipient_name = PaypalName.serialize_from_json(
                json_data['recipient_name'], response_type)

        return cls(json_data.get('recipient_type'),
                   amount,
                   json_data.get('note'),
                   json_data.get('receiver'),
                   json_data.get('sender_item_id'),
                   recipient_name,
                   json_data.get('recipient_wallet'),
                   json_response=json_data,
                   response_type=response_type)
Example #21
0
    def capture_authorized_payment_on_subscription(
            self,
            subscription_id: str,
            note: str,
            amount: Money,
            capture_type: str = 'OUTSTANDING_BALANCE',
            request_id: str = None) -> PaypalApiResponse[Capture]:
        """ Calls the Paypal API to perform a capture on an authorized payment 
            from the subscriber on the subscription.
        
        Arguments:
            subscription_id {str} -- the subscription id
            note {str} -- The reason or note for the subscription charge.
            amount {Money} -- The amount of the outstanding balance. Must not be greater than the current outstanding balance amount.
        
        Returns:
            PaypalApiResponse[Capture] -- A response with a capture
        """
        url = parse_url(self._base_url, subscription_id, 'capture')

        body = json.dumps({
            'note': note,
            'amount': amount.to_dict(),
            'capture_type': capture_type
        })

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

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

        j_data = api_response.json()
        return PaypalApiResponse.success(
            api_response,
            Capture.serialize_from_json(j_data) if j_data else None)
Example #22
0
 def serialize_from_json(
         cls: Type[T],
         json_data: dict,
         response_type: ResponseType = ResponseType.MINIMAL) -> T:
     args = {
         # Regular (primitive) arguments
         **json_data,
         # Serialized Money arguments
         **{
             k: Money.serialize_from_json(v)
             for k, v in json_data.items() if k in cls._MONEY_TYPES
         },
         # Serialized array arguments
         **{
             k: [
                 cls._PAYPAL_TYPE_ARRAYS[k].serialize_from_json(v) for v in json_data[k]
             ]
             for k in json_data.keys() if k in cls._PAYPAL_TYPE_ARRAYS
         }
     }
     return cls(**args,
                json_response=json_data,
                response_type=response_type)
Example #23
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()))