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(connector): [Klarna] Add support for Capture, Psync, Refunds and Rsync flows #4799

Merged
merged 21 commits into from
May 29, 2024

Conversation

swangi-kumari
Copy link
Contributor

@swangi-kumari swangi-kumari commented May 28, 2024

Type of Change

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

Description

Add support for Capture, Psync, Refunds and Rsync for Klarna Connector

Additional Changes

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

Motivation and Context

How did you test it?

  1. Create a Merchant
{
  "merchant_id": "merchant_{{$timestamp}}",
  "locker_id": "m0010",
  "merchant_name": "NewAge Retailer",
  "merchant_details": {
    "primary_contact_person": "John Test",
    "primary_email": "[email protected]",
    "primary_phone": "sunt laborum",
    "secondary_contact_person": "John Test2",
    "secondary_email": "[email protected]",
    "secondary_phone": "cillum do dolor id",
    "website": "https://www.example.com",
    "about_business": "Online Retail with a wide selection of organic products for North America",
    "address": {
      "line1": "1467",
      "line2": "Harrison Street",
      "line3": "Harrison Street",
      "city": "San Fransico",
      "state": "California",
      "zip": "94122",
      "country": "US"
    }
  },
  "return_url": "https://google.com/success",
  "webhook_details": {
    "webhook_version": "1.0.1",
    "webhook_username": "ekart_retail",
    "webhook_password": "password_ekart@123",
    "payment_created_enabled": true,
    "payment_succeeded_enabled": true,
    "payment_failed_enabled": true
  },
  "sub_merchants_enabled": false,
  "metadata": {
    "city": "NY",
    "unit": "245"
  },
  "primary_business_details": [
    {
      "country": "US",
      "business": "food"
    }
  ]
}

  1. Create API Key
{
  "name": "API Key 1",
  "description": null,
  "expiration": "2038-01-19T03:14:08.000Z"
}

  1. Create MCA
{
    "connector_type": "fiz_operations",
    "connector_name": "klarna",
    "connector_account_details": {
        "auth_type": "BodyKey",
        "api_key": "Password",
        "key1": "username"
    },
    "test_mode": true,
    "disabled": false,
    "payment_methods_enabled": [
        {
            "payment_method": "pay_later",
            "payment_method_types": [
                {
                    "minimum_amount": 1,
                    "maximum_amount": 68607706,
                    "recurring_enabled": true,
                    "installment_payment_enabled": true,
                    "payment_experience": "invoke_sdk_client",
                    "payment_method_type": "klarna"
                }
            ]
        },
        {
            "payment_method": "pay_later",
            "payment_method_types": [
                {
                    "minimum_amount": 1,
                    "maximum_amount": 68607706,
                    "recurring_enabled": true,
                    "installment_payment_enabled": true,
                    "payment_experience": "redirect_to_url",
                    "payment_method_type": "klarna"
                }
            ]
        }
    ],
    "metadata": {
        "city": "NY",
        "unit": "245",
        "klarna_region": "NorthAmerica"
    }
}

In metadata try 3 values and do a Klarna Payment via SDK for Klarna Connector for each klarna_region
i.

"metadata": {
       "city": "NY",
       "unit": "245",
       "klarna_region": "Europe"
   }

ii.

"metadata": {
       "city": "NY",
       "unit": "245",
       "klarna_region": "NorthAmerica"
   }

iii.

"metadata": {
       "city": "NY",
       "unit": "245",
       "region_based_endpoint": "Oceania"
   }

Create a payment via sdk with capture_method = manual

{
      currency: "USD",
      amount: 5000,
      order_details: [
        {
          product_name: "Apple iphone 15",
          quantity: 1,
          amount: 5000,
        },
      ],
      confirm: false,
      capture_method: "manual",
      authentication_type: "three_ds",
      customer_id: "hyperswitch_sdk_demo_id",
      email: "[email protected]",
      description: "Hello this is description",
      connector_metadata: {
        noon: {
          order_category: "applepay",
        },
      },
      metadata: {
        udf1: "value1",
        new_customer: "true",
        login_date: "2019-09-10T10:11:12Z",
      },
      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",
        },
      },
      }

Capture the Payment

curl --location 'http://localhost:8080/payments/{payment_id}/capture' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: ******************' \
--data '{
  "amount_to_capture": 5000,
  "statement_descriptor_name": "Joseph",
  "statement_descriptor_suffix": "JS"
}'

Response:

{
    "payment_id": "pay_bH1TZc5hf9jqEbFSz7GH",
    "merchant_id": "merchant_1716967519",
    "status": "succeeded",
    "amount": 5000,
    "net_amount": 5000,
    "amount_capturable": 0,
    "amount_received": 5000,
    "connector": "klarna",
    "client_secret": "pay_bH1TZc5hf9jqEbFSz7GH_secret_4nmkltwKTUENqgGmjgsi",
    "created": "2024-05-29T11:30:39.409Z",
    "currency": "USD",
    "customer_id": "hyperswitch_sdk_demo_id",
    "customer": {
        "id": "hyperswitch_sdk_demo_id",
        "name": null,
        "email": "[email protected]",
        "phone": null,
        "phone_country_code": null
    },
    "description": "Hello this is description",
    "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": "pay_later",
    "payment_method_data": {
        "pay_later": {},
        "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": [
        {
            "brand": null,
            "amount": 5000,
            "category": null,
            "quantity": 1,
            "product_id": null,
            "product_name": "Apple iphone 15",
            "product_type": null,
            "sub_category": null,
            "product_img_link": null,
            "requires_shipping": null
        }
    ],
    "email": "[email protected]",
    "name": null,
    "phone": null,
    "return_url": "http://localhost:9060/",
    "authentication_type": "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": "invoke_sdk_client",
    "payment_method_type": "klarna",
    "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": "3a20817c-8956-4ef7-9627-12967b4e9e17",
    "frm_message": null,
    "metadata": {
        "udf1": "value1",
        "login_date": "2019-09-10T10:11:12Z",
        "new_customer": "true"
    },
    "connector_metadata": {
        "apple_pay": null,
        "airwallex": null,
        "noon": {
            "order_category": "applepay"
        }
    },
    "feature_metadata": null,
    "reference_id": "3a20817c-8956-4ef7-9627-12967b4e9e17",
    "payment_link": null,
    "profile_id": "pro_W1dQQFAsWElgN0Uk9yjx",
    "surcharge_details": null,
    "attempt_count": 1,
    "merchant_decision": null,
    "merchant_connector_id": "mca_CR12bLCzbHKYSPJtvqzK",
    "incremental_authorization_allowed": null,
    "authorization_count": null,
    "incremental_authorizations": null,
    "external_authentication_details": null,
    "external_3ds_authentication_attempted": false,
    "expires_on": "2024-05-29T11:45:39.409Z",
    "fingerprint": null,
    "browser_info": {
        "language": "en-GB",
        "time_zone": -330,
        "ip_address": "::1",
        "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
        "color_depth": 24,
        "java_enabled": true,
        "screen_width": 1728,
        "accept_header": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "screen_height": 1117,
        "java_script_enabled": true
    },
    "payment_method_id": null,
    "payment_method_status": null,
    "updated": "2024-05-29T11:31:21.792Z",
    "charges": null,
    "frm_metadata": null
}

update payment_attempt set status = 'pending' and update payment_intent set status = 'processing'

Do a psync

curl --location 'http://localhost:8080/payments/{payment_id}?force_sync=true' \
--header 'Accept: application/json' \
--header 'api-key: **********************************\'

Response:

{
    "payment_id": "pay_NeIO8Rjzc10dYJrYsYz3",
    "merchant_id": "merchant_1716967519",
    "status": "succeeded",
    "amount": 5000,
    "net_amount": 5000,
    "amount_capturable": 0,
    "amount_received": 5000,
    "connector": "klarna",
    "client_secret": "pay_NeIO8Rjzc10dYJrYsYz3_secret_SyaVWvLioqrpMlUZIh9t",
    "created": "2024-05-29T09:55:50.071Z",
    "currency": "USD",
    "customer_id": "hyperswitch_sdk_demo_id",
    "customer": {
        "id": "hyperswitch_sdk_demo_id",
        "name": null,
        "email": "[email protected]",
        "phone": null,
        "phone_country_code": null
    },
    "description": "Hello this is description",
    "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": "pay_later",
    "payment_method_data": {
        "pay_later": {},
        "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": [
        {
            "brand": null,
            "amount": 5000,
            "category": null,
            "quantity": 1,
            "product_id": null,
            "product_name": "Apple iphone 15",
            "product_type": null,
            "sub_category": null,
            "product_img_link": null,
            "requires_shipping": null
        }
    ],
    "email": "[email protected]",
    "name": null,
    "phone": null,
    "return_url": "http://localhost:9060/",
    "authentication_type": "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": "invoke_sdk_client",
    "payment_method_type": "klarna",
    "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": "af406563-993d-48b8-83a4-56114856d7f9",
    "frm_message": null,
    "metadata": {
        "udf1": "value1",
        "login_date": "2019-09-10T10:11:12Z",
        "new_customer": "true"
    },
    "connector_metadata": {
        "apple_pay": null,
        "airwallex": null,
        "noon": {
            "order_category": "applepay"
        }
    },
    "feature_metadata": null,
    "reference_id": "af406563-993d-48b8-83a4-56114856d7f9",
    "payment_link": null,
    "profile_id": "pro_W1dQQFAsWElgN0Uk9yjx",
    "surcharge_details": null,
    "attempt_count": 1,
    "merchant_decision": null,
    "merchant_connector_id": "mca_CR12bLCzbHKYSPJtvqzK",
    "incremental_authorization_allowed": null,
    "authorization_count": null,
    "incremental_authorizations": null,
    "external_authentication_details": null,
    "external_3ds_authentication_attempted": false,
    "expires_on": "2024-05-29T10:10:50.071Z",
    "fingerprint": null,
    "browser_info": {
        "language": "en-GB",
        "time_zone": -330,
        "ip_address": "::1",
        "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
        "color_depth": 24,
        "java_enabled": true,
        "screen_width": 2560,
        "accept_header": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "screen_height": 1440,
        "java_script_enabled": true
    },
    "payment_method_id": null,
    "payment_method_status": null,
    "updated": "2024-05-29T09:58:44.297Z",
    "charges": null,
    "frm_metadata": null
}

Create a Refund

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

Response

{
    "refund_id": "ref_uzvvTjY9GNEEuiKtTgEa",
    "payment_id": "pay_bH1TZc5hf9jqEbFSz7GH",
    "amount": 5000,
    "currency": "USD",
    "status": "pending",
    "reason": "Customer returned product",
    "metadata": {
        "udf1": "value1",
        "new_customer": "true",
        "login_date": "2019-09-10T10:11:12Z"
    },
    "error_message": null,
    "error_code": null,
    "created_at": "2024-05-29T11:41:48.050Z",
    "updated_at": "2024-05-29T11:41:48.050Z",
    "connector": "klarna",
    "profile_id": "pro_W1dQQFAsWElgN0Uk9yjx",
    "merchant_connector_id": "mca_CR12bLCzbHKYSPJtvqzK",
    "charges": null
}

Do Rsync

curl --location 'http://localhost:8080/refunds/{refund_id}' \
--header 'Accept: application/json' \
--header 'api-key: *************'

Response:

{
    "refund_id": "ref_uzvvTjY9GNEEuiKtTgEa",
    "payment_id": "pay_bH1TZc5hf9jqEbFSz7GH",
    "amount": 5000,
    "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,
    "created_at": "2024-05-29T11:41:48.050Z",
    "updated_at": "2024-05-29T11:41:48.050Z",
    "connector": "klarna",
    "profile_id": "pro_W1dQQFAsWElgN0Uk9yjx",
    "merchant_connector_id": "mca_CR12bLCzbHKYSPJtvqzK",
    "charges": null
}

Create a payment with capture_method = manual
and then Void the payment.

curl --location 'http://localhost:8080/payments/{paymnet_id}/cancel' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: *****************' \
--data '{
  "cancellation_reason": "requested_by_customer"
}'

Response

{
    "payment_id": "pay_xqVbolfYyBBpqaZPBaDB",
    "merchant_id": "merchant_1716967519",
    "status": "cancelled",
    "amount": 5000,
    "net_amount": 5000,
    "amount_capturable": 0,
    "amount_received": null,
    "connector": "klarna",
    "client_secret": "pay_xqVbolfYyBBpqaZPBaDB_secret_3A4OQikb4oz99tyDb4UT",
    "created": "2024-05-29T10:52:04.202Z",
    "currency": "USD",
    "customer_id": "hyperswitch_sdk_demo_id",
    "customer": {
        "id": "hyperswitch_sdk_demo_id",
        "name": null,
        "email": "[email protected]",
        "phone": null,
        "phone_country_code": null
    },
    "description": "Hello this is description",
    "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": "pay_later",
    "payment_method_data": {
        "pay_later": {},
        "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": [
        {
            "brand": null,
            "amount": 5000,
            "category": null,
            "quantity": 1,
            "product_id": null,
            "product_name": "Apple iphone 15",
            "product_type": null,
            "sub_category": null,
            "product_img_link": null,
            "requires_shipping": null
        }
    ],
    "email": "[email protected]",
    "name": null,
    "phone": null,
    "return_url": "http://localhost:9060/",
    "authentication_type": "three_ds",
    "statement_descriptor_name": null,
    "statement_descriptor_suffix": null,
    "next_action": null,
    "cancellation_reason": "requested_by_customer",
    "error_code": null,
    "error_message": null,
    "unified_code": null,
    "unified_message": null,
    "payment_experience": "invoke_sdk_client",
    "payment_method_type": "klarna",
    "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": "e8922d04-7f80-49fe-a7a8-e40f5c49014f",
    "frm_message": null,
    "metadata": {
        "udf1": "value1",
        "login_date": "2019-09-10T10:11:12Z",
        "new_customer": "true"
    },
    "connector_metadata": {
        "apple_pay": null,
        "airwallex": null,
        "noon": {
            "order_category": "applepay"
        }
    },
    "feature_metadata": null,
    "reference_id": "e8922d04-7f80-49fe-a7a8-e40f5c49014f",
    "payment_link": null,
    "profile_id": "pro_W1dQQFAsWElgN0Uk9yjx",
    "surcharge_details": null,
    "attempt_count": 1,
    "merchant_decision": null,
    "merchant_connector_id": "mca_CR12bLCzbHKYSPJtvqzK",
    "incremental_authorization_allowed": null,
    "authorization_count": null,
    "incremental_authorizations": null,
    "external_authentication_details": null,
    "external_3ds_authentication_attempted": false,
    "expires_on": "2024-05-29T11:07:04.202Z",
    "fingerprint": null,
    "browser_info": {
        "language": "en-GB",
        "time_zone": -330,
        "ip_address": "::1",
        "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
        "color_depth": 24,
        "java_enabled": true,
        "screen_width": 2560,
        "accept_header": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "screen_height": 1440,
        "java_script_enabled": true
    },
    "payment_method_id": null,
    "payment_method_status": null,
    "updated": "2024-05-29T10:52:36.717Z",
    "charges": null,
    "frm_metadata": null
}

Create a payment via sdk with capture_method = automatic

{
      currency: "USD",
      amount: 5000,
      order_details: [
        {
          product_name: "Apple iphone 15",
          quantity: 1,
          amount: 5000,
        },
      ],
      confirm: false,
      capture_method: "automatic",
      authentication_type: "three_ds",
      customer_id: "hyperswitch_sdk_demo_id",
      email: "[email protected]",
      description: "Hello this is description",
      connector_metadata: {
        noon: {
          order_category: "applepay",
        },
      },
      metadata: {
        udf1: "value1",
        new_customer: "true",
        login_date: "2019-09-10T10:11:12Z",
      },
      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",
        },
      },
      }

Checklist

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

@swangi-kumari swangi-kumari self-assigned this May 29, 2024
@swangi-kumari swangi-kumari added A-connector-integration Area: Connector integration C-feature Category: Feature request or enhancement labels May 29, 2024
@swangi-kumari swangi-kumari changed the title Klarna capture refunds feat(connector): [Klarna] Add support for Capture, Psync, Refunds and Rsync flows May 29, 2024
@swangi-kumari swangi-kumari marked this pull request as ready for review May 29, 2024 12:11
@swangi-kumari swangi-kumari requested review from a team as code owners May 29, 2024 12:11
SamraatBansal
SamraatBansal previously approved these changes May 29, 2024
Narayanbhat166
Narayanbhat166 previously approved these changes May 29, 2024
crates/router/src/connector/klarna.rs Outdated Show resolved Hide resolved
crates/router/src/connector/klarna.rs Show resolved Hide resolved
@likhinbopanna likhinbopanna enabled auto-merge May 29, 2024 14:17
@likhinbopanna likhinbopanna added this pull request to the merge queue May 29, 2024
Merged via the queue into main with commit e41d5e2 May 29, 2024
10 checks passed
@likhinbopanna likhinbopanna deleted the klarna-capture-refunds branch May 29, 2024 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-connector-integration Area: Connector integration C-feature Category: Feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants