Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Handle payment void
Jakub Draganek5c4aa62dac98
diff --git a/saleor/payment/gateways/stripe_new/__init__.py b/saleor/payment/gateways/stripe_new/__init__.pyindex 33a14cadfc..a05d74a3a6 100644--- a/saleor/payment/gateways/stripe_new/__init__.py+++ b/saleor/payment/gateways/stripe_new/__init__.py@@ -118,7 +118,30 @@ def refund(payment_information: PaymentData, config: GatewayConfig) -> GatewayRedef void(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:- pass+ client = _get_client(**config.connection_params)+ try:+ intent = client.PaymentIntent.retrieve(id=payment_information.token)+ refund = intent["charges"]["data"][0].refund()+ response = GatewayResponse(+ is_success=refund.status == "succeeded",+ transaction_id=intent.id,+ amount=get_amount_from_stripe(intent.amount, intent.currency),+ currency=get_currency_from_stripe(refund.currency),+ error=None,+ kind=TransactionKind.VOID,+ raw_response=refund,+ )+ except stripe.error.StripeError as exc:+ response = GatewayResponse(+ is_success=False,+ transaction_id=payment_information.token,+ amount=payment_information.amount,+ currency=payment_information.currency,+ error=exc.user_message,+ kind=TransactionKind.VOID,+ raw_response=exc.json_body or {},+ )+ return responsedef process_payment(diff --git a/tests/gateway/stripe/cassettes/test_void.yaml b/tests/gateway/stripe/cassettes/test_void.yamlnew file mode 100644index 0000000000..c085787bd1--- /dev/null+++ b/tests/gateway/stripe/cassettes/test_void.yaml@@ -0,0 +1,217 @@+interactions:+- request:+ body: null+ headers:+ Accept:+ - '*/*'+ Accept-Encoding:+ - gzip, deflate+ Connection:+ - keep-alive+ User-Agent:+ - Stripe/v1 PythonBindings/2.33.0+ X-Stripe-Client-Telemetry:+ - '{"last_request_metrics": {"request_id": "req_ABPCzNFuuArBtR", "request_duration_ms":+ 4}}'+ 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_intents/pi_1F5BsOIUmJaD6Oqvmh5vBJIA+ response:+ body:+ string: "{\n \"id\": \"pi_1F5BsOIUmJaD6Oqvmh5vBJIA\",\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_1F5BsOIUmJaD6OqvbM6tKDjc\",\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_1F5BsOIUmJaD6OqvoDuvgoCK\",\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\": 1565271716,\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\": 8,\n \"seller_message\"\+ : \"Payment complete.\",\n \"type\": \"authorized\"\n },\n\+ \ \"paid\": true,\n \"payment_intent\": \"pi_1F5BsOIUmJaD6Oqvmh5vBJIA\"\+ ,\n \"payment_method\": \"pm_1F5BsOIUmJaD6OqvM6mSN7La\",\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_1F5BsOIUmJaD6OqvbM6tKDjc/rcpt_FaMbz3hC4CA0NuWiuFFISgfPltn2h4U\"\+ ,\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_1F5BsOIUmJaD6OqvbM6tKDjc/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_1F5BsOIUmJaD6Oqvmh5vBJIA\"\n\+ \ },\n \"client_secret\": \"pi_1F5BsOIUmJaD6Oqvmh5vBJIA_secret_bpJHb3g4rDKbYxIlWakCkartF\"\+ ,\n \"confirmation_method\": \"manual\",\n \"created\": 1565271716,\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_1F5BsOIUmJaD6OqvM6mSN7La\",\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:+ - '3995'+ Content-Type:+ - application/json+ Date:+ - Fri, 09 Aug 2019 08:41:06 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_YSwsImC7wigVHr+ stripe-version:+ - '2019-05-16'+ status:+ code: 200+ message: OK+- request:+ body: null+ headers:+ Accept:+ - '*/*'+ Accept-Encoding:+ - gzip, deflate+ Connection:+ - keep-alive+ Content-Length:+ - '0'+ Content-Type:+ - application/x-www-form-urlencoded+ Idempotency-Key:+ - 1ef4d5c4-e9f0-4d15-9633-00639291eebb+ User-Agent:+ - Stripe/v1 PythonBindings/2.33.0+ X-Stripe-Client-Telemetry:+ - '{"last_request_metrics": {"request_id": "req_YSwsImC7wigVHr", "request_duration_ms":+ 511}}'+ 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/charges/ch_1F5BsOIUmJaD6OqvbM6tKDjc/refund+ response:+ body:+ string: "{\n \"id\": \"ch_1F5BsOIUmJaD6OqvbM6tKDjc\",\n \"object\": \"charge\"\+ ,\n \"amount\": 4242,\n \"amount_refunded\": 4242,\n \"application\": null,\n\+ \ \"application_fee\": null,\n \"application_fee_amount\": null,\n \"balance_transaction\"\+ : \"txn_1F5BsOIUmJaD6OqvoDuvgoCK\",\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\": 1565271716,\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\"\+ : 8,\n \"seller_message\": \"Payment complete.\",\n \"type\": \"authorized\"\+ \n },\n \"paid\": true,\n \"payment_intent\": \"pi_1F5BsOIUmJaD6Oqvmh5vBJIA\"\+ ,\n \"payment_method\": \"pm_1F5BsOIUmJaD6OqvM6mSN7La\",\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_1F5BsOIUmJaD6OqvbM6tKDjc/rcpt_FaMbz3hC4CA0NuWiuFFISgfPltn2h4U\"\+ ,\n \"refunded\": true,\n \"refunds\": {\n \"object\": \"list\",\n \+ \ \"data\": [\n {\n \"id\": \"re_1F5TepIUmJaD6OqvC6ujUiMh\",\n\+ \ \"object\": \"refund\",\n \"amount\": 4242,\n \"balance_transaction\"\+ : \"txn_1F5TepIUmJaD6OqvqA291Jtj\",\n \"charge\": \"ch_1F5BsOIUmJaD6OqvbM6tKDjc\"\+ ,\n \"created\": 1565340067,\n \"currency\": \"usd\",\n \+ \ \"metadata\": {\n },\n \"reason\": null,\n \"receipt_number\"\+ : null,\n \"source_transfer_reversal\": null,\n \"status\":\+ \ \"succeeded\",\n \"transfer_reversal\": null\n }\n ],\n \+ \ \"has_more\": false,\n \"total_count\": 1,\n \"url\": \"/v1/charges/ch_1F5BsOIUmJaD6OqvbM6tKDjc/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"+ headers:+ Connection:+ - keep-alive+ Content-Length:+ - '2669'+ Content-Type:+ - application/json+ Date:+ - Fri, 09 Aug 2019 08:41:07 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:+ - 1ef4d5c4-e9f0-4d15-9633-00639291eebb+ original-request:+ - req_KPUCrhi56I6Zh0+ request-id:+ - req_KPUCrhi56I6Zh0+ stripe-version:+ - '2019-05-16'+ status:+ code: 200+ message: OK+version: 1diff --git a/tests/gateway/stripe/cassettes/test_void_error_response.yaml b/tests/gateway/stripe/cassettes/test_void_error_response.yamlnew file mode 100644index 0000000000..8b19b07a6a--- /dev/null+++ b/tests/gateway/stripe/cassettes/test_void_error_response.yaml@@ -0,0 +1,61 @@+interactions:+- request:+ body: null+ headers:+ Accept:+ - '*/*'+ Accept-Encoding:+ - gzip, deflate+ Connection:+ - keep-alive+ User-Agent:+ - Stripe/v1 PythonBindings/2.33.0+ X-Stripe-Client-Telemetry:+ - '{"last_request_metrics": {"request_id": "req_KPUCrhi56I6Zh0", "request_duration_ms":+ 3}}'+ 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_intents/THIS_INTENT_DOES_NOT_EXISTS+ 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:+ - Fri, 09 Aug 2019 08:44:27 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_fpe1Lbawiulevi+ stripe-version:+ - '2019-05-16'+ status:+ code: 404+ message: Not Found+version: 1diff --git a/tests/gateway/stripe/test_stripe.py b/tests/gateway/stripe/test_stripe.pyindex 158a0620ae..5047531390 100644--- a/tests/gateway/stripe/test_stripe.py+++ b/tests/gateway/stripe/test_stripe.py@@ -10,6 +10,7 @@ from saleor.payment.gateways.stripe_new import (authorize,capture,refund,+ void,)from saleor.payment.interface import GatewayConfigfrom saleor.payment.utils import create_payment_information@@ -20,7 +21,7 @@ TRANSACTION_CURRENCY = "USD"PAYMENT_METHOD_CARD_SIMPLE = "pm_card_pl"# Set to True if recording new cassette with sandbox using credentials in env-RECORD = True+RECORD = False@pytest.fixture()@@ -157,8 +158,8 @@ def stripe_paid_payment(stripe_payment):@pytest.mark.integration@pytest.mark.vcr(filter_headers=["authorization"])def test_refund(stripe_paid_payment, sandbox_gateway_config):- # Get id from sandbox for intent not yet captured- REFUND_AMOUNT = Decimal(10.0)+ # Get id from sandbox for succeeded payment+ REFUND_AMOUNT = Decimal(10.0) # partial refundINTENT_ID = "pi_1F5BsRIUmJaD6Oqvz2XMKZCD"payment_info = create_payment_information(stripe_paid_payment, amount=REFUND_AMOUNT, payment_token=INTENT_ID@@ -171,3 +172,51 @@ def test_refund(stripe_paid_payment, sandbox_gateway_config):assert response.is_successassert isclose(response.amount, REFUND_AMOUNT)assert response.currency == TRANSACTION_CURRENCY++[email protected](filter_headers=["authorization"])+def test_refund_error_response(stripe_payment, sandbox_gateway_config):+ INVALID_INTENT = "THIS_INTENT_DOES_NOT_EXISTS"+ payment_info = create_payment_information(stripe_payment, INVALID_INTENT)+ response = refund(payment_info, sandbox_gateway_config)++ assert response.error == "No such payment_intent: " + INVALID_INTENT+ assert response.transaction_id == INVALID_INTENT+ assert response.kind == TransactionKind.REFUND+ assert not response.is_success+ assert response.amount == stripe_payment.total+ assert response.currency == stripe_payment.currency++[email protected](filter_headers=["authorization"])+def test_void(stripe_paid_payment, sandbox_gateway_config):+ # Get id from sandbox for succedeed payment+ INTENT_ID = "pi_1F5BsOIUmJaD6Oqvmh5vBJIA"+ payment_info = create_payment_information(+ stripe_paid_payment, payment_token=INTENT_ID+ )+ response = void(payment_info, sandbox_gateway_config)++ assert not response.error+ assert response.transaction_id == INTENT_ID+ assert response.kind == TransactionKind.VOID+ assert response.is_success+ assert isclose(response.amount, TRANSACTION_AMOUNT)+ assert response.currency == TRANSACTION_CURRENCY++[email protected](filter_headers=["authorization"])+def test_void_error_response(stripe_payment, sandbox_gateway_config):+ INVALID_INTENT = "THIS_INTENT_DOES_NOT_EXISTS"+ payment_info = create_payment_information(stripe_payment, INVALID_INTENT)+ response = void(payment_info, sandbox_gateway_config)++ assert response.error == "No such payment_intent: " + INVALID_INTENT+ assert response.transaction_id == INVALID_INTENT+ assert response.kind == TransactionKind.VOID+ assert not response.is_success+ assert response.amount == stripe_payment.total+ assert response.currency == stripe_payment.currency