Save details for future payments with New Zealand the related setting Direct Debits
Learn how to save payment method details for future New Zealand bank account debit payments.
You can use the Setup Intents API to collect payment method details in advance, with the final amount or payment date determined later. This is useful for:
- Saving payment methods to a wallet to streamline future purchases
- Collecting surcharges after fulfilling a service
- Starting a free trial for a subscription
Set up Stripe Server-side
First, you need a Stripe account. Register now.
Use our official libraries for access to the Stripe API from your application:
Command Line
Select a language
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
# Available as a gem
sudo gem install stripe
Gemfile
Select a language
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
# If you use bundler, you can add this line to your Gemfile
gem 'stripe'
Create or retrieve a customer Server-side
To reuse a bank account for future payments, attach it to an object that represents your customer.
Use the Accounts v2 API to represent customers
The Accounts v2 API is generally available for Connect users, and in public preview for other Stripe users. If you’re part of the Accounts v2 preview, you need to specify a preview version in your code.
To join the Accounts v2 preview, go to Account previews and features in your Dashboard and enable Reusable payment methods for Global Payouts.
For most use cases, we recommend modeling your customers as customer-configured Account objects instead of using Customer objects.
Create a customer-configured Account or Customer when your customer creates an account with your business, or when saving a payment method. Associate the object’s ID with your own internal representation of a customer.
Create a new customer or retrieve an existing one to associate with this payment.
Command Line
Select a language
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
Create a SetupIntent Server-side
A SetupIntent is an object that represents your intent to set up a customer’s payment method for future payments. The SetupIntent tracks the steps of this set-up process.
Create a SetupIntent on your server. Enable NZ bank account in your Dashboard. Stripe handles the display of eligible payment methods using dynamic payment methods. Specify the ID of the customer-configured Account or Customer.
Command Line
Select a language
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
Retrieve the client secret
The SetupIntent includes a client secret that the client side uses to securely complete the payment process. You can use different approaches to pass the client secret to the client side.
Retrieve the client secret from an endpoint on your server, using the browser’s fetch function. This approach is best if your client side is a single-page application, particularly one built with a modern frontend framework like React. Create the server endpoint that serves the client secret:
main.rb
Select a language
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
get '/secret' do
intent = # ... Create or retrieve the SetupIntent
{client_secret: intent.client_secret}.to_json
end
And then fetch the client secret with JavaScript on the client side:
(async () => {
const response = await fetch('/secret');
const {client_secret: clientSecret} = await response.json();
// Render the form using the clientSecret
})();
Collect payment method details and mandate acknowledgement Client-side
Set up Stripe Elements
Include the Stripe.js script on your checkout page by adding it to the head of your HTML file. Always load Stripe.js directly from js.stripe.com. Don’t include the script in a bundle or host a copy of it yourself.
checkout.html
Create an instance of the Stripe object by providing your publishable API key:
checkout.js
// Set your publishable key: remember to change this to your live publishable key in production
// See your keys here: https://dashboard.stripe.com/apikeys
const stripe = Stripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
Add the Payment Element to your checkout page
On your checkout page, create an empty DOM node with a unique ID for the Payment Element to render into.
checkout.html
<form id="payment-form">
<h3>Payment</h3>
<div id="payment-element"></div>
<button id="submit">Submit</button>
</form>
When the form above finishes loading, create a new Elements group, passing the client secret from the previous step as configuration. You can also pass in the appearance option, customizing the Elements to match the design of your site.
Then, create an instance of the Payment Element and mount it to its corresponding DOM node:
checkout.js
// Customize the appearance of Elements using the Appearance API.
const appearance = { /* ... */ };
// Create an elements group from the Stripe instance, passing the clientSecret (obtained in step 2) and appearance (optional).
const elements = stripe.elements({clientSecret, appearance});
// Create Payment Element instance.
const paymentElement = elements.create("payment");
// Mount the Payment Element to its corresponding DOM node.
paymentElement.mount("#payment-element");
The Payment Element renders a dynamic form that allows your customer to pick a payment method type. The form automatically collects all necessary payments details for the payment method type that they select. For New Zealand the related setting Diret Debit payments, that includes the customer’s name, email address, and bank account number.
Mandate acknowledgement
The Payment Element also displays the New Zealand the related setting Direct Debit Service Terms and Conditions to your customer and collects their agreement with those terms. You’re not required to do anything else.
If you don’t use the Payment Element, you must separately display these terms and conditions to your customer and confirm their acceptance.
Note
By providing your bank account details and confirming this payment, you authorise Stripe New Zealand Limited (authorisation code 3143978), to debit your account with the amounts of direct debits payable to Rocket Rides (“we”, “us” or “Merchant”) in accordance with this authority.
You agree that this authority is subject to:
- your bank’s terms and conditions that relate to your account, and
- the Direct Debit Service Terms and Conditions
You certify that you’re either the sole account holder on the bank account listed above or that you’re an authorised signatory on, and have authority to operate, this bank account severally.
We’ll send you an email confirmation no later than 5 business days after your confirmation of this Direct Debit Authority.
If we request you to do so, you must promptly provide Stripe with a record of the mandates.
Optional Customize the appearance Client-side
Submit the payment method details to Stripe Client-side
Use stripe.confirmSetup to collect bank account details, create a PaymentMethod, and attach that PaymentMethod to the SetupIntent.
For some other payment method types, your customer might be first redirected to an intermediate site, like a bank authorization page, before being redirected to the return_url. Provide a return_url to this function to indicate where Stripe should redirect the customer after they complete the payment.
Since New Zealand the related setting Direct Debits don’t require a redirect, you can also set redirect to if_required in place of providing a return_url. A return_url will only be required if you add another redirect-based payment method later.
script.js
confirmationForm.addEventListener('submit', (ev) => {
ev.preventDefault();
stripe.confirmSetup({elements, redirect: "if_required"})
.then(({setupIntent, error}) => {
if (error) {
console.error(error.message);
// The confirmation failed for some reason.
} else if (setupIntent.status === "requires_payment_method") {
// Confirmation failed. Attempt again with a different payment method.
} else if (setupIntent.status === "succeeded") {
// Confirmation succeeded! The account is now saved.
// Display a message to the customer.
}
});
});
If successful, Stripe returns a SetupIntent object with the status succeeded. The attached PaymentMethod is now ready to be used for future payments.
Customer notification emails
You must send an email confirmation of the mandate and collected bank account details to your customer after successfully confirming the SetupIntent.
In addition, for every payment collected, you must send your customer an email notification of the debit date and amount at latest on the day the debit takes place.
Stripe handles sending these emails for you by default, but you can choose to send custom notifications.
Accepting future payments Server-side
When the SetupIntent succeeds, it creates a new PaymentMethod attached to either a customer-configured Account or a Customer. You can use these to initiate future payments without having to prompt the customer for their bank account a second time.
Command Line
Select a language
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
Test your integration
Test account numbers
In a sandbox, you can use the following parameters to simulate specific errors.
Test your form using bank code 11, branch code 0000, and one of the following account number and suffix combinations.
| Account number | Suffix | Description |
|---|---|---|
0000000 | 010 | PaymentIntents confirmed with the resulting PaymentMethod transition from processing to succeeded. The mandate status remains active. |
2222222 | 027 | PaymentIntents confirmed with the resulting PaymentMethod transition from processing to requires_payment_method with a insufficient_funds failure code. The mandate status remains active. |
8888888 | 000 | PaymentIntents confirmed with the resulting PaymentMethod transition from processing to requires_payment_method with a refer_to_customer failure code. The mandate status remains active. |
1111111 | 016 | PaymentIntents confirmed with the resulting PaymentMethod transition from processing to requires_payment_method with a no_account failure code. The mandate status becomes inactive. |
5555555 | 059 | PaymentIntents confirmed with the resulting PaymentMethod transition from processing to requires_payment_method with a debit_not_authorized failure code. The mandate status becomes inactive. |
9999999 | 000 | PaymentIntents confirmed with the resulting PaymentMethod transition to processing and remain there. To transition it further, use an API request as described below. |
