Payment Methods
Storing Payment Methods
In order to create subscriptions or perform "one-off" charges with Stripe, you will need to store a payment method and retrieve its identifier from Stripe. The approach used to accomplish this differs based on whether you plan to use the payment method for subscriptions or single charges, so we will examine both below.
Payment Methods for Subscriptions
When storing a customer's credit card information for future use by a subscription, the Stripe "Setup Intents" API must be used to securely gather the customer's payment method details. A "Setup Intent" indicates to Stripe the intention to charge a customer's payment method. Shopkeeper's Billable
trait includes the createSetupIntent
method to easily create a new Setup Intent. You should invoke this method from the route or controller that will render the form which gathers your customer's payment method details:
After you have created the Setup Intent and passed it to the view, you should attach its secret to the element that will gather the payment method. For example, consider this "update payment method" form:
Next, the Stripe.js library may be used to attach a Stripe Element to the form and securely gather the customer's payment details:
Next, the card can be verified and a secure "payment method identifier" can be retrieved from Stripe using Stripe's confirmCardSetup
method:
After the card has been verified by Stripe, you may pass the resulting setupIntent.payment_method
identifier to your Adonis application, where it can be attached to the customer. The payment method can either be added as a new payment method or used to update the default payment method. You can also immediately use the payment method identifier to create a new subscription.
If you would like more information about Setup Intents and gathering customer payment details please review this overview provided by Stripe.
Payment Methods for Single Charges
Of course, when making a single charge against a customer's payment method, we will only need to use a payment method identifier once. Due to Stripe limitations, you may not use the stored default payment method of a customer for single charges. You must allow the customer to enter their payment method details using the Stripe.js library. For example, consider the following form:
After defining such a form, the Stripe.js library may be used to attach a Stripe Element to the form and securely gather the customer's payment details:
Next, the card can be verified and a secure "payment method identifier" can be retrieved from Stripe using Stripe's createPaymentMethod
method:
If the card is verified successfully, you may pass the paymentMethod.id
to your Adonis application and process a single charge.
Retrieving Payment Methods
The paymentMethods
method on the billable model instance returns a list of Shopkeeper's PaymentMethod
instances:
By default, this method will return payment methods of every type. To retrieve payment methods of a specific type, you may pass the type
as an argument to the method:
To retrieve the customer's default payment method, the defaultPaymentMethod
method may be used:
You can retrieve a specific payment method that is attached to the billable model using the findPaymentMethod
method:
Payment Method Presence
To determine if a billable model has a default payment method attached to their account, invoke the hasDefaultPaymentMethod
method:
You may use the hasPaymentMethod
method to determine if a billable model has at least one payment method attached to their account:
This method will determine if the billable model has any payment method at all. To determine if a payment method of a specific type exists for the model, you may pass the type
as an argument to the method:
Updating the Default Payment Method
The updateDefaultPaymentMethod
method may be used to update a customer's default payment method information. This method accepts a Stripe payment method identifier and will assign the new payment method as the default billing payment method:
To sync your default payment method information with the customer's default payment method information in Stripe, you may use the updateDefaultPaymentMethodFromStripe
method:
The default payment method on a customer can only be used for invoicing and creating new subscriptions. Due to limitations imposed by Stripe, it may not be used for single charges.
Adding Payment Methods
To add a new payment method, you may call the addPaymentMethod
method on the billable model, passing the payment method identifier:
To learn how to retrieve payment method identifiers please review the payment method storage documentation.
Deleting Payment Methods
To delete a payment method, you may call the delete
method on the Shopkeeper's PaymentMethod
instance you wish to delete:
The deletePaymentMethod
method will delete a specific payment method from the billable model:
The deletePaymentMethods
method will delete all of the payment method information for the billable model:
By default, this method will delete payment methods of every type. To delete payment methods of a specific type you can pass the type
as an argument to the method:
If a user has an active subscription, your application should not allow them to delete their default payment method.