juspay / hyperswitch

An open source payments switch written in Rust to make payments fast, reliable and affordable
https://hyperswitch.io/
Apache License 2.0
12.19k stars 1.29k forks source link

[FEATURE] Send Payment Object in Payments Response in Connector Error also #1163

Open Abhicodes-crypto opened 1 year ago

Abhicodes-crypto commented 1 year ago

If a connector error is encountered currently the response contains only the connector error. It should also contain the payments object. So that the relevant parameters are returned and additional API calls need not be made to know the status of payment etc. Based on this next action can be taken by the merchant App.

Tasks :

Abhicodes-crypto commented 1 year ago

Currently ,

When the connector returns a response with http status code between 400-599 . Hyperswitch sends the the response as

{
    "error": {
         "code": "code_sent by connector",
         "message":  "error_message_sent by connector",
         "connector":  "connector name",
         "type": "connector"
}

For example,

{
    "error": {
        "type": "connector",
        "message": "incorrect_number: Your card number is incorrect.",
        "code": "CE_00",
        "connector": "stripe"
    }
}

It doesn't give any information about payments object that was created on hyperswitch. If it was sent in a confirm call, the payment_id is known so a sync call can be made.

But if this happened in a Create call no information regarding the payment object is known.

So after discussions with @manojradhakrishnan and @jarnura the API contract is being updated.

Now, if connector sends 400-599 errors

Hyperswitch would still send the response with http 200 status code and that response would be similar to

{
    "payment_id": "pay_GX2eIgbiPIA55yQ4BU0q",
    "merchant_id": "dsu371",
    "status": "failed",
    "amount": 6540,
    "amount_capturable": null,
    "amount_received": 0,
    "connector": "stripe",
    "client_secret": "pay_GX2eIgbiPIA55yQ4BU0q_secret_rPaGWILP5OOTZ5gpuiT8",
    "created": "2023-05-19T11:06:37.369Z",
    "currency": "USD",
    "customer_id": "StripeCustomer1",
    "description": "Its my first payment request",
    "refunds": null,
    "mandate_id": null,
    "mandate_data": null,
    "setup_future_usage": null,
    "off_session": null,
    "capture_on": null,
    "capture_method": "automatic",
    "payment_method": "card",
    "payment_method_data": {
        "card": {
            "last4": "0119",
            "exp_month": "10",
            "exp_year": "25"
        }
    },
    "payment_token": "token_OKme5f6irL8lJfUIpVPY",
    "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": "8056594427",
            "country_code": "+91"
        }
    },
    "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"
        }
    },
    "metadata": {
        "udf1": "value1",
        "payload": null,
        "login_date": "2019-09-10T10:11:12Z",
        "new_customer": "true",
        "order_details": null,
        "allowed_payment_method_types": null
    },
    "email": "guest@example.com",
    "name": "John Doe",
    "phone": "999999999",
    "return_url": "https://google.com/",
    "authentication_type": "no_three_ds",
    "statement_descriptor_name": "joseph",
    "statement_descriptor_suffix": "JS",

    "cancellation_reason": null,
    "error_code": null,
    "error_message": null,
    "error_details": {
        "type": "connector",
        "message": "incorrect_number: Your card number is incorrect.",
        "code": "CE_00",
        "connector": "stripe",
        "status_code": "402"
     },

    "payment_experience": null,
    "payment_method_type": "credit",
    "connector_label": "stripe_US_default",
    "business_country": "US",
    "business_label": "default",
    "business_sub_label": null,
    "allowed_payment_method_types": null
}

This will help in integration to take decisions based on payment status and another sync call is not needed. Also in payment create with confirm = true a payment id is received if it is created.

Hyperswitch will throw HE errors for 400-599 http status codes. It means it is an error due to Hyperswitch and not connector.

manojradhakrishnan commented 1 year ago

@akash-kamble-juspay This is a API contract change for better developer experience. SDK might have to adjust to this edge case (which will be mostly due to configuration error or PG downtimes)