Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Extend stripe gateway implementation with card storage for customer

Jakub Draganek6c1bc802e13d
diff --git a/saleor/payment/__init__.py b/saleor/payment/__init__.py
index 15fb6fbcc0..422a42ebee 100644
--- a/saleor/payment/__init__.py
+++ b/saleor/payment/__init__.py
@@ -30,6 +30,7 @@ class OperationType(Enum):
CAPTURE = "capture"
VOID = "void"
REFUND = "refund"
+ CONFIRM = "confirm"
class TransactionError(Enum):
diff --git a/saleor/payment/gateways/stripe_new/__init__.py b/saleor/payment/gateways/stripe_new/__init__.py
index 06ea7bea38..bbb03c7dfd 100644
--- a/saleor/payment/gateways/stripe_new/__init__.py
+++ b/saleor/payment/gateways/stripe_new/__init__.py
@@ -1,9 +1,15 @@
-from typing import Dict
+from typing import Dict, List
import stripe
from ... import TransactionKind
-from ...interface import GatewayConfig, GatewayResponse, PaymentData
+from ...interface import (
+ CustomerSource,
+ CreditCardInfo,
+ GatewayConfig,
+ GatewayResponse,
+ PaymentData,
+)
from .forms import StripePaymentModalForm
from .utils import (
get_amount_for_stripe,
@@ -27,6 +33,8 @@ def authorize(
client = _get_client(**config.connection_params)
currency = get_currency_for_stripe(payment_information.currency)
stripe_amount = get_amount_for_stripe(payment_information.amount, currency)
+ future_use = "off_session" if config.store_customer else "on_session"
+ customer_id = PaymentData.customer_id if payment_information.reuse_source else None
try:
intent = client.PaymentIntent.create(
@@ -36,7 +44,12 @@ def authorize(
confirmation_method="manual",
confirm=True,
capture_method="automatic" if config.auto_capture else "manual",
+ setup_future_usage=future_use,
+ customer=customer_id,
)
+ if config.store_customer and not customer_id:
+ customer = client.Customer.create(payment_method=intent.payment_method)
+ customer_id = customer.id
response = GatewayResponse(
is_success=intent.status
in ("succeeded", "requires_capture", "requires_action"),
@@ -47,6 +60,7 @@ def authorize(
error=None,
kind=kind,
raw_response=intent,
+ customer_id=customer_id,
)
except stripe.error.StripeError as exc:
response = GatewayResponse(
@@ -58,12 +72,14 @@ def authorize(
error=exc.user_message,
kind=kind,
raw_response=exc.json_body or {},
+ customer_id=customer_id,
)
return response
def capture(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
client = _get_client(**config.connection_params)
+ intent = None
try:
intent = client.PaymentIntent.retrieve(id=payment_information.token)
capture = intent.capture()
@@ -94,6 +110,35 @@ def capture(payment_information: PaymentData, config: GatewayConfig) -> GatewayR
return response
+def confirm(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
+ client = _get_client(**config.connection_params)
+ try:
+ intent = client.PaymentIntent(id=payment_information.token)
+ intent.confirm()
+ response = GatewayResponse(
+ is_success=intent.status in ("succeeded"),
+ action_required=False,
+ transaction_id=intent.id,
+ amount=get_amount_from_stripe(intent.amount, intent.currency),
+ currency=get_currency_from_stripe(intent.currency),
+ error=None,
+ kind=TransactionKind.CAPTURE,
+ raw_response=capture,
+ )
+ except stripe.error.StripeError as exc:
+ response = GatewayResponse(
+ is_success=False,
+ action_required=False,
+ transaction_id=payment_information.token,
+ amount=payment_information.amount,
+ currency=payment_information.currency,
+ error=exc.user_message,
+ kind=TransactionKind.CAPTURE,
+ raw_response=exc.json_body or {},
+ )
+ return response
+
+
def refund(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
client = _get_client(**config.connection_params)
currency = get_currency_for_stripe(payment_information.currency)
@@ -154,6 +199,26 @@ def void(payment_information: PaymentData, config: GatewayConfig) -> GatewayResp
return response
+def list_client_sources(
+ config: GatewayConfig, customer_id: str
+) -> List[CustomerSource]:
+ client = _get_client(**config.connection_params)
+ cards = client.PaymentMethod.list(customer=customer_id, type="card")["data"]
+ return [
+ CustomerSource(
+ id=c.id,
+ gateway="stripe",
+ credit_card_info=CreditCardInfo(
+ exp_year=c.card.exp_year,
+ exp_month=c.card.exp_month,
+ last_4=c.card.last4,
+ name_on_card=None,
+ ),
+ )
+ for c in cards
+ ]
+
+
def process_payment(
payment_information: PaymentData, config: GatewayConfig
) -> GatewayResponse:
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 164db8d0d2..c7e9cf1879 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -58,6 +58,8 @@ def get_gateway_operation_func(gateway, operation_type):
return gateway.void
if operation_type == OperationType.REFUND:
return gateway.refund
+ if operation_type == OperationType.CONFIRM:
+ return gateway.confirm
def create_payment_information(
diff --git a/tests/gateway/stripe/cassettes/test_authorize_and_save_customer_id.yaml b/tests/gateway/stripe/cassettes/test_authorize_and_save_customer_id.yaml
new file mode 100644
index 0000000000..69d6666bbe
--- /dev/null
+++ b/tests/gateway/stripe/cassettes/test_authorize_and_save_customer_id.yaml
@@ -0,0 +1,203 @@
+interactions:
+- request:
+ body: payment_method=pm_card_pl&amount=9840&currency=usd&confirmation_method=manual&confirm=True&capture_method=automatic&setup_future_usage=off_session
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '146'
+ Content-Type:
+ - application/x-www-form-urlencoded
+ Idempotency-Key:
+ - c42f2724-bc37-4d67-893c-0751d2831eb7
+ User-Agent:
+ - Stripe/v1 PythonBindings/2.33.0
+ X-Stripe-Client-User-Agent:
+ - '{"bindings_version": "2.33.0", "lang": "python", "publisher": "stripe", "httplib":
+ "requests", "lang_version": "3.7.4", "platform": "Darwin-18.7.0-x86_64-i386-64bit",
+ "uname": "Darwin mbp-jakub.lan 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun
+ 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64 i386"}'
+ method: POST
+ uri: https://api.stripe.com/v1/payment_intents
+ response:
+ body:
+ string: "{\n \"id\": \"pi_1F6dCXIUmJaD6OqvYV4sLW9U\",\n \"object\": \"payment_intent\"\
+ ,\n \"amount\": 9840,\n \"amount_capturable\": 0,\n \"amount_received\"\
+ : 9840,\n \"application\": null,\n \"application_fee_amount\": null,\n \
+ \ \"canceled_at\": null,\n \"cancellation_reason\": null,\n \"capture_method\"\
+ : \"automatic\",\n \"charges\": {\n \"object\": \"list\",\n \"data\"\
+ : [\n {\n \"id\": \"ch_1F6dCXIUmJaD6OqvALz0AVIW\",\n \"\
+ object\": \"charge\",\n \"amount\": 9840,\n \"amount_refunded\"\
+ : 0,\n \"application\": null,\n \"application_fee\": null,\n\
+ \ \"application_fee_amount\": null,\n \"balance_transaction\"\
+ : \"txn_1F6dCXIUmJaD6OqvUIEx67pT\",\n \"billing_details\": {\n \
+ \ \"address\": {\n \"city\": null,\n \"country\"\
+ : null,\n \"line1\": null,\n \"line2\": null,\n \
+ \ \"postal_code\": null,\n \"state\": null\n },\n\
+ \ \"email\": null,\n \"name\": null,\n \"phone\"\
+ : null\n },\n \"captured\": true,\n \"created\": 1565615081,\n\
+ \ \"currency\": \"usd\",\n \"customer\": null,\n \"description\"\
+ : null,\n \"destination\": null,\n \"dispute\": null,\n \
+ \ \"failure_code\": null,\n \"failure_message\": null,\n \
+ \ \"fraud_details\": {\n },\n \"invoice\": null,\n \"\
+ livemode\": false,\n \"metadata\": {\n },\n \"on_behalf_of\"\
+ : null,\n \"order\": null,\n \"outcome\": {\n \"network_status\"\
+ : \"approved_by_network\",\n \"reason\": null,\n \"risk_level\"\
+ : \"normal\",\n \"risk_score\": 23,\n \"seller_message\"\
+ : \"Payment complete.\",\n \"type\": \"authorized\"\n },\n\
+ \ \"paid\": true,\n \"payment_intent\": \"pi_1F6dCXIUmJaD6OqvYV4sLW9U\"\
+ ,\n \"payment_method\": \"pm_1F6dCWIUmJaD6OqvCtcAnPSq\",\n \"\
+ payment_method_details\": {\n \"card\": {\n \"brand\"\
+ : \"visa\",\n \"checks\": {\n \"address_line1_check\"\
+ : null,\n \"address_postal_code_check\": null,\n \
+ \ \"cvc_check\": null\n },\n \"country\": \"PL\",\n\
+ \ \"exp_month\": 8,\n \"exp_year\": 2020,\n \
+ \ \"fingerprint\": \"aleqpF0PMAATcfmb\",\n \"funding\": \"credit\"\
+ ,\n \"last4\": \"0005\",\n \"three_d_secure\": null,\n\
+ \ \"wallet\": null\n },\n \"type\": \"card\"\n\
+ \ },\n \"receipt_email\": null,\n \"receipt_number\"\
+ : null,\n \"receipt_url\": \"https://pay.stripe.com/receipts/acct_1EzlmjIUmJaD6Oqv/ch_1F6dCXIUmJaD6OqvALz0AVIW/rcpt_FbquBAyeTl9NMntBmlqLH311NYu3gUp\"\
+ ,\n \"refunded\": false,\n \"refunds\": {\n \"object\"\
+ : \"list\",\n \"data\": [\n\n ],\n \"has_more\"\
+ : false,\n \"total_count\": 0,\n \"url\": \"/v1/charges/ch_1F6dCXIUmJaD6OqvALz0AVIW/refunds\"\
+ \n },\n \"review\": null,\n \"shipping\": null,\n \
+ \ \"source\": null,\n \"source_transfer\": null,\n \"statement_descriptor\"\
+ : null,\n \"statement_descriptor_suffix\": null,\n \"status\"\
+ : \"succeeded\",\n \"transfer_data\": null,\n \"transfer_group\"\
+ : null\n }\n ],\n \"has_more\": false,\n \"total_count\": 1,\n\
+ \ \"url\": \"/v1/charges?payment_intent=pi_1F6dCXIUmJaD6OqvYV4sLW9U\"\n\
+ \ },\n \"client_secret\": \"pi_1F6dCXIUmJaD6OqvYV4sLW9U_secret_8Y3cK3xVMLMDoEPrdc27csqzz\"\
+ ,\n \"confirmation_method\": \"manual\",\n \"created\": 1565615081,\n \"\
+ currency\": \"usd\",\n \"customer\": null,\n \"description\": null,\n \"\
+ invoice\": null,\n \"last_payment_error\": null,\n \"livemode\": false,\n\
+ \ \"metadata\": {\n },\n \"next_action\": null,\n \"on_behalf_of\": null,\n\
+ \ \"payment_method\": \"pm_1F6dCWIUmJaD6OqvCtcAnPSq\",\n \"payment_method_options\"\
+ : {\n \"card\": {\n \"request_three_d_secure\": \"automatic\"\n \
+ \ }\n },\n \"payment_method_types\": [\n \"card\"\n ],\n \"receipt_email\"\
+ : null,\n \"review\": null,\n \"setup_future_usage\": \"off_session\",\n\
+ \ \"shipping\": null,\n \"source\": null,\n \"statement_descriptor\": null,\n\
+ \ \"statement_descriptor_suffix\": null,\n \"status\": \"succeeded\",\n\
+ \ \"transfer_data\": null,\n \"transfer_group\": null\n}\n"
+ headers:
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '4005'
+ Content-Type:
+ - application/json
+ Date:
+ - Mon, 12 Aug 2019 13:04:42 GMT
+ Server:
+ - nginx
+ Strict-Transport-Security:
+ - max-age=31556926; includeSubDomains; preload
+ access-control-allow-credentials:
+ - 'true'
+ access-control-allow-methods:
+ - GET, POST, HEAD, OPTIONS, DELETE
+ access-control-allow-origin:
+ - '*'
+ access-control-expose-headers:
+ - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
+ access-control-max-age:
+ - '300'
+ cache-control:
+ - no-cache, no-store
+ idempotency-key:
+ - c42f2724-bc37-4d67-893c-0751d2831eb7
+ original-request:
+ - req_KJM1qzNiHhKCXC
+ request-id:
+ - req_KJM1qzNiHhKCXC
+ stripe-version:
+ - '2019-05-16'
+ status:
+ code: 200
+ message: OK
+- request:
+ body: payment_method=pm_1F6dCWIUmJaD6OqvCtcAnPSq
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '42'
+ Content-Type:
+ - application/x-www-form-urlencoded
+ Idempotency-Key:
+ - ce704720-d12d-4867-b304-6011774e9b81
+ User-Agent:
+ - Stripe/v1 PythonBindings/2.33.0
+ X-Stripe-Client-Telemetry:
+ - '{"last_request_metrics": {"request_id": "req_KJM1qzNiHhKCXC", "request_duration_ms":
+ 1873}}'
+ X-Stripe-Client-User-Agent:
+ - '{"bindings_version": "2.33.0", "lang": "python", "publisher": "stripe", "httplib":
+ "requests", "lang_version": "3.7.4", "platform": "Darwin-18.7.0-x86_64-i386-64bit",
+ "uname": "Darwin mbp-jakub.lan 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun
+ 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64 i386"}'
+ method: POST
+ uri: https://api.stripe.com/v1/customers
+ response:
+ body:
+ string: "{\n \"id\": \"cus_FbquUfgBnLdlsY\",\n \"object\": \"customer\",\n\
+ \ \"account_balance\": 0,\n \"address\": null,\n \"balance\": 0,\n \"\
+ created\": 1565615082,\n \"currency\": null,\n \"default_source\": null,\n\
+ \ \"delinquent\": false,\n \"description\": null,\n \"discount\": null,\n\
+ \ \"email\": null,\n \"invoice_prefix\": \"7829D842\",\n \"invoice_settings\"\
+ : {\n \"custom_fields\": null,\n \"default_payment_method\": null,\n\
+ \ \"footer\": null\n },\n \"livemode\": false,\n \"metadata\": {\n \
+ \ },\n \"name\": null,\n \"phone\": null,\n \"preferred_locales\": [\n\n\
+ \ ],\n \"shipping\": null,\n \"sources\": {\n \"object\": \"list\",\n\
+ \ \"data\": [\n\n ],\n \"has_more\": false,\n \"total_count\"\
+ : 0,\n \"url\": \"/v1/customers/cus_FbquUfgBnLdlsY/sources\"\n },\n \"\
+ subscriptions\": {\n \"object\": \"list\",\n \"data\": [\n\n ],\n\
+ \ \"has_more\": false,\n \"total_count\": 0,\n \"url\": \"/v1/customers/cus_FbquUfgBnLdlsY/subscriptions\"\
+ \n },\n \"tax_exempt\": \"none\",\n \"tax_ids\": {\n \"object\": \"\
+ list\",\n \"data\": [\n\n ],\n \"has_more\": false,\n \"total_count\"\
+ : 0,\n \"url\": \"/v1/customers/cus_FbquUfgBnLdlsY/tax_ids\"\n },\n \"\
+ tax_info\": null,\n \"tax_info_verification\": null\n}\n"
+ headers:
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '1117'
+ Content-Type:
+ - application/json
+ Date:
+ - Mon, 12 Aug 2019 13:04:42 GMT
+ Server:
+ - nginx
+ Strict-Transport-Security:
+ - max-age=31556926; includeSubDomains; preload
+ access-control-allow-credentials:
+ - 'true'
+ access-control-allow-methods:
+ - GET, POST, HEAD, OPTIONS, DELETE
+ access-control-allow-origin:
+ - '*'
+ access-control-expose-headers:
+ - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
+ access-control-max-age:
+ - '300'
+ cache-control:
+ - no-cache, no-store
+ idempotency-key:
+ - ce704720-d12d-4867-b304-6011774e9b81
+ original-request:
+ - req_aXRJjwtjBNsaQi
+ request-id:
+ - req_aXRJjwtjBNsaQi
+ stripe-version:
+ - '2019-05-16'
+ status:
+ code: 200
+ message: OK
+version: 1
diff --git a/tests/gateway/stripe/cassettes/test_authorize_with_customer_id.yaml b/tests/gateway/stripe/cassettes/test_authorize_with_customer_id.yaml
new file mode 100644
index 0000000000..4f3bf2ace4
--- /dev/null
+++ b/tests/gateway/stripe/cassettes/test_authorize_with_customer_id.yaml
@@ -0,0 +1,121 @@
+interactions:
+- request:
+ body: payment_method=pm_card_visa&amount=4242&currency=usd&confirmation_method=manual&confirm=True&capture_method=automatic&setup_future_usage=on_session
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '147'
+ Content-Type:
+ - application/x-www-form-urlencoded
+ Idempotency-Key:
+ - cbe6c60a-c66b-465a-99ce-1135e3dd3d60
+ User-Agent:
+ - Stripe/v1 PythonBindings/2.33.0
+ X-Stripe-Client-User-Agent:
+ - '{"bindings_version": "2.33.0", "lang": "python", "publisher": "stripe", "httplib":
+ "requests", "lang_version": "3.7.4", "platform": "Darwin-18.7.0-x86_64-i386-64bit",
+ "uname": "Darwin mbp-jakub.lan 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun
+ 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64 i386"}'
+ method: POST
+ uri: https://api.stripe.com/v1/payment_intents
+ response:
+ body:
+ string: "{\n \"id\": \"pi_1F6dW3IUmJaD6OqvfELxlKfH\",\n \"object\": \"payment_intent\"\
+ ,\n \"amount\": 4242,\n \"amount_capturable\": 0,\n \"amount_received\"\
+ : 4242,\n \"application\": null,\n \"application_fee_amount\": null,\n \
+ \ \"canceled_at\": null,\n \"cancellation_reason\": null,\n \"capture_method\"\
+ : \"automatic\",\n \"charges\": {\n \"object\": \"list\",\n \"data\"\
+ : [\n {\n \"id\": \"ch_1F6dW3IUmJaD6OqvnhqTybtH\",\n \"\
+ object\": \"charge\",\n \"amount\": 4242,\n \"amount_refunded\"\
+ : 0,\n \"application\": null,\n \"application_fee\": null,\n\
+ \ \"application_fee_amount\": null,\n \"balance_transaction\"\
+ : \"txn_1F6dW3IUmJaD6OqvZ1Hokmlt\",\n \"billing_details\": {\n \
+ \ \"address\": {\n \"city\": null,\n \"country\"\
+ : null,\n \"line1\": null,\n \"line2\": null,\n \
+ \ \"postal_code\": null,\n \"state\": null\n },\n\
+ \ \"email\": null,\n \"name\": null,\n \"phone\"\
+ : null\n },\n \"captured\": true,\n \"created\": 1565616291,\n\
+ \ \"currency\": \"usd\",\n \"customer\": null,\n \"description\"\
+ : null,\n \"destination\": null,\n \"dispute\": null,\n \
+ \ \"failure_code\": null,\n \"failure_message\": null,\n \
+ \ \"fraud_details\": {\n },\n \"invoice\": null,\n \"\
+ livemode\": false,\n \"metadata\": {\n },\n \"on_behalf_of\"\
+ : null,\n \"order\": null,\n \"outcome\": {\n \"network_status\"\
+ : \"approved_by_network\",\n \"reason\": null,\n \"risk_level\"\
+ : \"normal\",\n \"risk_score\": 29,\n \"seller_message\"\
+ : \"Payment complete.\",\n \"type\": \"authorized\"\n },\n\
+ \ \"paid\": true,\n \"payment_intent\": \"pi_1F6dW3IUmJaD6OqvfELxlKfH\"\
+ ,\n \"payment_method\": \"pm_1F6dW3IUmJaD6OqvWVN4WVwt\",\n \"\
+ payment_method_details\": {\n \"card\": {\n \"brand\"\
+ : \"visa\",\n \"checks\": {\n \"address_line1_check\"\
+ : null,\n \"address_postal_code_check\": null,\n \
+ \ \"cvc_check\": null\n },\n \"country\": \"US\",\n\
+ \ \"exp_month\": 8,\n \"exp_year\": 2020,\n \
+ \ \"fingerprint\": \"TtkCcsbSxUHACPLM\",\n \"funding\": \"credit\"\
+ ,\n \"last4\": \"4242\",\n \"three_d_secure\": null,\n\
+ \ \"wallet\": null\n },\n \"type\": \"card\"\n\
+ \ },\n \"receipt_email\": null,\n \"receipt_number\"\
+ : null,\n \"receipt_url\": \"https://pay.stripe.com/receipts/acct_1EzlmjIUmJaD6Oqv/ch_1F6dW3IUmJaD6OqvnhqTybtH/rcpt_FbrE3VnfCU4rAGDp6dmi6hwLCDcaBpL\"\
+ ,\n \"refunded\": false,\n \"refunds\": {\n \"object\"\
+ : \"list\",\n \"data\": [\n\n ],\n \"has_more\"\
+ : false,\n \"total_count\": 0,\n \"url\": \"/v1/charges/ch_1F6dW3IUmJaD6OqvnhqTybtH/refunds\"\
+ \n },\n \"review\": null,\n \"shipping\": null,\n \
+ \ \"source\": null,\n \"source_transfer\": null,\n \"statement_descriptor\"\
+ : null,\n \"statement_descriptor_suffix\": null,\n \"status\"\
+ : \"succeeded\",\n \"transfer_data\": null,\n \"transfer_group\"\
+ : null\n }\n ],\n \"has_more\": false,\n \"total_count\": 1,\n\
+ \ \"url\": \"/v1/charges?payment_intent=pi_1F6dW3IUmJaD6OqvfELxlKfH\"\n\
+ \ },\n \"client_secret\": \"pi_1F6dW3IUmJaD6OqvfELxlKfH_secret_8JVwKjwO7pbpBreupOjALTkxM\"\
+ ,\n \"confirmation_method\": \"manual\",\n \"created\": 1565616291,\n \"\
+ currency\": \"usd\",\n \"customer\": null,\n \"description\": null,\n \"\
+ invoice\": null,\n \"last_payment_error\": null,\n \"livemode\": false,\n\
+ \ \"metadata\": {\n },\n \"next_action\": null,\n \"on_behalf_of\": null,\n\
+ \ \"payment_method\": \"pm_1F6dW3IUmJaD6OqvWVN4WVwt\",\n \"payment_method_options\"\
+ : {\n \"card\": {\n \"request_three_d_secure\": \"automatic\"\n \
+ \ }\n },\n \"payment_method_types\": [\n \"card\"\n ],\n \"receipt_email\"\
+ : null,\n \"review\": null,\n \"setup_future_usage\": \"on_session\",\n\
+ \ \"shipping\": null,\n \"source\": null,\n \"statement_descriptor\": null,\n\
+ \ \"statement_descriptor_suffix\": null,\n \"status\": \"succeeded\",\n\
+ \ \"transfer_data\": null,\n \"transfer_group\": null\n}\n"
+ headers:
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '4004'
+ Content-Type:
+ - application/json
+ Date:
+ - Mon, 12 Aug 2019 13:24:52 GMT
+ Server:
+ - nginx
+ Strict-Transport-Security:
+ - max-age=31556926; includeSubDomains; preload
+ access-control-allow-credentials:
+ - 'true'
+ access-control-allow-methods:
+ - GET, POST, HEAD, OPTIONS, DELETE
+ access-control-allow-origin:
+ - '*'
+ access-control-expose-headers:
+ - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
+ access-control-max-age:
+ - '300'
+ cache-control:
+ - no-cache, no-store
+ idempotency-key:
+ - cbe6c60a-c66b-465a-99ce-1135e3dd3d60
+ original-request:
+ - req_bauQ1QyPH1PRtP
+ request-id:
+ - req_bauQ1QyPH1PRtP
+ stripe-version:
+ - '2019-05-16'
+ status:
+ code: 200
+ message: OK
+version: 1
diff --git a/tests/gateway/stripe/cassettes/test_confirm__intent.yaml b/tests/gateway/stripe/cassettes/test_confirm__intent.yaml
new file mode 100644
index 0000000000..fd6c36a640
--- /dev/null
+++ b/tests/gateway/stripe/cassettes/test_confirm__intent.yaml
@@ -0,0 +1,124 @@
+interactions:
+- request:
+ body: null
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '0'
+ Content-Type:
+ - application/x-www-form-urlencoded
+ Idempotency-Key:
+ - 90e66f61-6641-4975-8039-c185eb6befcd
+ User-Agent:
+ - Stripe/v1 PythonBindings/2.33.0
+ X-Stripe-Client-Telemetry:
+ - '{"last_request_metrics": {"request_id": "req_fpe1Lbawiulevi", "request_duration_ms":
+ 6}}'
+ X-Stripe-Client-User-Agent:
+ - '{"bindings_version": "2.33.0", "lang": "python", "publisher": "stripe", "httplib":
+ "requests", "lang_version": "3.7.4", "platform": "Darwin-18.7.0-x86_64-i386-64bit",
+ "uname": "Darwin mbp-jakub.lan 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun
+ 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64 i386"}'
+ method: POST
+ uri: https://api.stripe.com/v1/payment_intents/pi_1F6bslIUmJaD6Oqv1MNDaBSv/confirm
+ response:
+ body:
+ string: "{\n \"id\": \"pi_1F6bslIUmJaD6Oqv1MNDaBSv\",\n \"object\": \"payment_intent\"\
+ ,\n \"amount\": 4500,\n \"amount_capturable\": 0,\n \"amount_received\"\
+ : 4500,\n \"application\": null,\n \"application_fee_amount\": null,\n \
+ \ \"canceled_at\": null,\n \"cancellation_reason\": null,\n \"capture_method\"\
+ : \"automatic\",\n \"charges\": {\n \"object\": \"list\",\n \"data\"\
+ : [\n {\n \"id\": \"ch_1F6cKFIUmJaD6Oqvlj3NIJXD\",\n \"\
+ object\": \"charge\",\n \"amount\": 4500,\n \"amount_refunded\"\
+ : 0,\n \"application\": null,\n \"application_fee\": null,\n\
+ \ \"application_fee_amount\": null,\n \"balance_transaction\"\
+ : \"txn_1F6cKGIUmJaD6OqvUWnb5Kn4\",\n \"billing_details\": {\n \
+ \ \"address\": {\n \"city\": null,\n \"country\"\
+ : null,\n \"line1\": null,\n \"line2\": null,\n \
+ \ \"postal_code\": null,\n \"state\": null\n },\n\
+ \ \"email\": null,\n \"name\": null,\n \"phone\"\
+ : null\n },\n \"captured\": true,\n \"created\": 1565611715,\n\
+ \ \"currency\": \"usd\",\n \"customer\": null,\n \"description\"\
+ : null,\n \"destination\": null,\n \"dispute\": null,\n \
+ \ \"failure_code\": null,\n \"failure_message\": null,\n \
+ \ \"fraud_details\": {\n },\n \"invoice\": null,\n \"\
+ livemode\": false,\n \"metadata\": {\n },\n \"on_behalf_of\"\
+ : null,\n \"order\": null,\n \"outcome\": {\n \"network_status\"\
+ : \"approved_by_network\",\n \"reason\": null,\n \"risk_level\"\
+ : \"normal\",\n \"risk_score\": 57,\n \"seller_message\"\
+ : \"Payment complete.\",\n \"type\": \"authorized\"\n },\n\
+ \ \"paid\": true,\n \"payment_intent\": \"pi_1F6bslIUmJaD6Oqv1MNDaBSv\"\
+ ,\n \"payment_method\": \"pm_1F6bslIUmJaD6OqvcjuBzexd\",\n \"\
+ payment_method_details\": {\n \"card\": {\n \"brand\"\
+ : \"visa\",\n \"checks\": {\n \"address_line1_check\"\
+ : null,\n \"address_postal_code_check\": null,\n \
+ \ \"cvc_check\": null\n },\n \"country\": \"US\",\n\
+ \ \"exp_month\": 8,\n \"exp_year\": 2020,\n \
+ \ \"fingerprint\": \"TtkCcsbSxUHACPLM\",\n \"funding\": \"credit\"\
+ ,\n \"last4\": \"4242\",\n \"three_d_secure\": null,\n\
+ \ \"wallet\": null\n },\n \"type\": \"card\"\n\
+ \ },\n \"receipt_email\": null,\n \"receipt_number\"\
+ : null,\n \"receipt_url\": \"https://pay.stripe.com/receipts/acct_1EzlmjIUmJaD6Oqv/ch_1F6cKFIUmJaD6Oqvlj3NIJXD/rcpt_Fbq097y9S9bnd5cHgH6oa87TlAT0ta6\"\
+ ,\n \"refunded\": false,\n \"refunds\": {\n \"object\"\
+ : \"list\",\n \"data\": [\n\n ],\n \"has_more\"\
+ : false,\n \"total_count\": 0,\n \"url\": \"/v1/charges/ch_1F6cKFIUmJaD6Oqvlj3NIJXD/refunds\"\
+ \n },\n \"review\": null,\n \"shipping\": null,\n \
+ \ \"source\": null,\n \"source_transfer\": null,\n \"statement_descriptor\"\
+ : null,\n \"statement_descriptor_suffix\": null,\n \"status\"\
+ : \"succeeded\",\n \"transfer_data\": null,\n \"transfer_group\"\
+ : null\n }\n ],\n \"has_more\": false,\n \"total_count\": 1,\n\
+ \ \"url\": \"/v1/charges?payment_intent=pi_1F6bslIUmJaD6Oqv1MNDaBSv\"\n\
+ \ },\n \"client_secret\": \"pi_1F6bslIUmJaD6Oqv1MNDaBSv_secret_vQUIoOL8zbTh0ZVXEdzKwnU7O\"\
+ ,\n \"confirmation_method\": \"automatic\",\n \"created\": 1565610011,\n\
+ \ \"currency\": \"usd\",\n \"customer\": null,\n \"description\": null,\n\
+ \ \"invoice\": null,\n \"last_payment_error\": null,\n \"livemode\": false,\n\
+ \ \"metadata\": {\n },\n \"next_action\": null,\n \"on_behalf_of\": null,\n\
+ \ \"payment_method\": \"pm_1F6bslIUmJaD6OqvcjuBzexd\",\n \"payment_method_options\"\
+ : {\n \"card\": {\n \"request_three_d_secure\": \"automatic\"\n \
+ \ }\n },\n \"payment_method_types\": [\n \"card\"\n ],\n \"receipt_email\"\
+ : null,\n \"review\": null,\n \"setup_future_usage\": null,\n \"shipping\"\
+ : null,\n \"source\": null,\n \"statement_descriptor\": null,\n \"statement_descriptor_suffix\"\
+ : null,\n \"status\": \"succeeded\",\n \"transfer_data\": null,\n \"transfer_group\"\
+ : null\n}\n"
+ headers:
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '3999'
+ Content-Type:
+ - application/json
+ Date:
+ - Mon, 12 Aug 2019 12:08:36 GMT
+ Server:
+ - nginx
+ Strict-Transport-Security:
+ - max-age=31556926; includeSubDomains; preload
+ access-control-allow-credentials:
+ - 'true'
+ access-control-allow-methods:
+ - GET, POST, HEAD, OPTIONS, DELETE
+ access-control-allow-origin:
+ - '*'
+ access-control-expose-headers:
+ - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
+ access-control-max-age:
+ - '300'
+ cache-control:
+ - no-cache, no-store
+ idempotency-key:
+ - 90e66f61-6641-4975-8039-c185eb6befcd
+ original-request:
+ - req_sAaLx3X78HhJGo
+ request-id:
+ - req_sAaLx3X78HhJGo
+ stripe-version:
+ - '2019-05-16'
+ status:
+ code: 200
+ message: OK
+version: 1
diff --git a/tests/gateway/stripe/cassettes/test_confirm_error_response.yaml b/tests/gateway/stripe/cassettes/test_confirm_error_response.yaml
new file mode 100644
index 0000000000..aef82cea16
--- /dev/null
+++ b/tests/gateway/stripe/cassettes/test_confirm_error_response.yaml
@@ -0,0 +1,68 @@
+interactions:
+- request:
+ body: null
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '0'
+ Content-Type:
+ - application/x-www-form-urlencoded
+ Idempotency-Key:
+ - 65ce06a3-1e17-4495-a81d-5206e03546a1
+ User-Agent:
+ - Stripe/v1 PythonBindings/2.33.0
+ X-Stripe-Client-User-Agent:
+ - '{"bindings_version": "2.33.0", "lang": "python", "publisher": "stripe", "httplib":
+ "requests", "lang_version": "3.7.4", "platform": "Darwin-18.7.0-x86_64-i386-64bit",
+ "uname": "Darwin mbp-jakub.lan 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun
+ 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64 i386"}'
+ method: POST
+ uri: https://api.stripe.com/v1/payment_intents/THIS_INTENT_DOES_NOT_EXISTS/confirm
+ response:
+ body:
+ string: "{\n \"error\": {\n \"code\": \"resource_missing\",\n \"doc_url\"\
+ : \"https://stripe.com/docs/error-codes/resource-missing\",\n \"message\"\
+ : \"No such payment_intent: THIS_INTENT_DOES_NOT_EXISTS\",\n \"param\"\
+ : \"intent\",\n \"type\": \"invalid_request_error\"\n }\n}\n"
+ headers:
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '253'
+ Content-Type:
+ - application/json
+ Date:
+ - Mon, 12 Aug 2019 12:42:33 GMT
+ Server:
+ - nginx
+ Strict-Transport-Security:
+ - max-age=31556926; includeSubDomains; preload
+ access-control-allow-credentials:
+ - 'true'
+ access-control-allow-methods:
+ - GET, POST, HEAD, OPTIONS, DELETE
+ access-control-allow-origin:
+ - '*'
+ access-control-expose-headers:
+ - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
+ access-control-max-age:
+ - '300'
+ cache-control:
+ - no-cache, no-store
+ idempotency-key:
+ - 65ce06a3-1e17-4495-a81d-5206e03546a1
+ original-request:
+ - req_RnWTTJZjPLfv56
+ request-id:
+ - req_RnWTTJZjPLfv56
+ stripe-version:
+ - '2019-05-16'
+ status:
+ code: 404
+ message: Not Found
+version: 1
diff --git a/tests/gateway/stripe/cassettes/test_list_customer_sources.yaml b/tests/gateway/stripe/cassettes/test_list_customer_sources.yaml
new file mode 100644
index 0000000000..f2b834943b
--- /dev/null
+++ b/tests/gateway/stripe/cassettes/test_list_customer_sources.yaml
@@ -0,0 +1,71 @@
+interactions:
+- request:
+ body: null
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Connection:
+ - keep-alive
+ User-Agent:
+ - Stripe/v1 PythonBindings/2.33.0
+ X-Stripe-Client-User-Agent:
+ - '{"bindings_version": "2.33.0", "lang": "python", "publisher": "stripe", "httplib":
+ "requests", "lang_version": "3.7.4", "platform": "Darwin-18.7.0-x86_64-i386-64bit",
+ "uname": "Darwin mbp-jakub.lan 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun
+ 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64 i386"}'
+ method: GET
+ uri: https://api.stripe.com/v1/payment_methods?customer=cus_FbquUfgBnLdlsY&type=card
+ response:
+ body:
+ string: "{\n \"object\": \"list\",\n \"data\": [\n {\n \"id\": \"\
+ pm_1F6dCWIUmJaD6OqvCtcAnPSq\",\n \"object\": \"payment_method\",\n \
+ \ \"billing_details\": {\n \"address\": {\n \"city\": null,\n\
+ \ \"country\": null,\n \"line1\": null,\n \"line2\"\
+ : null,\n \"postal_code\": null,\n \"state\": null\n \
+ \ },\n \"email\": null,\n \"name\": null,\n \"phone\"\
+ : null\n },\n \"card\": {\n \"brand\": \"visa\",\n \
+ \ \"checks\": {\n \"address_line1_check\": null,\n \"address_postal_code_check\"\
+ : null,\n \"cvc_check\": null\n },\n \"country\": \"\
+ PL\",\n \"exp_month\": 8,\n \"exp_year\": 2020,\n \"\
+ fingerprint\": \"aleqpF0PMAATcfmb\",\n \"funding\": \"credit\",\n \
+ \ \"generated_from\": null,\n \"last4\": \"0005\",\n \"\
+ three_d_secure_usage\": {\n \"supported\": true\n },\n \
+ \ \"wallet\": null\n },\n \"created\": 1565615081,\n \"\
+ customer\": \"cus_FbquUfgBnLdlsY\",\n \"livemode\": false,\n \"\
+ metadata\": {\n },\n \"type\": \"card\"\n }\n ],\n \"has_more\"\
+ : false,\n \"url\": \"/v1/payment_methods\"\n}\n"
+ headers:
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '1110'
+ Content-Type:
+ - application/json
+ Date:
+ - Tue, 13 Aug 2019 07:31:00 GMT
+ Server:
+ - nginx
+ Strict-Transport-Security:
+ - max-age=31556926; includeSubDomains; preload
+ access-control-allow-credentials:
+ - 'true'
+ access-control-allow-methods:
+ - GET, POST, HEAD, OPTIONS, DELETE
+ access-control-allow-origin:
+ - '*'
+ access-control-expose-headers:
+ - Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
+ access-control-max-age:
+ - '300'
+ cache-control:
+ - no-cache, no-store
+ request-id:
+ - req_yh7op4y5fX8eF2
+ stripe-version:
+ - '2019-05-16'
+ status:
+ code: 200
+ message: OK
+version: 1
diff --git a/tests/gateway/stripe/test_stripe.py b/tests/gateway/stripe/test_stripe.py
index bffea96f2b..5b087d32ee 100644
--- a/tests/gateway/stripe/test_stripe.py
+++ b/tests/gateway/stripe/test_stripe.py
@@ -9,10 +9,12 @@ from saleor.payment.gateways.stripe_new import (
TransactionKind,
authorize,
capture,
+ confirm,
+ list_client_sources,
refund,
void,
)
-from saleor.payment.interface import GatewayConfig
+from saleor.payment.interface import CreditCardInfo, CustomerSource, GatewayConfig
from saleor.payment.utils import create_payment_information
TRANSACTION_AMOUNT = Decimal(42.42)
@@ -124,6 +126,36 @@ def test_authorize_without_capture(stripe_payment, sandbox_gateway_config):
assert response.is_success is True
[email protected](filter_headers=["authorization"])
+def test_authorize_and_save_customer_id(payment_dummy, sandbox_gateway_config):
+ CUSTOMER_ID = "cus_FbquUfgBnLdlsY" # retrieved from sandbox
+ payment = payment_dummy
+
+ payment_info = create_payment_information(payment, PAYMENT_METHOD_CARD_SIMPLE)
+
+ sandbox_gateway_config.store_customer = True
+ response = authorize(payment_info, sandbox_gateway_config)
+ assert not response.error
+ assert response.customer_id == CUSTOMER_ID
+
+
[email protected](filter_headers=["authorization"])
+def test_authorize_with_customer_id(payment_dummy, sandbox_gateway_config):
+ CUSTOMER_ID = "cus_FbquUfgBnLdlsY" # retrieved from sandbox
+ payment = payment_dummy
+
+ payment_info = create_payment_information(payment, "pm_card_visa")
+ payment_info.amount = TRANSACTION_AMOUNT
+ payment_info.customer_id = CUSTOMER_ID
+ payment_info.reuse_source = True
+
+ response = authorize(payment_info, sandbox_gateway_config)
+ assert not response.error
+ assert response.is_success
+
+
@pytest.fixture()
def stripe_authorized_payment(stripe_payment):
stripe_payment.charge_status = ChargeStatus.NOT_CHARGED
@@ -256,3 +288,50 @@ def test_void_error_response(stripe_payment, sandbox_gateway_config):
assert not response.is_success
assert response.amount == stripe_payment.total
assert response.currency == stripe_payment.currency
+
+
[email protected](filter_headers=["authorization"])
+def test_confirm__intent(stripe_payment, sandbox_gateway_config):
+ PAYMENT_INTENT = (
+ "pi_1F6bslIUmJaD6Oqv1MNDaBSv"
+ ) # PI with status "requires_confirmation"
+ payment_info = create_payment_information(stripe_payment, PAYMENT_INTENT)
+ response = confirm(payment_info, sandbox_gateway_config)
+ assert not response.error
+ assert response.kind == TransactionKind.CAPTURE
+ assert isclose(response.amount, 45.0)
+ assert response.currency == TRANSACTION_CURRENCY
+ assert response.is_success
+ assert not response.action_required
+
+
[email protected](filter_headers=["authorization"])
+def test_confirm_error_response(stripe_payment, sandbox_gateway_config):
+ INVALID_INTENT = "THIS_INTENT_DOES_NOT_EXISTS"
+ payment_info = create_payment_information(stripe_payment, INVALID_INTENT)
+ response = confirm(payment_info, sandbox_gateway_config)
+
+ assert response.error == "No such payment_intent: " + INVALID_INTENT
+ assert response.transaction_id == INVALID_INTENT
+ assert response.kind == TransactionKind.CAPTURE
+ assert not response.is_success
+ assert response.amount == stripe_payment.total
+ assert response.currency == stripe_payment.currency
+
+
[email protected](filter_headers=["authorization"])
+def test_list_customer_sources(sandbox_gateway_config):
+ CUSTOMER_ID = "cus_FbquUfgBnLdlsY" # retrieved from sandbox
+ expected_credit_card = CreditCardInfo(
+ last_4="0005", exp_year=2020, exp_month=8, name_on_card=None
+ )
+ expected_customer_source = CustomerSource(
+ id="pm_1F6dCWIUmJaD6OqvCtcAnPSq",
+ gateway="stripe",
+ credit_card_info=expected_credit_card,
+ )
+ sources = list_client_sources(sandbox_gateway_config, CUSTOMER_ID)
+ assert sources == [expected_customer_source]