Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add stripe payment gateway
Unknownd81f250d3ff0
diff --git a/saleor/payment/gateways/stripe/__init__.py b/saleor/payment/gateways/stripe/__init__.pynew file mode 100644index 0000000000..bc2546e94d--- /dev/null+++ b/saleor/payment/gateways/stripe/__init__.py@@ -0,0 +1,236 @@+from django_countries import countries+import stripe++from ... import TransactionKind+from ...utils import create_transaction+from . import errors+from .forms import StripePaymentModalForm+from .utils import get_amount_for_stripe, get_amount_from_stripe+++# TODO: Add token to transactions and filter charge back from transactions+# TODO: Create a card and add address for fraud detection+# TODO: Add fraud details as https://github.com/mirumee/django-payments/blob/master/payments/stripe/forms.py+++def get_client_token(**_):+ """Not implemented for stripe gateway currently. The client token can+ generated by Stripe's checkout.js or stripe.js automatically.+ """+ return+++def get_client(**connection_params):+ stripe.api_key = connection_params.get('secret_key')+ return stripe+++def authorize(payment, payment_token, **connection_params):+ client, error = get_client(**connection_params), ''++ # Get amount from payment+ amount = payment.total++ try:+ # Authorize without capture+ response = _create_stripe_charge(+ client=client, payment=payment, amount=amount,+ payment_token=payment_token, capture=False)+ except stripe.error.StripeError as exc:+ response = _get_error_response_from_exc(exc)+ error = exc.user_message++ # Create transaction+ txn = _create_transaction(+ payment=payment,+ amount=amount,+ kind=TransactionKind.AUTH,+ response=response)++ return txn, error+++def capture(payment, amount, **connection_params):+ client, error = get_client(**connection_params), ''++ # Get amount from argument or payment, and convert to stripe's amount+ amount = amount or payment.total+ stripe_amount = get_amount_for_stripe(amount, payment.currency)++ try:+ # Retrieve stripe charge and capture specific amount+ stripe_charge = client.Charge.retrieve(payment.token)+ response = stripe_charge.capture(amount=stripe_amount)+ except stripe.error.StripeError as exc:+ response = _get_error_response_from_exc(exc)+ error = exc.user_message++ # Create transaction+ txn = _create_transaction(+ payment=payment,+ amount=amount,+ kind=TransactionKind.CAPTURE,+ response=response)++ return txn, error+++def charge(payment, payment_token, amount, **connection_params):+ client, error = get_client(**connection_params), ''++ # Get amount from argument or payment+ amount = amount or payment.total++ try:+ # Charge without pre-authorize+ response = _create_stripe_charge(+ client=client, payment=payment, amount=amount,+ payment_token=payment_token, capture=True)+ except stripe.error.StripeError as exc:+ response = _get_error_response_from_exc(exc)+ error = exc.user_message++ # Create transaction+ txn = _create_transaction(+ payment=payment,+ amount=amount,+ kind=TransactionKind.CHARGE,+ response=response)++ return txn, error+++def refund(payment, amount, **connection_params):+ client, error = get_client(**connection_params), ''++ # Get amount from argument or payment, and convert to stripe's amount+ amount = amount or payment.total+ stripe_amount = get_amount_for_stripe(amount, payment.currency)++ capture_txn = payment.transactions.filter(+ kind=TransactionKind.CHARGE, is_success=True).first()+ if not capture_txn:+ error = errors.ORDER_NOT_CHARGED+ response = dict()++ try:+ # Retrieve stripe charge and refund specific amount+ stripe_charge = client.Charge.retrieve(capture_txn.token)+ response = client.Refund.create(+ charge=stripe_charge.id, amount=stripe_amount)+ except stripe.error.StripeError as exc:+ response = _get_error_response_from_exc(exc)+ error = exc.user_message++ # Create transaction+ txn = _create_transaction(+ payment=payment,+ amount=amount,+ kind=TransactionKind.REFUND,+ response=response)++ return txn, error+++def void(payment, **connection_params):+ client, error = get_client(**connection_params), ''++ capture_txn = payment.transactions.filter(+ kind=TransactionKind.CHARGE, is_success=True).first()+ if not capture_txn:+ error = errors.ORDER_NOT_CHARGED+ response = dict()++ try:+ # Retrieve stripe charge and refund all+ stripe_charge = client.Charge.retrieve(capture_txn.token)+ response = client.Refund.create(charge=stripe_charge.id)+ except stripe.error.StripeError as exc:+ response = _get_error_response_from_exc(exc)+ error = exc.user_message++ # Create transaction+ txn = _create_transaction(+ payment=payment,+ amount=payment.total,+ kind=TransactionKind.VOID,+ response=response)++ return txn, error+++def get_form_class():+ return StripePaymentModalForm+++def _create_stripe_charge(client, payment, amount, payment_token, capture):+ """Create a charge with specific amount, ignoring payment's total."""+ # Get currency+ currency = str(payment.currency).upper()++ # Get appropriate amount for stripe+ stripe_amount = get_amount_for_stripe(amount, currency)++ # Get billing name from payment+ name = '%s %s' % (+ payment.billing_last_name, payment.billing_first_name)++ # Update shipping address to prevent fraud in Stripe+ shipping = dict(name=name, address=dict(+ line1=payment.order.shipping_address.street_address_1,+ line2=payment.order.shipping_address.street_address_2,+ city=payment.order.shipping_address.city,+ state=payment.order.shipping_address.country_area,+ country=dict(countries).get(+ payment.order.shipping_address.country, ''),+ postal_code=payment.order.shipping_address.postal_code,+ ))++ # Create stripe charge+ stripe_charge = client.Charge.create(+ capture=capture,+ amount=stripe_amount,+ currency=currency,+ source=payment_token,+ shipping=shipping,+ description=name)++ return stripe_charge+++def _create_transaction(payment, amount, kind, response):+ # Get currency from response or payment+ currency = response.get('currency', payment.currency)++ # Get amount from response or payment+ if 'amount' in response:+ amount = get_amount_from_stripe(response.get('amount'), currency)++ # Get token, which is an empty string for error responses+ token = response.get('id', '')++ # Check is_uccess from response+ is_success = (response.get('status') == 'succeeded')++ # Create transaction+ txn = create_transaction(+ payment=payment,+ token=token,+ kind=kind,+ is_success=is_success,+ amount=amount,+ currency=currency,+ gateway_response=response)++ return txn+++def _get_error_response_from_exc(exc):+ response = exc.json_body++ # Some errors from stripe don't json_body as None+ # such as stripe.error.InvalidRequestError+ if response is None:+ response = dict()++ return responsediff --git a/saleor/payment/gateways/stripe/errors.py b/saleor/payment/gateways/stripe/errors.pynew file mode 100644index 0000000000..d3d33fac29--- /dev/null+++ b/saleor/payment/gateways/stripe/errors.py@@ -0,0 +1,5 @@+from django.utils.translation import pgettext_lazy+++ORDER_NOT_CHARGED = pgettext_lazy(+ 'Razorpay payment error', 'Order was not charged.')diff --git a/saleor/payment/gateways/stripe/utils.py b/saleor/payment/gateways/stripe/utils.pynew file mode 100644index 0000000000..df2ba05c78--- /dev/null+++ b/saleor/payment/gateways/stripe/utils.py@@ -0,0 +1,21 @@+from decimal import Decimal+++# FIXME: The amount should be dynamically calculated by payment's currency.+# For example, amount will wrong for JPY since JPY does not support cents+++def get_amount_for_stripe(amount, currency):+ """Get appropriate amount for stripe.+ Stripe is using currency's smallest unit such as cents for USD.+ Stripe requires integer instead of decimal.+ """+ return int(amount * 100)+++def get_amount_from_stripe(amount, currency):+ """Get appropriate amount from stripe.+ Stripe is using currency's smallest unit such as cents for USD.+ Saleor requires decimal instead of float or integer.+ """+ return Decimal(amount / 100.0)