#bojack420
1 messages · Page 1 of 1 (latest)
Yes you can still use PaymentIntents API with source
so the source tokens work with payment intents without needing to change payment intent code at all?
Can you share with me your code?
Umm sure, there is a fair bit to it though.
ill send it in individual sections
so im creating payment intent with this:
` def create_payment_intent()
logger.debug "Initiate Apple Pay"
product_id = params[:product_id]
logger.debug "product id #{product_id}"
@product = Product.find_by(id: params[:product_id])
logger.debug "product #{@product}"
@product_quantity = params[:quantity].to_i
@subtotal = @product.regular_price * @product_quantity
@total = @product.regular_price * @product_quantity
@total_in_cents = (@total * 100).to_i
@total_gst = @total - (@total / 1.1)
stripe_customer_id = params[:stripe_customer_id]
logger.debug "stripe_customer_id #{stripe_customer_id}}"
logger.debug "amount #{@total_in_cents}"
logger.debug "customer #{@current_user}"
logger.debug "2 customer #{current_user}"
@payment_intent = create_stripe_payment_intent({
amount: @total_in_cents,
currency: 'aud',
customer: stripe_customer_id,
description: "Intent for order",
setup_future_usage: 'off_session',
automatic_payment_methods: {
enabled: true,
allow_redirects: 'never'
}
})
`
it might be abit too much code to send in snippets.
Is there an easy way i can firstly just check if it works?
as we are pretty much only using the source tokens for payment renwals
As our old customer base used source tokens, so it would just be used for renwals, cancellations and updating details
I don't see you specify source anywhere in the code that you provided earlier.
yeah i dont have source tokens implemented anywhere. As this new system we built currently just uses payment intents. But when we are migrating our database from the old site that uses source tokens we need a way to read both the use of new payment intents but to use old source tokens to renew, cancel and update card details
so the code i provided is actually not relevant
currently for renewals we do # Attempt to charge the customer using the stored payment method stripe_payment_intent = charge_customer(subscription, subscription.product.regular_price, last_order, new_order) # Retrieve the Charge object using the latest_charge attribute from the PaymentIntent charge_id = stripe_payment_intent.latest_charge charge = Stripe::Charge.retrieve(charge_id)
####################################################
` def self.charge_customer(subscription, amount, last_order, new_order)
# Get the Stripe customer ID from the user associated with the subscription
stripe_customer_id = subscription.user.stripe_customer_id
# Get the Stripe payment method ID from the last successful order
payment_method_id = last_order.stripe_pm_id
# Create a PaymentIntent using the Stripe customer ID, payment method ID, and amount
stripe_payment_intent = Stripe::PaymentIntent.create(
amount: (amount * 100).to_i, # Convert to cents
currency: 'aud', # Set the appropriate currency
customer: stripe_customer_id,
payment_method: payment_method_id,
off_session: true, # Indicates that the payment is not happening during an active customer session
confirm: true, # Automatically confirm the PaymentIntent
description: "Order number: #{new_order.format_order_number} (renewal)",
)
# Create a new order note with the Stripe PaymentIntent detail
stripe_payment_intent_detail = "Stripe payment intent created (Payment Intent ID: #{stripe_payment_intent.id})"
OrderNote.create_private_note(
order_id: new_order.id,
content: stripe_payment_intent_detail,
)
# Ensure the PaymentIntent is in the 'succeeded' state
if stripe_payment_intent.status != 'succeeded'
raise "Failed to process the payment for subscription #{subscription.id}"
end
stripe_payment_intent
end`
This is how we currently process renwals with payment intents
Hmm, I don't quite get it, why are you manually creating the recurring payments if you are already using subscriptions?
because we are using our own subscription system and are just using stripe for the payments for it.
payment_method_id = last_order.stripe_pm_id, is this payment_method_id a reusable source?
Yeah so we save the last payment method that the user used aka for the first payment and then charge them from that payment method when the renew
If it this is a reusable source, then yes you can pass it as a payment_method when creating a PaymentIntent.
im not all too familar with source tokens. Does the source token also output a payment ID?
and im guessing the stripe customer id's and stuff are all the same still?
Is there a easy way to test it without making a section that creates source tokens and payments for those source tokens?
No a source object's ID starts with src_
Yes you are right, you can test it out in test mode
Yeah im in test mode, can i create a fake payment using source tokens in the stripe dashboard?
You might find it easier to do it programmatically.