The first thing you'll need to do is add a payment method. You will get a payment method token from the payment processor's Javascript library. See the payment processors docs for how to setup their Javascript.
With Pay, you can simply assign the payment method token before making a charge or subscription.
Pay will attempt to save an API request by creating a Customer and assigning PaymentMethod in the same request if a payment_method_token
is set.
@user.payment_processor.payment_method_token = params[:payment_method_token]
@user.payment_processor.charge(15_00)
The payment method will be set as the default, so future payments do not need to collect a payment method.
If a customer would like to purchase and provide a new payment method, you may still assign a payment_method_token
before purchasing.
To update the default payment method on file, you can use update_payment_method
:
@user.payment_processor.update_payment_method(params[:payment_method_token])
This will add the payment method via the API and mark it as the default for future payments.
For updating payment method details on Paddle, a transaction ID is required. This can be generated by using
subscription = @user.payment_processor.subscription(name: "plan name")
transaction = subscription.payment_method_transaction
Once you have a transaction ID, you can then pass that through to Paddle.js like so
<a href="#"
class="paddle_button"
data-display-mode="overlay"
data-theme="light"
data-locale="en"
data-transaction-id="<%= transaction.id %>"
>
Update Payment Details
</a>
This will then open the Paddle overlay and allow the user to update their payment details.
For more information, see the Paddle documentation
Paddle uses an Update Payment Details URL for each customer which allows them to update the payment method. This is stored on the Pay::Subscription
record for easy access.
@user.payment_processor.subscription.paddle_update_url
You may either redirect to this URL or use Paddle's Javascript to render as an overlay or inline.
You can also add a payment method without making it the default.
@user.payment_processor.add_payment_method(params[:payment_method_token], default: false)
See Charges