Skip to content

Commit

Permalink
Pre-fill billing information on PayPal.
Browse files Browse the repository at this point in the history
Handles blank phone number which would otherwise cause a "400 Bad Request" from PayPal.
  • Loading branch information
BenSturmfels committed Dec 12, 2018
1 parent 02931b0 commit 3c9080c
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions payments/paypal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,26 @@ def get_transactions_data(self, payment):
tax = payment.tax.quantize(CENTS, rounding=ROUND_HALF_UP)
delivery = payment.delivery.quantize(
CENTS, rounding=ROUND_HALF_UP)
# Pre-fill the PayPal billing address so the customer doesn't have to
# type it in again. Although the PayPal API calls this
# "shipping_address", the PayPal payment form labels it as "Billing
# address", hence we're passing the billing address.
#
# See: https://developer.paypal.com/docs/api/payments/v1/#definition-item_list
addr = payment.order.billing_address
billing_address = {
'recipient_name': ' '.join([addr.first_name, addr.last_name]),
'line1': addr.street_address_1,
'line2': addr.street_address_2,
'city': addr.city,
'state': addr.country_area,
'postal_code': addr.postal_code,
'country_code': addr.country.code,
}
if str(addr.phone):
# PayPal will return "HTTP/1.1 400 Bad Request" if you send a blank
# phone number.
billing_address['phone'] = str(addr.phone)
data = {
'intent': 'sale' if self._capture else 'authorize',
'transactions': [{'amount': {
Expand All @@ -189,7 +209,10 @@ def get_transactions_data(self, payment):
'subtotal': str(sub_total),
'tax': str(tax),
'shipping': str(delivery)}},
'item_list': {'items': items},
'item_list': {
'items': items,
'shipping_address': billing_address, # See comment above.
},
'description': payment.description}]}
return data

Expand All @@ -198,7 +221,12 @@ def get_product_data(self, payment, extra_data=None):
data = self.get_transactions_data(payment)
data['redirect_urls'] = {'return_url': return_url,
'cancel_url': return_url}
data['payer'] = {'payment_method': 'paypal'}
data['payer'] = {
'payment_method': 'paypal',
'payer_info': {
'email': payment.billing_email,
},
}
return data

def get_form(self, payment, data=None):
Expand Down

0 comments on commit 3c9080c

Please sign in to comment.