Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-fill billing information on PayPal (work in progress) #204

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions payments/paypal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ def get_transactions_data(self, payment):
total = payment.total.quantize(CENTS, rounding=ROUND_HALF_UP)
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
billing_address = {
"recipient_name": " ".join(
[payment.billing_first_name, payment.billing_last_name]
),
"line1": payment.billing_address_1,
"line2": payment.billing_address_2,
"city": payment.billing_city,
"state": payment.billing_country_area,
"postal_code": payment.billing_postcode,
"country_code": payment.billing_country_code,
}
data = {
"intent": "sale" if self._capture else "authorize",
"transactions": [
Expand All @@ -202,7 +219,10 @@ def get_transactions_data(self, payment):
"shipping": str(delivery),
},
},
"item_list": {"items": items},
"item_list": {
"items": items,
"shipping_address": billing_address, # See comment above.
},
"description": payment.description,
}
],
Expand All @@ -213,7 +233,12 @@ def get_product_data(self, payment, extra_data=None):
return_url = self.get_return_url(payment)
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
9 changes: 9 additions & 0 deletions payments/paypal/test_paypal.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ class Payment(Mock):
variant = VARIANT
transaction_id = None
message = ""
billing_first_name = "John"
billing_last_name = "Doe"
billing_address_1 = "Unit 4"
billing_address_2 = "5 Green Street"
billing_city = "Greenville"
billing_postcode = "1234"
billing_country_code = "US"
billing_country_area = "Some state"
billing_email = "[email protected]"
extra_data = json.dumps(
{
"links": {
Expand Down