# Set up future Touch 'n Go payments

Learn how to save Touch 'n Go details and charge your customers later.

> Recurring Touch 'n Go payments need recurring access, which is enabled separately from Touch 'n Go preview access. Without it, requests that use `setup_future_usage` or a SetupIntent return an error. Contact us to ask for recurring access.

You can use the [Setup Intents API](https://docs.stripe.com/payments/setup-intents.md) to collect Touch 'n Go payment method details in advance, and determine the final amount or payment date later.

Alternatively, you can [save payment details during a Touch 'n Go payment](https://docs.stripe.com/payments/touch-n-go/save-during-payment.md) and charge the saved payment method immediately.

First, you need a Stripe account. [Register now](https://dashboard.stripe.com/register).

To access the Stripe API from your application, use our official libraries:

#### Ruby

```bash
# Available as a gem
sudo gem install stripe
```

```ruby
# If you use bundler, you can add this line to your Gemfile
gem 'stripe'
```

If you use a [restricted API key](https://docs.stripe.com/keys/restricted-api-keys.md), give it write permission for the resources in this guide (Checkout Sessions, PaymentIntents, SetupIntents, and PaymentMethods).

## Create and confirm a SetupIntent [Server-side]

A [SetupIntent](https://docs.stripe.com/api/setup_intents.md) is an object that represents your intent and tracks the steps to set up your customer’s payment method for future payments.

Create a `PaymentMethod` with a `type` of `touch_n_go`. Then create and confirm a `SetupIntent` with that payment method and the customer’s ID. Set `usage` to `off_session`. Touch 'n Go doesn’t support `on_session` or single-use SetupIntents.

Pass `mandate_data` to record that the customer authorized you to use their Touch 'n Go account for future payments. You must also specify the `return_url` where we redirect the customer after they complete the setup.

```curl
curl https://api.stripe.com/v1/setup_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d "payment_method_types[]=touch_n_go" \
  -d "payment_method={{PAYMENT_METHOD_ID}}" \
  -d "customer={{CUSTOMER_ID}}" \
  -d usage=off_session \
  -d "mandate_data[customer_acceptance][type]=online" \
  -d "mandate_data[customer_acceptance][online][ip_address]={{CUSTOMER_IP_ADDRESS}}" \
  -d "mandate_data[customer_acceptance][online][user_agent]={{CUSTOMER_USER_AGENT}}" \
  --data-urlencode "return_url=https://example.com/setup/complete" \
  -d confirm=true
```

The SetupIntent has a status of `requires_action` and a `next_action` of type `redirect_to_url`. Redirect the customer to the URL in the `next_action.redirect_to_url.url` property.

The mandate becomes active only after the customer completes the setup and the SetupIntent has a status of `succeeded`. Wait for the `setup_intent.succeeded` webhook event and check that the mandate’s status is `active` before you charge the payment method off session. [Verify the signature of each incoming webhook](https://docs.stripe.com/webhooks.md#verify-events) and check that it comes from a [Stripe IP address](https://docs.stripe.com/ips.md), so you don’t treat an unauthenticated request as proof of payment. An off-session charge fails if the payment method has no active mandate.

## Charge a saved Touch 'n Go payment method [Server-side]

When you’re ready to charge your customer, create and confirm a [PaymentIntent](https://docs.stripe.com/api/payment_intents.md) with the customer’s ID, the *PaymentMethod* (PaymentMethods represent your customer's payment instruments, used with the Payment Intents or Setup Intents APIs) ID, and the amount and currency of the payment. To charge your customer *off session* (A payment is described as off-session if it occurs without the direct involvement of the customer, using previously-collected payment information), set [off_session](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-off_session) to true. That indicates that the customer isn’t in your checkout flow during this payment attempt.

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=100000 \
  -d currency=myr \
  -d "payment_method_types[]=touch_n_go" \
  -d "payment_method={{PAYMENT_METHOD_ID}}" \
  -d "customer={{CUSTOMER_ID}}" \
  -d off_session=true \
  -d confirm=true
```

The `return_url` parameter isn’t required for an off-session payment that uses a saved Touch 'n Go payment method. The Charge’s `payment_method_details.touch_n_go.mandate` attribute contains the ID of the mandate that the payment used.
