diff --git a/doc/modules.rst b/doc/modules.rst index 745a7bc11..e5ab428be 100644 --- a/doc/modules.rst +++ b/doc/modules.rst @@ -199,6 +199,7 @@ PayPal :param client_id: Client ID assigned by PayPal or your email address :param secret: Secret assigned by PayPal :param endpoint: The API endpoint to use. For the production environment, use ``'https://api.paypal.com'`` instead + :param send_shipping_address: Whether to send a shipping address to Paypal. If ``True``, requires a method ``get_shipping_address_for_paypal`` on the ``Payment`` model which returns the address as a dict. :param capture: Whether to capture the payment automatically. See :ref:`capture-payments` for more details. @@ -210,6 +211,7 @@ Example:: 'client_id': 'user@example.com', 'secret': 'iseedeadpeople', 'endpoint': 'https://api.sandbox.paypal.com', + 'send_shipping_address': False, 'capture': False})} .. class:: payments.paypal.PaypalCardProvider(client_id, secret[, endpoint='https://api.sandbox.paypal.com']) @@ -264,7 +266,7 @@ Sofort.com :param id: Your sofort.com user id :param key: Your secret key :param project_id: Your sofort.com project id - :param endpoint: The API endpoint to use. + :param endpoint: The API endpoint to use. Example:: diff --git a/payments/paypal/__init__.py b/payments/paypal/__init__.py index fbeb2421e..63da7135f 100644 --- a/payments/paypal/__init__.py +++ b/payments/paypal/__init__.py @@ -59,10 +59,12 @@ class PaypalProvider(BasicProvider): paypal.com payment provider ''' def __init__(self, client_id, secret, - endpoint='https://api.sandbox.paypal.com', **kwargs): + endpoint='https://api.sandbox.paypal.com', + send_shipping_address=False, **kwargs): self.secret = secret self.client_id = client_id self.endpoint = endpoint + self.send_shipping_address = send_shipping_address self.oauth2_url = self.endpoint + '/v1/oauth2/token' self.payments_url = self.endpoint + '/v1/payments/payment' self.payment_execute_url = self.payments_url + '/%(id)s/execute/' @@ -180,17 +182,32 @@ def get_transactions_data(self, payment): tax = payment.tax.quantize(CENTS, rounding=ROUND_HALF_UP) delivery = payment.delivery.quantize( CENTS, rounding=ROUND_HALF_UP) + + if self.send_shipping_address: + item_list = { + 'items': items, + 'shipping_address': payment.get_shipping_address_for_paypal() + } + else: + item_list = {'items': items} + data = { 'intent': 'sale' if self._capture else 'authorize', - 'transactions': [{'amount': { - 'total': str(total), - 'currency': payment.currency, - 'details': { - 'subtotal': str(sub_total), - 'tax': str(tax), - 'shipping': str(delivery)}}, - 'item_list': {'items': items}, - 'description': payment.description}]} + 'transactions': [{ + 'amount': { + 'total': str(total), + 'currency': payment.currency, + 'details': { + 'subtotal': str(sub_total), + 'tax': str(tax), + 'shipping': str(delivery) + } + }, + 'item_list': item_list, + 'description': payment.description + }] + } + return data def get_product_data(self, payment, extra_data=None):