hyperswitch icon indicating copy to clipboard operation
hyperswitch copied to clipboard

feat(core): payments overcapture support for V1

Open AkshayaFoiger opened this issue 1 year ago • 5 comments

Note: This feature is supported only via manual capture

Type of Change

  • [ ] Bugfix
  • [x] New feature
  • [x] Enhancement
  • [ ] Refactoring
  • [ ] Dependency updates
  • [ ] Documentation
  • [ ] CI/CD

Description

Overcapture allows you to capture with an amount that’s higher than the authorised amount for a card payment. Unlike incremental authorisations, overcapture doesn’t result in additional authorisations with the card networks.

In this PR, Overcapture is implemented for stripe card payments.

Additional Changes

  • [x] This PR modifies the API contract
  • [x] This PR modifies the database schema
  • [ ] This PR modifies application configuration/environment variables

API contract changes:

a) /payments create request to include request_overcapture boolean

b) /payments response to include

overcapture_status (applicable/ not_applicable)
amount_capturable will be updated

c) Profile create request and response to include always_request_overcapture (boolean)

d) Profile update request and response to include always_request_overcapture (boolean)

DB changes:

a) request_overcapture enum(Enable/Skip) field to be introduced in payment_intent

b)ovecapture_status enum(Applicable/NotApplicable) to be introduced in payment_attempt

d) always_request_overcapture boolean field to be introduced in business_profiles table

Not Handled (phase 2 )

a) Error is not thrown, when overcapture is not supported by the connector

b) List of supported connector and payment method not maintained.

c) Extend this feature for multiple manual capture and incremental authorization

-> Cypress test not added because it cannot be tested, via stripe. As this functionality has to be enabled by stripe. -> Supported only for stripe card flow

How did you test it?

  1. Create/ Update a business_profile with "always_request_overcapture": true
curl --location 'http://localhost:8080/account/postman_merchant_GHAction_358cf835-2879-4d90-8475-ef0e1a246611/business_profile/pro_bzfd1zHHxs30uUyc2TQg' \
--header 'Content-Type: application/json' \
--header 'api-key: test_admin' \
--data '{
  "profile_name": "US_default",
  "always_request_overcapture": true
}'

Response

{
    "merchant_id": "postman_merchant_GHAction_358cf835-2879-4d90-8475-ef0e1a246611",
    "profile_id": "pro_bzfd1zHHxs30uUyc2TQg",
    "profile_name": "US_default",
    "return_url": "https://duck.com/success",
    "enable_payment_response_hash": true,
    "payment_response_hash_key": "83d4dikvQDLVgKR2jQDWYODswS1Tw6R2IP40rBatKGBEJwX6LhNeuNQbLzQIrX7T",
    "redirect_to_merchant_with_http_post": false,
    "webhook_details": {
        "webhook_version": "1.0.1",
        "webhook_username": "ekart_retail",
        "webhook_password": "password_ekart@123",
        "webhook_url": null,
        "payment_created_enabled": true,
        "payment_succeeded_enabled": true,
        "payment_failed_enabled": true
    },
    "metadata": null,
    "routing_algorithm": null,
    "intent_fulfillment_time": 900,
    "frm_routing_algorithm": null,
    "payout_routing_algorithm": null,
    "applepay_verified_domains": null,
    "session_expiry": 900,
    "payment_link_config": null,
    "authentication_connector_details": null,
    "use_billing_as_payment_method_billing": true,
    "extended_card_info_config": null,
    "collect_shipping_details_from_wallet_connector": false,
    "collect_billing_details_from_wallet_connector": false,
    "always_collect_shipping_details_from_wallet_connector": false,
    "always_collect_billing_details_from_wallet_connector": false,
    "is_connector_agnostic_mit_enabled": false,
    "payout_link_config": null,
    "outgoing_webhook_custom_http_headers": null,
    "tax_connector_id": null,
    "is_tax_connector_enabled": false,
    "is_network_tokenization_enabled": false,
    "is_auto_retries_enabled": false,
    "max_auto_retries_enabled": null,
    "is_click_to_pay_enabled": false,
    "authentication_product_ids": null,
    "always_request_overcapture": true
}

DB - business_profile will have a field called always_request_overcapture , marked as true Screenshot 2024-12-23 at 3 07 37 PM

  1. Do a payment via stripe, without sending request_overcapture in the payment request
curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_oqtfJBx4JGn0Pmr3ZLtDMyUEzRZQ0IBeqnvImk4W1lbRHnh07rqLSXNTv8JKYk9L' \
--data-raw '{
    "amount": 6540,
    "currency": "USD",
    "confirm": true,
    "capture_method": "manual",
    "capture_on": "2022-09-10T10:11:12Z",
    "amount_to_capture": 6540,
    "customer_id": "StripeCustomer",
    "email": "[email protected]",
    "name": "John Doe",
    "phone": "999999999",
    "phone_country_code": "+1",
    "description": "Its my first payment request",
    "authentication_type": "no_three_ds",
    "return_url": "https://duck.com",
    "payment_method": "card",
    "payment_method_type": "credit",
    "payment_method_data": {
        "card": {
            "card_number": "4242424242424242",
            "card_exp_month": "10",
            "card_exp_year": "25",
            "card_holder_name": "joseph Doe",
            "card_cvc": "123"
        }
    },
    "billing": {
        "address": {
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "city": "San Fransico",
            "state": "California",
            "zip": "94122",
            "country": "US",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "9123456789",
            "country_code": "+91"
        },
        "email": "[email protected]"
    },
    "shipping": {
        "address": {
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "city": "San Fransico",
            "state": "California",
            "zip": "94122",
            "country": "US",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "9123456789",
            "country_code": "+91"
        }
    },
    "statement_descriptor_name": "joseph",
    "statement_descriptor_suffix": "JS",
    "metadata": {
        "udf1": "value1",
        "new_customer": "true",
        "login_date": "2019-09-10T10:11:12Z"
    },
    "routing": {
        "type": "single",
        "data": {
            "connector": "stripe",
            "merchant_connector_id": "mca_5SMXtr84AcZ8zhBefG8d"
        }
    }
    
}'

Cannot be tested because, this feature needs to be enabled at stripe

{"payment_id":"pay_l4MFxlOyZzF4XYXnKOqQ","merchant_id":"postman_merchant_GHAction_4f79729e-d004-4479-8bca-7201b2b231cf","status":"failed","amount":100000,"net_amount":100000,"shipping_cost":null,"amount_capturable":100000,"amount_received":null,"connector":"stripe","client_secret":"pay_l4MFxlOyZzF4XYXnKOqQ_secret_yYm9SUp6zsKMck6bp6IS","created":"2025-04-10T06:47:21.826Z","currency":"USD","customer_id":"cust_no_pm","customer":{"id":"cust_no_pm","name":null,"email":"[email protected]","phone":null,"phone_country_code":null},"description":null,"refunds":null,"disputes":null,"mandate_id":null,"mandate_data":null,"setup_future_usage":null,"off_session":null,"capture_on":null,"capture_method":"manual","payment_method":"card","payment_method_data":{"card":{"last4":"4242","card_type":null,"card_network":null,"card_issuer":null,"card_issuing_country":null,"card_isin":"424242","card_extended_bin":null,"card_exp_month":"12","card_exp_year":"27","card_holder_name":"joseph Doe","payment_checks":null,"authentication_data":null},"billing":null},"payment_token":null,"shipping":null,"billing":null,"order_details":null,"email":"[email protected]","name":null,"phone":null,"return_url":null,"authentication_type":"no_three_ds","statement_descriptor_name":null,"statement_descriptor_suffix":null,"next_action":null,"cancellation_reason":null,"error_code":"payment_intent_invalid_parameter","error_message":"This account is not eligible for the requested card features. See https://stripe.com/docs/payments/flexible-payments for more details.","unified_code":"UE_9000","unified_message":"Something went wrong","payment_experience":null,"payment_method_type":null,"connector_label":null,"business_country":null,"business_label":"default","business_sub_label":null,"allowed_payment_method_types":null,"ephemeral_key":{"customer_id":"cust_no_pm","created_at":1744267641,"expires":1744271241,"secret":"epk_699f09d11e474ce8a94b966a92e7103d"},"manual_retry_allowed":true,"connector_transaction_id":"pi_3RCEtTD5R7gDAGff025s2bBV","frm_message":null,"metadata":null,"connector_metadata":null,"feature_metadata":null,"reference_id":null,"payment_link":null,"profile_id":"pro_n9tKKY8IdEgvz80VpbRO","surcharge_details":null,"attempt_count":1,"merchant_decision":null,"merchant_connector_id":"mca_rW9zxYm57HLxSUWytHok","incremental_authorization_allowed":false,"authorization_count":null,"incremental_authorizations":null,"external_authentication_details":null,"external_3ds_authentication_attempted":false,"expires_on":"2025-04-10T07:02:21.826Z","fingerprint":null,"browser_info":{"language":"nl-NL","time_zone":0,"ip_address":"13.232.74.226","user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36","color_depth":24,"java_enabled":true,"screen_width":1536,"accept_header":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","screen_height":723,"java_script_enabled":true},"payment_method_id":null,"payment_method_status":null,"updated":"2025-04-10T06:47:23.510Z","split_payments":null,"frm_metadata":null,"extended_authorization_applied":null,"capture_before":null,"merchant_order_reference_id":null,"order_tax_amount":null,"connector_mandate_id":null,"card_discovery":"manual","force_3ds_challenge":false,"force_3ds_challenge_trigger":false,"issuer_error_code":null,"issuer_error_message":null,"overcapture_status":"applicable"}

Response

{"payment_id":"pay_l4MFxlOyZzF4XYXnKOqQ","merchant_id":"postman_merchant_GHAction_4f79729e-d004-4479-8bca-7201b2b231cf","status":"failed","amount":100000,"net_amount":100000,"shipping_cost":null,"amount_capturable":100000,"amount_received":null,"connector":"stripe","client_secret":"pay_l4MFxlOyZzF4XYXnKOqQ_secret_yYm9SUp6zsKMck6bp6IS","created":"2025-04-10T06:47:21.826Z","currency":"USD","customer_id":"cust_no_pm","customer":{"id":"cust_no_pm","name":null,"email":"[email protected]","phone":null,"phone_country_code":null},"description":null,"refunds":null,"disputes":null,"mandate_id":null,"mandate_data":null,"setup_future_usage":null,"off_session":null,"capture_on":null,"capture_method":"manual","payment_method":"card","payment_method_data":{"card":{"last4":"4242","card_type":null,"card_network":null,"card_issuer":null,"card_issuing_country":null,"card_isin":"424242","card_extended_bin":null,"card_exp_month":"12","card_exp_year":"27","card_holder_name":"joseph Doe","payment_checks":null,"authentication_data":null},"billing":null},"payment_token":null,"shipping":null,"billing":null,"order_details":null,"email":"[email protected]","name":null,"phone":null,"return_url":null,"authentication_type":"no_three_ds","statement_descriptor_name":null,"statement_descriptor_suffix":null,"next_action":null,"cancellation_reason":null,"error_code":"payment_intent_invalid_parameter","error_message":"This account is not eligible for the requested card features. See https://stripe.com/docs/payments/flexible-payments for more details.","unified_code":"UE_9000","unified_message":"Something went wrong","payment_experience":null,"payment_method_type":null,"connector_label":null,"business_country":null,"business_label":"default","business_sub_label":null,"allowed_payment_method_types":null,"ephemeral_key":{"customer_id":"cust_no_pm","created_at":1744267641,"expires":1744271241,"secret":"epk_699f09d11e474ce8a94b966a92e7103d"},"manual_retry_allowed":true,"connector_transaction_id":"pi_3RCEtTD5R7gDAGff025s2bBV","frm_message":null,"metadata":null,"connector_metadata":null,"feature_metadata":null,"reference_id":null,"payment_link":null,"profile_id":"pro_n9tKKY8IdEgvz80VpbRO","surcharge_details":null,"attempt_count":1,"merchant_decision":null,"merchant_connector_id":"mca_rW9zxYm57HLxSUWytHok","incremental_authorization_allowed":false,"authorization_count":null,"incremental_authorizations":null,"external_authentication_details":null,"external_3ds_authentication_attempted":false,"expires_on":"2025-04-10T07:02:21.826Z","fingerprint":null,"browser_info":{"language":"nl-NL","time_zone":0,"ip_address":"13.232.74.226","user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36","color_depth":24,"java_enabled":true,"screen_width":1536,"accept_header":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","screen_height":723,"java_script_enabled":true},"payment_method_id":null,"payment_method_status":null,"updated":"2025-04-10T06:47:23.510Z","split_payments":null,"frm_metadata":null,"extended_authorization_applied":null,"capture_before":null,"merchant_order_reference_id":null,"order_tax_amount":null,"connector_mandate_id":null,"card_discovery":"manual","force_3ds_challenge":false,"force_3ds_challenge_trigger":false,"issuer_error_code":null,"issuer_error_message":null,"overcapture_status":"applicable"}
  1. Create a update the business account "always_request_overcapture": false
curl --location 'http://localhost:8080/account/postman_merchant_GHAction_77b1a97f-001b-458d-b185-67998995c880/business_profile/pro_ZzurAEZrSqx46L8gNMk5' \
--header 'Content-Type: application/json' \
--header 'api-key: test_admin' \
--data '{
  "profile_name": "US_default",
  "always_request_overcapture": false
}'

Response

{
    "merchant_id": "postman_merchant_GHAction_77b1a97f-001b-458d-b185-67998995c880",
    "profile_id": "pro_ZzurAEZrSqx46L8gNMk5",
    "profile_name": "US_default",
    "return_url": "https://duck.com/success",
    "enable_payment_response_hash": true,
    "payment_response_hash_key": "iJLfkEZljBCu2mNKSoAp2fOOh84X4WJPBAtkAJE4s4Tqjne78DrKkJUjWcv01PEG",
    "redirect_to_merchant_with_http_post": false,
    "webhook_details": {
        "webhook_version": "1.0.1",
        "webhook_username": "ekart_retail",
        "webhook_password": "password_ekart@123",
        "webhook_url": null,
        "payment_created_enabled": true,
        "payment_succeeded_enabled": true,
        "payment_failed_enabled": true
    },
    "metadata": null,
    "routing_algorithm": null,
    "intent_fulfillment_time": 900,
    "frm_routing_algorithm": null,
    "payout_routing_algorithm": null,
    "applepay_verified_domains": null,
    "session_expiry": 900,
    "payment_link_config": null,
    "authentication_connector_details": null,
    "use_billing_as_payment_method_billing": true,
    "extended_card_info_config": null,
    "collect_shipping_details_from_wallet_connector": false,
    "collect_billing_details_from_wallet_connector": false,
    "always_collect_shipping_details_from_wallet_connector": false,
    "always_collect_billing_details_from_wallet_connector": false,
    "is_connector_agnostic_mit_enabled": false,
    "payout_link_config": null,
    "outgoing_webhook_custom_http_headers": null,
    "tax_connector_id": null,
    "is_tax_connector_enabled": false,
    "is_network_tokenization_enabled": false,
    "is_auto_retries_enabled": false,
    "max_auto_retries_enabled": null,
    "is_click_to_pay_enabled": false,
    "authentication_product_ids": null,
    "always_request_overcapture": false
}
  1. Create an overcapture payment with stripe
curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_oqtfJBx4JGn0Pmr3ZLtDMyUEzRZQ0IBeqnvImk4W1lbRHnh07rqLSXNTv8JKYk9L' \
--data-raw '{
    "amount": 6540,
    "currency": "USD",
    "confirm": true,
    "capture_method": "manual",
    "capture_on": "2022-09-10T10:11:12Z",
    "amount_to_capture": 6540,
    "customer_id": "StripeCustomer",
    "email": "[email protected]",
    "name": "John Doe",
    "phone": "999999999",
    "phone_country_code": "+1",
    "description": "Its my first payment request",
    "authentication_type": "no_three_ds",
    "return_url": "https://duck.com",
    "payment_method": "card",
    "payment_method_type": "credit",
    "payment_method_data": {
        "card": {
            "card_number": "4242424242424242",
            "card_exp_month": "10",
            "card_exp_year": "25",
            "card_holder_name": "joseph Doe",
            "card_cvc": "123"
        }
    },
    "billing": {
        "address": {
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "city": "San Fransico",
            "state": "California",
            "zip": "94122",
            "country": "US",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "9123456789",
            "country_code": "+91"
        },
        "email": "[email protected]"
    },
    "shipping": {
        "address": {
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "city": "San Fransico",
            "state": "California",
            "zip": "94122",
            "country": "US",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "9123456789",
            "country_code": "+91"
        }
    },
    "statement_descriptor_name": "joseph",
    "statement_descriptor_suffix": "JS",
    "metadata": {
        "udf1": "value1",
        "new_customer": "true",
        "login_date": "2019-09-10T10:11:12Z"
    },
    "routing": {
        "type": "single",
        "data": {
            "connector": "stripe",
            "merchant_connector_id": "mca_5SMXtr84AcZ8zhBefG8d"
        }
    }
    "overcapture_status": "not_applicable"
}'

Response

{
    "payment_id": "pay_FKzjYUzqpSigIfzoXolG",
    "merchant_id": "postman_merchant_GHAction_77b1a97f-001b-458d-b185-67998995c880",
    "status": "requires_capture",
    "amount": 6540,
    "net_amount": 6540,
    "shipping_cost": null,
    "amount_capturable": 6540,
    "amount_received": 0,
    "connector": "stripe",
    "client_secret": "pay_FKzjYUzqpSigIfzoXolG_secret_sB7pumP3GJcc4ZnSvCxX",
    "created": "2024-12-23T09:57:04.620Z",
    "currency": "USD",
    "customer_id": "StripeCustomer",
    "customer": {
        "id": "StripeCustomer",
        "name": "John Doe",
        "email": "[email protected]",
        "phone": "999999999",
        "phone_country_code": "+1"
    },
    "description": "Its my first payment request",
    "refunds": null,
    "disputes": null,
    "mandate_id": null,
    "mandate_data": null,
    "setup_future_usage": null,
    "off_session": null,
    "capture_on": null,
    "capture_method": "manual",
    "payment_method": "card",
    "payment_method_data": {
        "card": {
            "last4": "4242",
            "card_type": null,
            "card_network": null,
            "card_issuer": null,
            "card_issuing_country": null,
            "card_isin": "424242",
            "card_extended_bin": null,
            "card_exp_month": "10",
            "card_exp_year": "25",
            "card_holder_name": "joseph Doe",
            "payment_checks": {
                "cvc_check": "pass",
                "address_line1_check": "pass",
                "address_postal_code_check": "pass"
            },
            "authentication_data": null
        },
        "billing": null
    },
    "payment_token": null,
    "shipping": {
        "address": {
            "city": "San Fransico",
            "country": "US",
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "zip": "94122",
            "state": "California",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "9123456789",
            "country_code": "+91"
        },
        "email": null
    },
    "billing": {
        "address": {
            "city": "San Fransico",
            "country": "US",
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "zip": "94122",
            "state": "California",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "9123456789",
            "country_code": "+91"
        },
        "email": "[email protected]"
    },
    "order_details": null,
    "email": "[email protected]",
    "name": "John Doe",
    "phone": "999999999",
    "return_url": "https://duck.com/",
    "authentication_type": "no_three_ds",
    "statement_descriptor_name": "joseph",
    "statement_descriptor_suffix": "JS",
    "next_action": null,
    "cancellation_reason": null,
    "error_code": null,
    "error_message": null,
    "unified_code": null,
    "unified_message": null,
    "payment_experience": null,
    "payment_method_type": "credit",
    "connector_label": null,
    "business_country": null,
    "business_label": "default",
    "business_sub_label": null,
    "allowed_payment_method_types": null,
    "ephemeral_key": {
        "customer_id": "StripeCustomer",
        "created_at": 1734947824,
        "expires": 1734951424,
        "secret": "epk_54c122be30324503a3199e33d42fff0f"
    },
    "manual_retry_allowed": false,
    "connector_transaction_id": "pi_3QZ8NpIVaesDjvMP1f6ANdO7",
    "frm_message": null,
    "metadata": {
        "udf1": "value1",
        "login_date": "2019-09-10T10:11:12Z",
        "new_customer": "true"
    },
    "connector_metadata": null,
    "feature_metadata": null,
    "reference_id": "pi_3QZ8NpIVaesDjvMP1f6ANdO7",
    "payment_link": null,
    "profile_id": "pro_ZzurAEZrSqx46L8gNMk5",
    "surcharge_details": null,
    "attempt_count": 1,
    "merchant_decision": null,
    "merchant_connector_id": "mca_5SMXtr84AcZ8zhBefG8d",
    "incremental_authorization_allowed": null,
    "authorization_count": null,
    "incremental_authorizations": null,
    "external_authentication_details": null,
    "external_3ds_authentication_attempted": false,
    "expires_on": "2024-12-23T10:12:04.620Z",
    "fingerprint": null,
    "browser_info": null,
    "payment_method_id": null,
    "payment_method_status": null,
    "updated": "2024-12-23T09:57:06.130Z",
    "split_payments": null,
    "frm_metadata": null,
    "merchant_order_reference_id": null,
    "order_tax_amount": null,
    "connector_mandate_id": null,
     "overcapture_status": "not_applicable"
}

Hardcoded testcases - cannot be tested in integ/sandbox/prod. These are tests triggered after hardcoding the amount values.

curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_3p8fPUv04vz8PLxkCUOq8UrLy5DPodzOfCPaiCdjl2VWDewZ3L5s3IxXhoGJeBh2' \
--data '{
    "amount": 2000,
    "currency": "USD",
    "confirm": true,
    "capture_method": "manual",
    
    "customer_id": "cust_no_pm",
    "authentication_type": "no_three_ds",
    
    
    
    
    
    "payment_method": "card",
    "payment_method_type": "debit",
    "payment_method_data": {
        "card": {
            "card_number":  "4242424242424242",
            "card_exp_month": "03",
            "card_exp_year": "30",
            "card_holder_name": "joseph Doe",
            "card_cvc": "737"
        }
    },
    "billing": {
        "address": {
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "city": "San Fransico",
            "state": "California",
            "zip": "94122",
            "country": "US",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "8056594427",
            "country_code": "+91"
        }
    },    
    "browser_info": {
        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
        "accept_header": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "language": "nl-NL",
        "color_depth": 24,
        "screen_height": 723,
        "screen_width": 1536,
        "time_zone": 0,
        "java_enabled": true,
        "java_script_enabled": true,
        "ip_address": "13.232.74.226"
    }
    ,"request_overcapture": "enable"
}'

Response

{
    "payment_id": "pay_J8MKhjr4saG0eyjUAaGg",
    "merchant_id": "postman_merchant_GHAction_b8c5a022-d24d-4739-879d-718ed959825e",
    "status": "requires_capture",
    "amount": 2000,
    "net_amount": 2000,
    "shipping_cost": null,
    "amount_capturable": 7000,
    "amount_received": 0,
    "connector": "stripe",
    "client_secret": "pay_J8MKhjr4saG0eyjUAaGg_secret_FGxWJr0vgQ2RGeAZqtOP",
    "created": "2025-01-17T08:04:53.891Z",
    "currency": "USD",
    "customer_id": "cust_no_pm",
    "customer": {
        "id": "cust_no_pm",
        "name": null,
        "email": null,
        "phone": null,
        "phone_country_code": null
    },
    "description": null,
    "refunds": null,
    "disputes": null,
    "mandate_id": null,
    "mandate_data": null,
    "setup_future_usage": null,
    "off_session": null,
    "capture_on": null,
    "capture_method": "manual",
    "payment_method": "card",
    "payment_method_data": {
        "card": {
            "last4": "4242",
            "card_type": null,
            "card_network": null,
            "card_issuer": null,
            "card_issuing_country": null,
            "card_isin": "424242",
            "card_extended_bin": null,
            "card_exp_month": "03",
            "card_exp_year": "30",
            "card_holder_name": "joseph Doe",
            "payment_checks": {
                "cvc_check": "pass",
                "address_line1_check": "pass",
                "address_postal_code_check": "pass"
            },
            "authentication_data": null
        },
        "billing": null
    },
    "payment_token": null,
    "shipping": null,
    "billing": {
        "address": {
            "city": "San Fransico",
            "country": "US",
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "zip": "94122",
            "state": "California",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "8056594427",
            "country_code": "+91"
        },
        "email": null
    },
    "order_details": null,
    "email": null,
    "name": null,
    "phone": null,
    "return_url": null,
    "authentication_type": "no_three_ds",
    "statement_descriptor_name": null,
    "statement_descriptor_suffix": null,
    "next_action": null,
    "cancellation_reason": null,
    "error_code": null,
    "error_message": null,
    "unified_code": null,
    "unified_message": null,
    "payment_experience": null,
    "payment_method_type": "debit",
    "connector_label": null,
    "business_country": null,
    "business_label": "default",
    "business_sub_label": null,
    "allowed_payment_method_types": null,
    "ephemeral_key": {
        "customer_id": "cust_no_pm",
        "created_at": 1737101093,
        "expires": 1737104693,
        "secret": "epk_589715ba256940348209ca91c674dbf1"
    },
    "manual_retry_allowed": false,
    "connector_transaction_id": "pi_3QiAXyIVaesDjvMP2VPJ4ZG7",
    "frm_message": null,
    "metadata": null,
    "connector_metadata": null,
    "feature_metadata": null,
    "reference_id": "pi_3QiAXyIVaesDjvMP2VPJ4ZG7",
    "payment_link": null,
    "profile_id": "pro_2h3e1fc0m7QCfwBdDyAT",
    "surcharge_details": null,
    "attempt_count": 1,
    "merchant_decision": null,
    "merchant_connector_id": "mca_VwsDtrjWEltcLycDi5iP",
    "incremental_authorization_allowed": null,
    "authorization_count": null,
    "incremental_authorizations": null,
    "external_authentication_details": null,
    "external_3ds_authentication_attempted": false,
    "expires_on": "2025-01-17T08:19:53.891Z",
    "fingerprint": null,
    "browser_info": {
        "language": "nl-NL",
        "time_zone": 0,
        "ip_address": "13.232.74.226",
        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
        "color_depth": 24,
        "java_enabled": true,
        "screen_width": 1536,
        "accept_header": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "screen_height": 723,
        "java_script_enabled": true
    },
    "payment_method_id": null,
    "payment_method_status": null,
    "updated": "2025-01-17T08:04:55.587Z",
    "split_payments": null,
    "frm_metadata": null,
    "merchant_order_reference_id": null,
    "order_tax_amount": null,
    "connector_mandate_id": null,
    "overcapture_status": "applicable"
}

Overcapture the authorized amount

{
    "payment_id": "pay_J8MKhjr4saG0eyjUAaGg",
    "merchant_id": "postman_merchant_GHAction_b8c5a022-d24d-4739-879d-718ed959825e",
    "status": "succeeded",
    "amount": 2000,
    "net_amount": 2000,
    "shipping_cost": null,
    "amount_capturable": 0,
    "amount_received": 7000,
    "connector": "stripe",
    "client_secret": "pay_J8MKhjr4saG0eyjUAaGg_secret_FGxWJr0vgQ2RGeAZqtOP",
    "created": "2025-01-17T08:04:53.891Z",
    "currency": "USD",
    "customer_id": "cust_no_pm",
    "customer": {
        "id": "cust_no_pm",
        "name": null,
        "email": null,
        "phone": null,
        "phone_country_code": null
    },
    "description": null,
    "refunds": null,
    "disputes": null,
    "mandate_id": null,
    "mandate_data": null,
    "setup_future_usage": null,
    "off_session": null,
    "capture_on": null,
    "capture_method": "manual",
    "payment_method": "card",
    "payment_method_data": {
        "card": {
            "last4": "4242",
            "card_type": null,
            "card_network": null,
            "card_issuer": null,
            "card_issuing_country": null,
            "card_isin": "424242",
            "card_extended_bin": null,
            "card_exp_month": "03",
            "card_exp_year": "30",
            "card_holder_name": "joseph Doe",
            "payment_checks": {
                "cvc_check": "pass",
                "address_line1_check": "pass",
                "address_postal_code_check": "pass"
            },
            "authentication_data": null
        },
        "billing": null
    },
    "payment_token": null,
    "shipping": null,
    "billing": {
        "address": {
            "city": "San Fransico",
            "country": "US",
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "zip": "94122",
            "state": "California",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "8056594427",
            "country_code": "+91"
        },
        "email": null
    },
    "order_details": null,
    "email": null,
    "name": null,
    "phone": null,
    "return_url": null,
    "authentication_type": "no_three_ds",
    "statement_descriptor_name": null,
    "statement_descriptor_suffix": null,
    "next_action": null,
    "cancellation_reason": null,
    "error_code": null,
    "error_message": null,
    "unified_code": null,
    "unified_message": null,
    "payment_experience": null,
    "payment_method_type": "debit",
    "connector_label": null,
    "business_country": null,
    "business_label": "default",
    "business_sub_label": null,
    "allowed_payment_method_types": null,
    "ephemeral_key": null,
    "manual_retry_allowed": false,
    "connector_transaction_id": "pi_3QiAXyIVaesDjvMP2VPJ4ZG7",
    "frm_message": null,
    "metadata": null,
    "connector_metadata": null,
    "feature_metadata": null,
    "reference_id": "pi_3QiAXyIVaesDjvMP2VPJ4ZG7",
    "payment_link": null,
    "profile_id": "pro_2h3e1fc0m7QCfwBdDyAT",
    "surcharge_details": null,
    "attempt_count": 1,
    "merchant_decision": null,
    "merchant_connector_id": "mca_VwsDtrjWEltcLycDi5iP",
    "incremental_authorization_allowed": null,
    "authorization_count": null,
    "incremental_authorizations": null,
    "external_authentication_details": null,
    "external_3ds_authentication_attempted": false,
    "expires_on": "2025-01-17T08:19:53.891Z",
    "fingerprint": null,
    "browser_info": {
        "language": "nl-NL",
        "time_zone": 0,
        "ip_address": "13.232.74.226",
        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
        "color_depth": 24,
        "java_enabled": true,
        "screen_width": 1536,
        "accept_header": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "screen_height": 723,
        "java_script_enabled": true
    },
    "payment_method_id": null,
    "payment_method_status": null,
    "updated": "2025-01-17T08:06:06.296Z",
    "split_payments": null,
    "frm_metadata": null,
    "merchant_order_reference_id": null,
    "order_tax_amount": null,
    "connector_mandate_id": null,
    "overcapture_status": "not_applicable"
}

Response

{
    "payment_id": "pay_J8MKhjr4saG0eyjUAaGg",
    "merchant_id": "postman_merchant_GHAction_b8c5a022-d24d-4739-879d-718ed959825e",
    "status": "succeeded",
    "amount": 2000,
    "net_amount": 2000,
    "shipping_cost": null,
    "amount_capturable": 0,
    "amount_received": 7000,
    "connector": "stripe",
    "client_secret": "pay_J8MKhjr4saG0eyjUAaGg_secret_FGxWJr0vgQ2RGeAZqtOP",
    "created": "2025-01-17T08:04:53.891Z",
    "currency": "USD",
    "customer_id": "cust_no_pm",
    "customer": {
        "id": "cust_no_pm",
        "name": null,
        "email": null,
        "phone": null,
        "phone_country_code": null
    },
    "description": null,
    "refunds": null,
    "disputes": null,
    "mandate_id": null,
    "mandate_data": null,
    "setup_future_usage": null,
    "off_session": null,
    "capture_on": null,
    "capture_method": "manual",
    "payment_method": "card",
    "payment_method_data": {
        "card": {
            "last4": "4242",
            "card_type": null,
            "card_network": null,
            "card_issuer": null,
            "card_issuing_country": null,
            "card_isin": "424242",
            "card_extended_bin": null,
            "card_exp_month": "03",
            "card_exp_year": "30",
            "card_holder_name": "joseph Doe",
            "payment_checks": {
                "cvc_check": "pass",
                "address_line1_check": "pass",
                "address_postal_code_check": "pass"
            },
            "authentication_data": null
        },
        "billing": null
    },
    "payment_token": null,
    "shipping": null,
    "billing": {
        "address": {
            "city": "San Fransico",
            "country": "US",
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "zip": "94122",
            "state": "California",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "8056594427",
            "country_code": "+91"
        },
        "email": null
    },
    "order_details": null,
    "email": null,
    "name": null,
    "phone": null,
    "return_url": null,
    "authentication_type": "no_three_ds",
    "statement_descriptor_name": null,
    "statement_descriptor_suffix": null,
    "next_action": null,
    "cancellation_reason": null,
    "error_code": null,
    "error_message": null,
    "unified_code": null,
    "unified_message": null,
    "payment_experience": null,
    "payment_method_type": "debit",
    "connector_label": null,
    "business_country": null,
    "business_label": "default",
    "business_sub_label": null,
    "allowed_payment_method_types": null,
    "ephemeral_key": null,
    "manual_retry_allowed": false,
    "connector_transaction_id": "pi_3QiAXyIVaesDjvMP2VPJ4ZG7",
    "frm_message": null,
    "metadata": null,
    "connector_metadata": null,
    "feature_metadata": null,
    "reference_id": "pi_3QiAXyIVaesDjvMP2VPJ4ZG7",
    "payment_link": null,
    "profile_id": "pro_2h3e1fc0m7QCfwBdDyAT",
    "surcharge_details": null,
    "attempt_count": 1,
    "merchant_decision": null,
    "merchant_connector_id": "mca_VwsDtrjWEltcLycDi5iP",
    "incremental_authorization_allowed": null,
    "authorization_count": null,
    "incremental_authorizations": null,
    "external_authentication_details": null,
    "external_3ds_authentication_attempted": false,
    "expires_on": "2025-01-17T08:19:53.891Z",
    "fingerprint": null,
    "browser_info": {
        "language": "nl-NL",
        "time_zone": 0,
        "ip_address": "13.232.74.226",
        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
        "color_depth": 24,
        "java_enabled": true,
        "screen_width": 1536,
        "accept_header": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "screen_height": 723,
        "java_script_enabled": true
    },
    "payment_method_id": null,
    "payment_method_status": null,
    "updated": "2025-01-17T08:06:06.296Z",
    "split_payments": null,
    "frm_metadata": null,
    "merchant_order_reference_id": null,
    "order_tax_amount": null,
    "connector_mandate_id": null,
    "overcapture_status": "applicable"
}

Refund

curl --location 'http://localhost:8080/refunds' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_3p8fPUv04vz8PLxkCUOq8UrLy5DPodzOfCPaiCdjl2VWDewZ3L5s3IxXhoGJeBh2' \
--data '{
    "payment_id": "pay_J8MKhjr4saG0eyjUAaGg",
    "amount": 6000,
    "reason": "Customer returned product",
    "refund_type": "instant",
    "metadata": {
        "udf1": "value1",
        "new_customer": "true",
        "login_date": "2019-09-10T10:11:12Z"
    }
}'

Response

{
    "refund_id": "ref_s5sciZjzMcno0invyl2P",
    "payment_id": "pay_J8MKhjr4saG0eyjUAaGg",
    "amount": 6000,
    "currency": "USD",
    "status": "succeeded",
    "reason": "Customer returned product",
    "metadata": {
        "udf1": "value1",
        "new_customer": "true",
        "login_date": "2019-09-10T10:11:12Z"
    },
    "error_message": null,
    "error_code": null,
    "unified_code": null,
    "unified_message": null,
    "created_at": "2025-01-17T08:08:03.680Z",
    "updated_at": "2025-01-17T08:08:05.197Z",
    "connector": "stripe",
    "profile_id": "pro_2h3e1fc0m7QCfwBdDyAT",
    "merchant_connector_id": "mca_VwsDtrjWEltcLycDi5iP",
    "split_refunds": null
}

Screenshot 2024-12-24 at 3 05 48 PM

Checklist

  • [x] I formatted the code cargo +nightly fmt --all
  • [x] I addressed lints thrown by cargo clippy
  • [ ] I reviewed the submitted code
  • [ ] I added unit tests for my changes where possible

AkshayaFoiger avatar Dec 12 '24 07:12 AkshayaFoiger

Review changes with  SemanticDiff

Changed Files
File Status
  crates/router/src/connector/utils.rs  56% smaller
  crates/diesel_models/src/user/sample_data.rs  30% smaller
  crates/openapi/src/openapi_v2.rs  11% smaller
  crates/router/src/connector/stripe/transformers.rs  11% smaller
  crates/openapi/src/openapi.rs  10% smaller
  crates/diesel_models/src/schema_v2.rs  7% smaller
  crates/router/src/types.rs  6% smaller
  api-reference-v2/openapi_spec.json  0% smaller
  api-reference/openapi_spec.json  0% smaller
  crates/api_models/src/admin.rs  0% smaller
  crates/api_models/src/payments.rs  0% smaller
  crates/common_enums/src/enums.rs  0% smaller
  crates/diesel_models/src/business_profile.rs  0% smaller
  crates/diesel_models/src/payment_attempt.rs  0% smaller
  crates/diesel_models/src/payment_intent.rs  0% smaller
  crates/diesel_models/src/schema.rs  0% smaller
  crates/hyperswitch_connectors/src/utils.rs  0% smaller
  crates/hyperswitch_domain_models/src/business_profile.rs  0% smaller
  crates/hyperswitch_domain_models/src/payments.rs  0% smaller
  crates/hyperswitch_domain_models/src/payments/payment_attempt.rs  0% smaller
  crates/hyperswitch_domain_models/src/payments/payment_intent.rs  0% smaller
  crates/hyperswitch_domain_models/src/router_data.rs  0% smaller
  crates/hyperswitch_domain_models/src/router_request_types.rs  0% smaller
  crates/router/src/core/admin.rs  0% smaller
  crates/router/src/core/payments/helpers.rs  0% smaller
  crates/router/src/core/payments/operations/payment_confirm.rs  0% smaller
  crates/router/src/core/payments/operations/payment_create.rs  0% smaller
  crates/router/src/core/payments/operations/payment_response.rs  0% smaller
  crates/router/src/core/payments/operations/payment_update.rs  0% smaller
  crates/router/src/core/payments/retry.rs  0% smaller
  crates/router/src/core/payments/transformers.rs  0% smaller
  crates/router/src/core/utils.rs  0% smaller
  crates/router/src/types/api/admin.rs  0% smaller
  crates/router/src/types/api/verify_connector.rs  0% smaller
  crates/router/src/types/storage/payment_attempt.rs  0% smaller
  crates/router/src/utils/user/sample_data.rs  0% smaller
  crates/router/tests/connectors/utils.rs  0% smaller
  crates/router/tests/payments.rs  0% smaller
  crates/router/tests/payments2.rs  0% smaller
  crates/storage_impl/src/mock_db/payment_attempt.rs  0% smaller
  crates/storage_impl/src/payments/payment_attempt.rs  0% smaller
  migrations/2025-04-10-084950_add_request_overcapture/down.sql Unsupported file format
  migrations/2025-04-10-084950_add_request_overcapture/up.sql Unsupported file format

semanticdiff-com[bot] avatar Dec 12 '24 07:12 semanticdiff-com[bot]

@CodiumAI-Agent /review

AkshayaFoiger avatar Apr 15 '25 09:04 AkshayaFoiger

PR Reviewer Guide 🔍

(Review updated until commit https://github.com/juspay/hyperswitch/commit/38f0791c7ca6f491f1aa05c2c33de7fce6b3b057)

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 5 🔵🔵🔵🔵🔵
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Mapping Logic

The conversion from Option<bool> to StripeOvercaptureRequest maps both Some(false) and None to None, but the enum offers an explicit Never variant. Consider whether Some(false) should map to Some(StripeOvercaptureRequest::Never) instead of None to clearly differentiate a skip request from absence of the field.

payment_method_data_card_request_overcapture: match is_request_overcapture {
    Some(true) => Some(StripeOvercaptureRequest::IfAvailable),
    Some(false) | None => None,
},
Response Extraction

extract_overcapture_response_from_latest_charge uses unwrap_or((None, None)), which may mask missing nested data and always return a default tuple. This can hide partial data and lead to dropping valid status or amount. Consider restructuring to avoid blanket unwraps and preserve partial presence.

fn extract_overcapture_response_from_latest_charge(
    latest_charge: &StripeChargeEnum,
) -> Option<types::OverCaptureData> {
    let (overcapture_status, maximum_overcapture_amount) = match latest_charge {
        StripeChargeEnum::ChargeObject(charge_object) => charge_object
            .payment_method_details
            .as_ref()
            .and_then(|payment_method_details| match payment_method_details {
                StripePaymentMethodDetailsResponse::Card { card } => {
                    let overcapture_status = card.overcapture.as_ref().and_then(|overcapture| {
                        match overcapture.status.clone() {
                            Some(StripeOverCaptureStatus::Available) => {
                                Some(common_enums::OverCaptureStatus::Applicable)
                            }
                            Some(StripeOverCaptureStatus::Unavailable) => {
                                Some(common_enums::OverCaptureStatus::NotApplicable)
                            }
                            None => None,
                        }
                    });
                    let maximum_capturable_amount = card
                        .overcapture
                        .as_ref()
                        .and_then(|overcapture| overcapture.maximum_amount_capturable);
                    Some((overcapture_status, maximum_capturable_amount))
                }
                _ => None,
            })
            .unwrap_or((None, None)),
        _ => (None, None),
    };
    overcapture_status
        .zip(maximum_overcapture_amount)
        .map(|overcapture_data| types::OverCaptureData::new(overcapture_data.0, overcapture_data.1))
}
Validation Semantics

validate_and_get_overcapture_status applies profile defaults only when confirm == true and returns None otherwise, potentially ignoring always_request_overcapture. Ensure the logic aligns with API contract and consider whether profile defaults should apply even when confirm is false.

pub fn validate_overcapture_request(
    capture_method: Option<api_enums::CaptureMethod>,
    request_overcapture: Option<api_enums::OverCaptureRequest>,
) -> Result<(), errors::ApiErrorResponse> {
    utils::when(
        request_overcapture.is_some_and(|request_overcapture| request_overcapture.is_enabled())
            && !(capture_method.is_some_and(|capture_method| capture_method.is_manual())),
        || {
            Err(errors::ApiErrorResponse::PreconditionFailed {
                message: "Requesting overcapture is only supported when the capture method is set to manual".to_string(),
            })
        },
    )
}

#[cfg(feature = "v1")]
pub fn validate_and_get_overcapture_status(
    capture_method: &Option<storage_enums::CaptureMethod>,
    request_overcapture: &Option<api_enums::OverCaptureRequest>,
    profile: &domain::Profile,
    confirm: bool,
) -> Result<Option<api_enums::OverCaptureStatus>, errors::ApiErrorResponse> {
    if let Some(api_enums::CaptureMethod::Manual) = capture_method {
        let is_overcapture_applied = match *request_overcapture {
            Some(request_overcapture) => {
                Some(api_enums::OverCaptureStatus::from(request_overcapture))
            }
            None => {
                if confirm {
                    profile
                        .always_request_overcapture
                        .map(api_enums::OverCaptureStatus::from)
                } else {
                    None
                }
            }
        };
        Ok(is_overcapture_applied)
    } else {
        request_overcapture.map_or(
            Ok(None),
            |_| Err(errors::ApiErrorResponse::PreconditionFailed {
                message: "Requesting overcapture is only supported when the capture method is set to manual".to_string(),
            })
        )
    }
}

QodoAI-Agent avatar Apr 15 '25 09:04 QodoAI-Agent

Work In Progress 🚧

AkshayaFoiger avatar Apr 24 '25 11:04 AkshayaFoiger

Persistent review updated to latest commit https://github.com/juspay/hyperswitch/commit/38f0791c7ca6f491f1aa05c2c33de7fce6b3b057

QodoAI-Agent avatar Apr 24 '25 11:04 QodoAI-Agent

Implementing as part of another pull request

AkshayaFoiger avatar Aug 06 '25 11:08 AkshayaFoiger