Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(payments_v2): implement payments capture v2 #6722

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

Narayanbhat166
Copy link
Member

@Narayanbhat166 Narayanbhat166 commented Dec 2, 2024

Type of Change

  • New feature

Description

This PR introduces payments capture endpoint for the v2 version.

This PR also changes the GenerateResponse trait to be implemented on the payment data. The new trait looks like this

pub trait GenerateResponse<Response>
where
    Self: Sized,
{
    #[cfg(feature = "v2")]
    fn generate_response(
        self,
        state: &SessionState,
        connector_http_status_code: Option<u16>,
        external_latency: Option<u128>,
        is_latency_header_enabled: Option<bool>,
        merchant_account: &domain::MerchantAccount,
    ) -> RouterResponse<Response>;
}

Also the fields in AttemptAmountDetails are made as private, necessary getters and setters are provided to work with these fields.

Resolved some of the clippy warnings for v2.

Motivation and Context

How did you test it?

Create a payment with automatic capture
curl --location 'http://localhost:8080/v2/payments/create-intent' \
--header 'api-key: dev_WvQ9nNM8yudKrDcVqh3FrURJm3HMKU3tmxMFKnlZef9Hkhyxy1r9A89I4s03jO9h' \
--header 'Content-Type: application/json' \
--header 'x-profile-id: pro_MxajZP8J2IgEXmAs0MpY' \
--data '{
    "amount_details": {
        "order_amount": 100,
        "currency": "USD"
    },
    "capture_method":"automatic",
    "authentication_type": "no_three_ds"
}'
  • Response
{
    "id": "12345_pay_0193882e51ac7692905c3279518700eb",
    "status": "requires_payment_method",
    "amount_details": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "skip_external_tax_calculation": "Skip",
        "skip_surcharge_calculation": "Skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null
    },
    "client_secret": "12345_pay_0193882e51ac7692905c3279518700eb_secret_0193882e51ac7692905c3284173ac888",
    "merchant_reference_id": null,
    "routing_algorithm_id": null,
    "capture_method": "automatic",
    "authentication_type": "no_three_ds",
    "billing": null,
    "shipping": null,
    "customer_id": null,
    "customer_present": "Present",
    "description": null,
    "return_url": null,
    "setup_future_usage": "on_session",
    "apply_mit_exemption": "Skip",
    "statement_descriptor": null,
    "order_details": null,
    "allowed_payment_method_types": null,
    "metadata": null,
    "connector_metadata": null,
    "feature_metadata": null,
    "payment_link_enabled": "Skip",
    "payment_link_config": null,
    "request_incremental_authorization": "default",
    "expires_on": "2024-12-02T16:37:37.228Z",
    "frm_metadata": null,
    "request_external_three_ds_authentication": "Skip"
}
Confirm the payment intent
curl --location 'http://localhost:8080/v2/payments/12345_pay_0193882e51ac7692905c3279518700eb/confirm-intent' \
--header 'x-client-secret: 12345_pay_0193882e51ac7692905c3279518700eb_secret_0193882e51ac7692905c3284173ac888' \
--header 'x-profile-id: pro_MxajZP8J2IgEXmAs0MpY' \
--header 'Content-Type: application/json' \
--header 'api-key: pk_dev_538be9702de84258b89c6ac654f5ecb1' \
--data '{
    "payment_method_data": {
        "card": {
            "card_number": "4242424242424242",
            "card_exp_month": "01",
            "card_exp_year": "25",
            "card_holder_name": "John Doe",
            "card_cvc": "100"
        }
    },
    "payment_method_type": "card",
    "payment_method_subtype": "credit"
}'
  • In the response, amount_capturable should be 0 indicating that no amount can be captured further. amount_captured should be equal to the net amount.
{
    "id": "12345_pay_0193882e51ac7692905c3279518700eb",
    "status": "succeeded",
    "amount": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "skip_external_tax_calculation": "Skip",
        "skip_surcharge_calculation": "Skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null,
        "net_amount": 100,
        "amount_to_capture": null,
        "amount_capturable": 0,
        "amount_captured": 100
    },
    "connector": "stripe",
    "client_secret": "12345_pay_0193882e51ac7692905c3279518700eb_secret_0193882e51ac7692905c3284173ac888",
    "created": "2024-12-02T16:22:37.228Z",
    "payment_method_data": null,
    "payment_method_type": "card",
    "payment_method_subtype": "credit",
    "next_action": null,
    "connector_transaction_id": "pi_3QRcOZD5R7gDAGff1DNcLrku",
    "connector_reference_id": null,
    "merchant_connector_id": "mca_viTlEW768QtaKIyYYGjl",
    "browser_info": null,
    "error": null
}
Create a payment with manual capture
curl --location 'http://localhost:8080/v2/payments/create-intent' \
--header 'api-key: dev_WvQ9nNM8yudKrDcVqh3FrURJm3HMKU3tmxMFKnlZef9Hkhyxy1r9A89I4s03jO9h' \
--header 'Content-Type: application/json' \
--header 'x-profile-id: pro_MxajZP8J2IgEXmAs0MpY' \
--data '{
    "amount_details": {
        "order_amount": 100,
        "currency": "USD"
    },
    "capture_method":"manual",
    "authentication_type": "no_three_ds"
}'
Confirm the payment intent
curl --location 'http://localhost:8080/v2/payments/create-intent' \
--header 'api-key: dev_WvQ9nNM8yudKrDcVqh3FrURJm3HMKU3tmxMFKnlZef9Hkhyxy1r9A89I4s03jO9h' \
--header 'Content-Type: application/json' \
--header 'x-profile-id: pro_MxajZP8J2IgEXmAs0MpY' \
--data '{
    "amount_details": {
        "order_amount": 100,
        "currency": "USD"
    },
    "capture_method":"manual",
    "authentication_type": "no_three_ds"
}'
  • The status should be requires_capture, with amount_capturable equal to the net amount
{
    "id": "12345_pay_0193967efce773d38d2db5dcd00df061",
    "status": "requires_capture",
    "amount": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "external_tax_calculation": "skip",
        "surcharge_calculation": "skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null,
        "net_amount": 100,
        "amount_to_capture": null,
        "amount_capturable": 100,
        "amount_captured": 0
    },
    "connector": "stripe",
    "client_secret": "12345_pay_0193967efce773d38d2db5dcd00df061_secret_0193967efce87481a4075ab617dba612",
    "created": "2024-12-05T11:05:24.969Z",
    "payment_method_data": {
        "billing": null
    },
    "payment_method_type": "card",
    "payment_method_subtype": "credit",
    "next_action": null,
    "connector_transaction_id": "pi_3QScs9D5R7gDAGff1Vn6BBrV",
    "connector_reference_id": null,
    "merchant_connector_id": "mca_VyXpopmpz9vwaQaNhmER",
    "browser_info": null,
    "error": null
}
Capture the payment
curl --location 'http://localhost:8080/v2/payments/12345_pay_019396c5a45870c1a45722b60b0159b5/capture' \
--header 'x-profile-id: pro_ByYDje1ZZELFwRyx8IIS' \
--header 'Content-Type: application/json' \
--header 'api-key: dev_cj5iA6st5Dn6o8Grs6vrA9SvblTcRcwUWQ6LzC27Qv80wQQPodhTZuFgBcV59JDA' \
--data '{}'
  • Response
{
    "id": "12345_pay_019396c5a45870c1a45722b60b0159b5",
    "status": "succeeded",
    "amount": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "external_tax_calculation": "skip",
        "surcharge_calculation": "skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null,
        "net_amount": 100,
        "amount_to_capture": null,
        "amount_capturable": 0,
        "amount_captured": 100
    }
}
Create a 3DS payment
curl --location 'http://localhost:8080/v2/payments/create-intent' \
--header 'api-key: dev_cj5iA6st5Dn6o8Grs6vrA9SvblTcRcwUWQ6LzC27Qv80wQQPodhTZuFgBcV59JDA' \
--header 'Content-Type: application/json' \
--header 'x-profile-id: pro_ByYDje1ZZELFwRyx8IIS' \
--data-raw '{
    "amount_details": {
        "order_amount": 100,
        "currency": "USD"
    },
    "capture_method":"manual",
    "authentication_type": "three_ds",
    "billing": {
        "address": {
            "first_name": "John",
            "last_name": "Dough"
        },
        "email": "[email protected]"
    },
    "shipping": {
        "address": {
            "first_name": "John",
            "last_name": "Dough",
            "city": "Karwar",
            "zip": "581301",
            "state": "Karnataka"
        },
        "email": "[email protected]"
    }
}'
  • Response
{
    "id": "12345_pay_019396c80c8074d0a664df3d3a194856",
    "status": "requires_payment_method",
    "amount_details": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "external_tax_calculation": "skip",
        "surcharge_calculation": "skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null
    },
    "client_secret": "12345_pay_019396c80c8074d0a664df3d3a194856_secret_019396c80c8074d0a664df4671f65402",
    "profile_id": "pro_ByYDje1ZZELFwRyx8IIS",
    "merchant_reference_id": null,
    "routing_algorithm_id": null,
    "capture_method": "manual",
    "authentication_type": "three_ds",
    "billing": {
        "address": {
            "city": null,
            "country": null,
            "line1": null,
            "line2": null,
            "line3": null,
            "zip": null,
            "state": null,
            "first_name": "John",
            "last_name": "Dough"
        },
        "phone": null,
        "email": "[email protected]"
    },
    "shipping": {
        "address": {
            "city": "Karwar",
            "country": null,
            "line1": null,
            "line2": null,
            "line3": null,
            "zip": "581301",
            "state": "Karnataka",
            "first_name": "John",
            "last_name": "Dough"
        },
        "phone": null,
        "email": "[email protected]"
    },
    "customer_id": null,
    "customer_present": "Present",
    "description": null,
    "return_url": null,
    "setup_future_usage": "on_session",
    "apply_mit_exemption": "Skip",
    "statement_descriptor": null,
    "order_details": null,
    "allowed_payment_method_types": null,
    "metadata": null,
    "connector_metadata": null,
    "feature_metadata": null,
    "payment_link_enabled": "Skip",
    "payment_link_config": null,
    "request_incremental_authorization": "default",
    "expires_on": "2024-12-05T12:40:13.089Z",
    "frm_metadata": null,
    "request_external_three_ds_authentication": "Skip"
}
  • Confirm the payment intent
curl --location 'http://localhost:8080/v2/payments/12345_pay_019396cc683b7a2195085791a9018983/confirm-intent' \
--header 'x-client-secret: 12345_pay_019396cc683b7a2195085791a9018983_secret_019396cc683c72e3a33309c8f573d29e' \
--header 'x-profile-id: pro_ByYDje1ZZELFwRyx8IIS' \
--header 'Content-Type: application/json' \
--header 'api-key: pk_dev_1896564f01cc4ffc8039f4e538a43b88' \
--data '{
    "payment_method_data": {
        "card": {
            "card_number": "4242424242424242",
            "card_exp_month": "01",
            "card_exp_year": "25",
            "card_holder_name": "John Doe",
            "card_cvc": "100"
        }
    },
    "payment_method_type": "card",
    "payment_method_subtype": "credit"
}'
  • Response
{
    "id": "12345_pay_019396cc683b7a2195085791a9018983",
    "status": "requires_customer_action",
    "amount": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "external_tax_calculation": "skip",
        "surcharge_calculation": "skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null,
        "net_amount": 100,
        "amount_to_capture": null,
        "amount_capturable": 0,
        "amount_captured": null
    },
    "connector": "stripe",
    "client_secret": "12345_pay_019396cc683b7a2195085791a9018983_secret_019396cc683c72e3a33309c8f573d29e",
    "created": "2024-12-05T12:29:58.716Z",
    "payment_method_data": {
        "billing": null
    },
    "payment_method_type": "card",
    "payment_method_subtype": "credit",
    "next_action": {
        "type": "redirect_to_url",
        "redirect_to_url": "http://localhost:8080/v2/payments/12345_pay_019396cc683b7a2195085791a9018983/start-redirection?publishable_key=pk_dev_1896564f01cc4ffc8039f4e538a43b88&profile_id=pro_ByYDje1ZZELFwRyx8IIS"
    },
    "connector_transaction_id": "pi_3QSeC3D5R7gDAGff0mIJA1Wg",
    "connector_reference_id": null,
    "merchant_connector_id": "mca_VyXpopmpz9vwaQaNhmER",
    "browser_info": null,
    "error": null
}
  • Complete the redirection
    image

  • Retrieve the payment

curl --location 'http://localhost:8080/v2/payments/12345_pay_019396cc683b7a2195085791a9018983' \
--header 'x-profile-id: pro_ByYDje1ZZELFwRyx8IIS' \
--header 'api-key: dev_cj5iA6st5Dn6o8Grs6vrA9SvblTcRcwUWQ6LzC27Qv80wQQPodhTZuFgBcV59JDA' \
--data ''
  • Response
{
    "id": "12345_pay_019396cc683b7a2195085791a9018983",
    "status": "requires_capture",
    "amount": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "external_tax_calculation": "skip",
        "surcharge_calculation": "skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null,
        "net_amount": 100,
        "amount_to_capture": null,
        "amount_capturable": 100,
        "amount_captured": 100
    },
    "connector": "stripe",
    "client_secret": "12345_pay_019396cc683b7a2195085791a9018983_secret_019396cc683c72e3a33309c8f573d29e",
    "created": "2024-12-05T12:29:58.716Z",
    "payment_method_data": {
        "billing": null
    },
    "payment_method_type": "card",
    "payment_method_subtype": "credit",
    "connector_transaction_id": "pi_3QSeC3D5R7gDAGff0mIJA1Wg",
    "connector_reference_id": null,
    "merchant_connector_id": "mca_VyXpopmpz9vwaQaNhmER",
    "browser_info": null,
    "error": null,
    "shipping": {
        "address": {
            "city": "Karwar",
            "country": null,
            "line1": null,
            "line2": null,
            "line3": null,
            "zip": "581301",
            "state": "Karnataka",
            "first_name": "John",
            "last_name": "Dough"
        },
        "phone": null,
        "email": "[email protected]"
    },
    "billing": {
        "address": {
            "city": null,
            "country": null,
            "line1": null,
            "line2": null,
            "line3": null,
            "zip": null,
            "state": null,
            "first_name": "John",
            "last_name": "Dough"
        },
        "phone": null,
        "email": "[email protected]"
    }
}
  • Capture the payment
curl --location 'http://localhost:8080/v2/payments/12345_pay_019396cc683b7a2195085791a9018983/capture' \
--header 'x-profile-id: pro_ByYDje1ZZELFwRyx8IIS' \
--header 'Content-Type: application/json' \
--header 'api-key: dev_cj5iA6st5Dn6o8Grs6vrA9SvblTcRcwUWQ6LzC27Qv80wQQPodhTZuFgBcV59JDA' \
--data '{}'
  • Response
{
    "id": "12345_pay_019396cc683b7a2195085791a9018983",
    "status": "succeeded",
    "amount": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "external_tax_calculation": "skip",
        "surcharge_calculation": "skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null,
        "net_amount": 100,
        "amount_to_capture": null,
        "amount_capturable": 0,
        "amount_captured": 100
    }
}
Create a partial capture
  • Create a payment
curl --location 'http://localhost:8080/v2/payments/create-intent' \
--header 'api-key: dev_cj5iA6st5Dn6o8Grs6vrA9SvblTcRcwUWQ6LzC27Qv80wQQPodhTZuFgBcV59JDA' \
--header 'Content-Type: application/json' \
--header 'x-profile-id: pro_ByYDje1ZZELFwRyx8IIS' \
--data-raw '{
    "amount_details": {
        "order_amount": 100,
        "currency": "USD"
    },
    "capture_method":"manual",
    "authentication_type": "no_three_ds",
    "billing": {
        "address": {
            "first_name": "John",
            "last_name": "Dough"
        },
        "email": "[email protected]"
    },
    "shipping": {
        "address": {
            "first_name": "John",
            "last_name": "Dough",
            "city": "Karwar",
            "zip": "581301",
            "state": "Karnataka"
        },
        "email": "[email protected]"
    }
}'
  • Response
{
    "id": "12345_pay_019396d160917c41baa6130f8716dbb7",
    "status": "requires_payment_method",
    "amount_details": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "external_tax_calculation": "skip",
        "surcharge_calculation": "skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null
    },
    "client_secret": "12345_pay_019396d160917c41baa6130f8716dbb7_secret_019396d160917c41baa6131341a1bc9e",
    "profile_id": "pro_ByYDje1ZZELFwRyx8IIS",
    "merchant_reference_id": null,
    "routing_algorithm_id": null,
    "capture_method": "manual",
    "authentication_type": "no_three_ds",
    "billing": {
        "address": {
            "city": null,
            "country": null,
            "line1": null,
            "line2": null,
            "line3": null,
            "zip": null,
            "state": null,
            "first_name": "John",
            "last_name": "Dough"
        },
        "phone": null,
        "email": "[email protected]"
    },
    "shipping": {
        "address": {
            "city": "Karwar",
            "country": null,
            "line1": null,
            "line2": null,
            "line3": null,
            "zip": "581301",
            "state": "Karnataka",
            "first_name": "John",
            "last_name": "Dough"
        },
        "phone": null,
        "email": "[email protected]"
    },
    "customer_id": null,
    "customer_present": "Present",
    "description": null,
    "return_url": null,
    "setup_future_usage": "on_session",
    "apply_mit_exemption": "Skip",
    "statement_descriptor": null,
    "order_details": null,
    "allowed_payment_method_types": null,
    "metadata": null,
    "connector_metadata": null,
    "feature_metadata": null,
    "payment_link_enabled": "Skip",
    "payment_link_config": null,
    "request_incremental_authorization": "default",
    "expires_on": "2024-12-05T12:50:24.434Z",
    "frm_metadata": null,
    "request_external_three_ds_authentication": "Skip"
}
  • confirm the payment
curl --location 'http://localhost:8080/v2/payments/12345_pay_019396d160917c41baa6130f8716dbb7/confirm-intent' \
--header 'x-client-secret: 12345_pay_019396d160917c41baa6130f8716dbb7_secret_019396d160917c41baa6131341a1bc9e' \
--header 'x-profile-id: pro_ByYDje1ZZELFwRyx8IIS' \
--header 'Content-Type: application/json' \
--header 'api-key: pk_dev_1896564f01cc4ffc8039f4e538a43b88' \
--data '{
    "payment_method_data": {
        "card": {
            "card_number": "4242424242424242",
            "card_exp_month": "01",
            "card_exp_year": "25",
            "card_holder_name": "John Doe",
            "card_cvc": "100"
        }
    },
    "payment_method_type": "card",
    "payment_method_subtype": "credit"
}'
  • Response
{
    "id": "12345_pay_019396d160917c41baa6130f8716dbb7",
    "status": "requires_capture",
    "amount": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "external_tax_calculation": "skip",
        "surcharge_calculation": "skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null,
        "net_amount": 100,
        "amount_to_capture": null,
        "amount_capturable": 100,
        "amount_captured": 0
    },
    "connector": "stripe",
    "client_secret": "12345_pay_019396d160917c41baa6130f8716dbb7_secret_019396d160917c41baa6131341a1bc9e",
    "created": "2024-12-05T12:35:24.434Z",
    "payment_method_data": {
        "billing": null
    },
    "payment_method_type": "card",
    "payment_method_subtype": "credit",
    "next_action": null,
    "connector_transaction_id": "pi_3QSeIyD5R7gDAGff1woLG5lU",
    "connector_reference_id": null,
    "merchant_connector_id": "mca_VyXpopmpz9vwaQaNhmER",
    "browser_info": null,
    "error": null
}
  • Capture the payment with amount_to_capture
curl --location 'http://localhost:8080/v2/payments/12345_pay_019396d160917c41baa6130f8716dbb7/capture' \
--header 'x-profile-id: pro_ByYDje1ZZELFwRyx8IIS' \
--header 'Content-Type: application/json' \
--header 'api-key: dev_cj5iA6st5Dn6o8Grs6vrA9SvblTcRcwUWQ6LzC27Qv80wQQPodhTZuFgBcV59JDA' \
--data '{
    "amount_to_capture": 69
}'
  • Response, status should be partially_captured
{
    "id": "12345_pay_019396d160917c41baa6130f8716dbb7",
    "status": "partially_captured",
    "amount": {
        "order_amount": 100,
        "currency": "USD",
        "shipping_cost": null,
        "order_tax_amount": null,
        "external_tax_calculation": "skip",
        "surcharge_calculation": "skip",
        "surcharge_amount": null,
        "tax_on_surcharge": null,
        "net_amount": 100,
        "amount_to_capture": 69,
        "amount_capturable": 0,
        "amount_captured": 69
    }
}

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code

Copy link

semanticdiff-com bot commented Dec 2, 2024

@hyperswitch-bot hyperswitch-bot bot added the M-api-contract-changes Metadata: This PR involves API contract changes label Dec 2, 2024
@Narayanbhat166 Narayanbhat166 self-assigned this Dec 3, 2024
@Narayanbhat166 Narayanbhat166 marked this pull request as ready for review December 4, 2024 17:18
@Narayanbhat166 Narayanbhat166 requested review from a team as code owners December 4, 2024 17:18
jarnura
jarnura previously approved these changes Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-v2 M-api-contract-changes Metadata: This PR involves API contract changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants