# Save payment method during payment with Drop-in

# Supported payment methods

| Payment method  | Country / Region |
| :-------------- | :--------------- |
| Card            | Global           |
| DANA            | Indonesia        |
| Boost           | Malaysia         |
| Touch'n Go      | Malaysia         |
| Alipay          | China            |
| AlipayHK        | Hong Kong        |
| GCash           | Philippines      |
| KakaoPay        | South Korea      |
| Rabbit LINE Pay | Thailand         |
| TrueMoney       | Thailand         |

# **Learn how to save payment details during a payment.**

Unlike creating a SetupIntent to save payment details, you also have the option to save the payment method during the payment process. In the future, you can  complete payments using the customer and payment method ID.

1. Direct API
2. JS SDK

# Direct API

## 1.[Create a Customer](api-55528906)

*Request*

```JSON
{
    "email":"test@gmail.com"
}
```

*Response*

```JSON
{
    "id": "cus_1706288169156083712",
    "object": "customer",
    "created": 1695645775000,
    "livemode": false,
    "address": {},
    "email": "test@gmail.com"
}
```

## 2. [Create the PaymentIntent](api-42631218)

Below is an example using `alipay` as the payment method.

*Request*

```JSON
{
    "amount": 100,
    "currency": "USD",
    "confirm": true,
    "customer": "cus_1760950869144109056",
    "payment_method_data":{
        "type":"alipay"
    },
    "payment_method_options":{
        "alipay":{
            "terminal_type":"WEB"
        }
    },
    "setup_future_usage": "off_session",
    "return_url": "https://XXXXXXX"
}
```

*Response*

```JSON
{
    "id": "pi_1762686851044343808",
    "object": "payment_intent",
    "created": 1709092269000,
    "livemode": false,
    "currency": "USD",
    "amount": 100,
    "status": "requires_action",
    "customer": "cus_1760950869144109056",
    "client_secret": "pi_1762686851044343808_secret_RSWWKd4j8HqTUZNGgvCYEFoF",
    "next_action": {
        "type": "alipay_handle_redirect",
        "alipay_handle_redirect": {
            "url": "https://XXXXXXXXXXXXXXXXXXXXx"
        }
    },
    "payment_method_types": [
        "alipay"
    ],
    "confirmation_method": "automatic",
    "payment_method_options": {
        "alipay": {
            "terminal_type": "WEB"
        }
    },
    "return_url": "https://XXXXXXX",
    "payment_method": "pm_1762686850952069120",
    "capture_method": "automatic",
    "setup_future_usage": "off_session"
}
```

Customer will be redirected to the payment method's page to complete the payment and bind the account

You can store the Customer ID along with the PM_id, or find the customer's saved payment method using the following method.

## **3. Retrieve the payment method attached to the customer**

To find a payment method to charge, list the payment methods associated with the customers using List a Customer's PaymentMethods.

*Request*

```JSON
{"type":"alipay"}
```

## **4. Charge the saved payment method later**

Using the Customer and PaymentMethod ID to create a payment intent.

When you already have the Customer and Paymentmethod IDs, create PaymentIntent with the amount and currency of the payment.

Before that, there are some parameters in PaymentIntent you need to know.

1. `confirm`: If your set it to `true`, payment will be confirmed immediately when the paymentintent is created.
2. Set `payment_method` to the ID of the PaymentMethod and `Customer` to the ID of the Customer.

### [Create a PaymentIntent](api-42631218)

*Request*

```JSON
{
    "amount": 100,
    "currency": "CNY",
    "confirm": true,
    "customer": "cus_1742882448670195712",
    "payment_method" : "pm_1743112678097813504",
    "return_url": "https://XXXXXXX"
}
```

*Response*

```JSON
{
    "id": "pi_1762769251795468288",
    "object": "payment_intent",
    "created": 1709111915000,
    "livemode": false,
    "currency": "CNY",
    "amount": 100,
    "status": "succeeded",
    "customer": "cus_1742882448670195712",
    "client_secret": "pi_1762769251795468288_secret_gYnWXf7uwcBYpJKbkc8tvl90",
    "payment_method_types": [
        "alipay"
    ],
    "confirmation_method": "automatic",
    "return_url": "https://XXXXXXX",
    "payment_method": "pm_1743112678097813504",
    "amount_received": 100,
    "capture_method": "automatic"
}
```

# JS SDK

## 1.[Create a Customer](api-55528906)

*Request*

```JSON
{
    "email":"test@gmail.com"
}
```

*Response*

```JSON
{
    "id": "cus_1706288169156083712",
    "object": "customer",
    "created": 1695645775000,
    "livemode": false,
    "address": {},
    "email": "test@gmail.com"
}
```

## 2. [Create the PaymentIntent](api-42631218)
Below is an example using `card` as the payment method.

*Request*

```JSON
{
    "amount": 1000,
    "currency": "USD",
    "customer":"cus_1706288169156083712",
    "setup_future_usage":"on_session",
    "merchant_order_id": "Order112344343",
    "return_url": "https://wooshpay.com"
}
```

*Response*

```JSON
{
    "id": "pi_1706504504247058432",
    "object": "payment_intent",
    "created": 1695697354000,
    "livemode": false,
    "currency": "USD",
    "amount": 1000,
    "status": "requires_payment_method",
    "customer": "cus_1706288169156083712",
    "merchant_order_id": "Order112344343",
    "client_secret": "pi_1706504504247058432_secret_0qE9OXnjgZ3XjZJJXjq67ye5",
    "payment_method_types": [
        "card"
    ],
    "confirmation_method": "automatic",
    "return_url": "https://wooshpay.com",
    "capture_method": "automatic",
    "setup_future_usage": "on_session"
}
```

**Retrieve the client secret**

Included in the returned PaymentIntent is a client secret, which the client side uses to securely complete the payment process instead of passing the entire PaymentIntent object.

## 3. Collect payment details

Pass the client secret from the previous step into options when you create the Element instance

```HTML
<form id="payment-app">
  <div>
    <!--Wooshpay Will Inject the Payment Element At Here-->
  </div>
  <div class="submit-app">
    <button id="submit-btn">Pay</button>
    <div id="payment-err"></div>
      <!-- Display error message to your customers here -->
  </div>
</form>

<script src="https://js.wooshpay.com/v1/wooshpay.js"></script>
```


```JavaScript
  // Get your Dom
  const app = document.getElementById("payment-app");
  // Initialize Wooshpay
  const wooshpay = Wooshpay("pk_test_xxxx" /* publishable key */, {
    locale: "en",
  });
  const options = {
    clientSecret: "{{CLIENT_SECRET}}",
    // Fully customizable with appearance API.
    appearance: {
      /*...*/
    },
  };
  // Set up Wooshpay.js and Elements using the SetupIntent's client secret
  const elements = wooshpay.elements(options);
  // Create and mount the Payment Element
  elements.create("payment", {
    billingDetails: "auto", // billingDetails:'auto'
  });
  elements.mount(app);
```

## 4. Submit the payment details to Wooshpay

You can complete the setup using Wooshpay.confirmSetup. Provide a return_url

in this function so that customer can be redirected after they compelet setup.

```JavaScript
  const {error} = await wooshpay.confirmPayment({
    //`Elements` instance that was used to create the Payment Element
    elements,
    confirmParams: {
      return_url: 'https://XXXXXXXX',
    }
  });
```

## 5. Retrieve the payment method attached to the customer

To find a  payment method to charge, list the payment methods associated with the customers using [List a Customer's PaymentMethods](api-112153316). 

*Request* 

```JSON
{
    "type":"card"
}
```

## 6. Charge the saved payment method later 

Using the Customer and PaymentMethod ID to create a payment intent. 

When you already have the Customer and Paymentmethod IDs, create PaymentIntent with the amount and currency of the payment. 

Before that, there are some parameters in PaymentIntent you need to know.


1. `confirm`: If your set it to `true`, payment will be confirmed immediately when the paymentintent is created.
2. Set `payment_method` to the ID of the PaymentMethod and `Customer` to the ID of the Customer.

### [Create a PaymentIntent](api-42631218)
*Request* 

```JSON
{
    "amount": 1000,
    "currency": "USD",
    "confirm": true,
    "off_session":false,
    "customer":"cus_1706288169156083712",
    "payment_method":"pm_1704478135803707392",
    "return_url": "https://wooshpay.com"
}
```

*Response*

```JSON
{
    "id": "pi_1704478320244031488",
    "object": "payment_intent",
    "created": 1695214274000,
    "livemode": false,
    "currency": "USD",
    "amount": 1000,
    "status": "succeeded",
    "customer": "cus_1704388704790904832",
    "client_secret": "pi_1704478320244031488_secret_wqlE0XivPRU1d3zkAaJUir52",
    "payment_method_types": [
        "card"
    ],
    "confirmation_method": "automatic",
    "payment_method_options": {
        "card": {
            "request_three_d_secure": "auto",
            "setup_future_usage": "off_session"
        }
    },
    "return_url": "https://wooshpay.com",
    "payment_method": "pm_1704478135803707392",
    "amount_received": 1000,
    "capture_method": "automatic",
    "latest_charge": "ch_1704478320336306176"
}
```
