Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Handle refunds
Jakub Draganekb7a5a584eb0c
diff --git a/saleor/payment/gateways/stripe_new/__init__.py b/saleor/payment/gateways/stripe_new/__init__.pyindex 444264f4a8..33a14cadfc 100644--- a/saleor/payment/gateways/stripe_new/__init__.py+++ b/saleor/payment/gateways/stripe_new/__init__.py@@ -89,7 +89,32 @@ def capture(payment_information: PaymentData, config: GatewayConfig) -> GatewayRdef refund(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:- pass+ client = _get_client(**config.connection_params)+ currency = get_currency_for_stripe(payment_information.currency)+ stripe_amount = get_amount_for_stripe(payment_information.amount, currency)+ try:+ intent = client.PaymentIntent.retrieve(id=payment_information.token)+ refund = intent["charges"]["data"][0].refund(amount=stripe_amount)+ response = GatewayResponse(+ is_success=refund.status == "succeeded",+ transaction_id=intent.id,+ amount=payment_information.amount,+ currency=get_currency_from_stripe(refund.currency),+ error=None,+ kind=TransactionKind.REFUND,+ 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.REFUND,+ raw_response=exc.json_body or {},+ )+ return responsedef void(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:diff --git a/tests/gateway/stripe/cassettes/test_refund.yaml b/tests/gateway/stripe/cassettes/test_refund.yamlnew file mode 100644index 0000000000..2b55a2667f--- /dev/null+++ b/tests/gateway/stripe/cassettes/test_refund.yaml@@ -0,0 +1,218 @@+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_J74zANRVs5Kp3I", "request_duration_ms":+ 5}}'+ 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_1F5BsRIUmJaD6Oqvz2XMKZCD+ response:+ body:+ string: "{\n \"id\": \"pi_1F5BsRIUmJaD6Oqvz2XMKZCD\",\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\"\+ : \"manual\",\n \"charges\": {\n \"object\": \"list\",\n \"data\":\+ \ [\n {\n \"id\": \"ch_1F5BsRIUmJaD6Oqvl2WF1xdU\",\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_1F5D3BIUmJaD6OqvpbNul1gK\",\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\": 1565271719,\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\": 10,\n \"seller_message\"\+ : \"Payment complete.\",\n \"type\": \"authorized\"\n },\n\+ \ \"paid\": true,\n \"payment_intent\": \"pi_1F5BsRIUmJaD6Oqvz2XMKZCD\"\+ ,\n \"payment_method\": \"pm_1F5BsQIUmJaD6OqvbB4aOcJX\",\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_1F5BsRIUmJaD6Oqvl2WF1xdU/rcpt_FaMbtUmGgcY6xnKYCxzQgjVlAwLno4y\"\+ ,\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_1F5BsRIUmJaD6Oqvl2WF1xdU/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_1F5BsRIUmJaD6Oqvz2XMKZCD\"\n\+ \ },\n \"client_secret\": \"pi_1F5BsRIUmJaD6Oqvz2XMKZCD_secret_FnZ8ApzrD4JVONWgCbLGbKAdI\"\+ ,\n \"confirmation_method\": \"manual\",\n \"created\": 1565271719,\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_1F5BsQIUmJaD6OqvbB4aOcJX\",\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:+ - '3993'+ Content-Type:+ - application/json+ Date:+ - Thu, 08 Aug 2019 16:26:54 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_Nu0wuOQgNYPd0u+ stripe-version:+ - '2019-05-16'+ status:+ code: 200+ message: OK+- request:+ body: amount=1000+ headers:+ Accept:+ - '*/*'+ Accept-Encoding:+ - gzip, deflate+ Connection:+ - keep-alive+ Content-Length:+ - '11'+ Content-Type:+ - application/x-www-form-urlencoded+ Idempotency-Key:+ - be73e68d-808a-4277-a928-b68e68037e88+ User-Agent:+ - Stripe/v1 PythonBindings/2.33.0+ X-Stripe-Client-Telemetry:+ - '{"last_request_metrics": {"request_id": "req_Nu0wuOQgNYPd0u", "request_duration_ms":+ 494}}'+ 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_1F5BsRIUmJaD6Oqvl2WF1xdU/refund+ response:+ body:+ string: "{\n \"id\": \"ch_1F5BsRIUmJaD6Oqvl2WF1xdU\",\n \"object\": \"charge\"\+ ,\n \"amount\": 4242,\n \"amount_refunded\": 1000,\n \"application\": null,\n\+ \ \"application_fee\": null,\n \"application_fee_amount\": null,\n \"balance_transaction\"\+ : \"txn_1F5D3BIUmJaD6OqvpbNul1gK\",\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\": 1565271719,\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\"\+ : 10,\n \"seller_message\": \"Payment complete.\",\n \"type\": \"authorized\"\+ \n },\n \"paid\": true,\n \"payment_intent\": \"pi_1F5BsRIUmJaD6Oqvz2XMKZCD\"\+ ,\n \"payment_method\": \"pm_1F5BsQIUmJaD6OqvbB4aOcJX\",\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_1F5BsRIUmJaD6Oqvl2WF1xdU/rcpt_FaMbtUmGgcY6xnKYCxzQgjVlAwLno4y\"\+ ,\n \"refunded\": false,\n \"refunds\": {\n \"object\": \"list\",\n \+ \ \"data\": [\n {\n \"id\": \"re_1F5ES2IUmJaD6Oqv7emYlU1k\"\+ ,\n \"object\": \"refund\",\n \"amount\": 1000,\n \"\+ balance_transaction\": \"txn_1F5ES2IUmJaD6OqvJLdKm7MT\",\n \"charge\"\+ : \"ch_1F5BsRIUmJaD6Oqvl2WF1xdU\",\n \"created\": 1565281614,\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_1F5BsRIUmJaD6Oqvl2WF1xdU/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:+ - '2671'+ Content-Type:+ - application/json+ Date:+ - Thu, 08 Aug 2019 16:26:54 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:+ - be73e68d-808a-4277-a928-b68e68037e88+ original-request:+ - req_AvzNPKR2rCCyy9+ request-id:+ - req_AvzNPKR2rCCyy9+ stripe-version:+ - '2019-05-16'+ status:+ code: 200+ message: OK+version: 1diff --git a/tests/gateway/stripe/cassettes/test_refund_error_response.yaml b/tests/gateway/stripe/cassettes/test_refund_error_response.yamlnew file mode 100644index 0000000000..8904a4931a--- /dev/null+++ b/tests/gateway/stripe/cassettes/test_refund_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_AvzNPKR2rCCyy9", "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:13:29 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_ABPCzNFuuArBtR+ 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 97798a8e5c..158a0620ae 100644--- a/tests/gateway/stripe/test_stripe.py+++ b/tests/gateway/stripe/test_stripe.py@@ -5,7 +5,12 @@ import osimport pytestfrom saleor.payment import ChargeStatus-from saleor.payment.gateways.stripe_new import TransactionKind, authorize, capture+from saleor.payment.gateways.stripe_new import (+ TransactionKind,+ authorize,+ capture,+ refund,+)from saleor.payment.interface import GatewayConfigfrom saleor.payment.utils import create_payment_information@@ -111,6 +116,7 @@ def stripe_authorized_payment(stripe_payment):@pytest.mark.integration@pytest.mark.vcr(filter_headers=["authorization"])def test_capture(stripe_authorized_payment, sandbox_gateway_config):+ # Get id from sandbox for intent not yet capturedINTENT_ID = "pi_1F5BsRIUmJaD6Oqvz2XMKZCD"payment_info = create_payment_information(stripe_authorized_payment, payment_token=INTENT_ID@@ -138,3 +144,30 @@ def test_capture_error_response(stripe_payment, sandbox_gateway_config):assert not response.is_successassert response.amount == stripe_payment.totalassert response.currency == stripe_payment.currency+++def stripe_paid_payment(stripe_payment):+ stripe_payment.charge_status = ChargeStatus.FULLY_CHARGED+ stripe_payment.save(update_fields=["charge_status"])++ return stripe_payment++[email protected](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)+ INTENT_ID = "pi_1F5BsRIUmJaD6Oqvz2XMKZCD"+ payment_info = create_payment_information(+ stripe_paid_payment, amount=REFUND_AMOUNT, payment_token=INTENT_ID+ )+ response = refund(payment_info, sandbox_gateway_config)++ assert not response.error+ assert response.transaction_id == INTENT_ID+ assert response.kind == TransactionKind.REFUND+ assert response.is_success+ assert isclose(response.amount, REFUND_AMOUNT)+ assert response.currency == TRANSACTION_CURRENCY