0

I would like to know how to send an email to the user after successful checkout

My Webhook controller:

class WebhooksController < ApplicationController
  skip_before_action :verify_authenticity_token
  
  def create
    payload = request.body.read
    sig_header = request.env['HTTP_STRIPE_SIGNATURE']
    event = nil
  
    begin
      event = Stripe::Webhook.construct_event(
        payload, sig_header, Rails.application.credentials[:stripe][:webhook]
      )
    rescue JSON::ParserError => e
      status 400
      return
    rescue Stripe::SignatureVerificationError => e
      # Invalid signature
      puts "Signature error"
      return
    end
  
    # Handle the event
    case event.type
    when 'checkout.session.completed'
      session = event.data.object
      session_with_expand = Stripe::Checkout::Session.retrieve({id: session.id, expand: ["line_items"] })  
      session_with_expand.line_items.data.each do |line_item| 
      product = Product.find_by(stripe_product_id: line_item.price.product)
      product.increment!(:sales_count)

      end
    end
  
    render json: { message: 'success' }
  end
end
Yurii Stefaniuk
  • 1,594
  • 2
  • 11
  • 16
StrikerGs
  • 3
  • 1

1 Answers1

0

If you are creating the payment intent with the Ruby SDK you can add the receipt_email field to add a customers email.(see ther Stripe PaymentIntent docs)

Stripe.api_key = 'API_KEY'

intent = Stripe::PaymentIntent.create({
  amount: 1099,
  currency: 'cad',
  payment_method_types: ['card'],
  receipt_email: 'jenny.rosen@example.com',
})

If you are creating the checkout session server side you can preconfigure the properties/settings like for the checkout page per the Stripe Checkout Docs

require 'stripe'
Stripe.api_key ='API_KEY'

Stripe::Checkout::Session.create({
  success_url: 'https://example.com/success',
  cancel_url: 'https://example.com/cancel',
  customer_email:'email',
  line_items: [
    {price: 'price_H5ggYwtDq4fbrJ', quantity: 2},
  ],
  mode: 'payment',
})

This is the note on the customer email field per Stripe docs :

If provided, this value will be used when the Customer object is created. If not provided, customers will be asked to enter their email address. Use this parameter to prefill customer data if you already have an email on file. To access information about the customer once a session is complete, use the customer field.

richetechguy
  • 38
  • 1
  • 5
  • Thanks for the answer, i would like to know if there is a way to send using the action mailer, because I think the API only sends if it is in production, i'm using test data – StrikerGs Jun 19 '22 at 14:20
  • Yes that's correct it will only work in production mode as Stripe won't send emails in test mode. This thread talks about actionmailer with subscriptions/invoices as an example https://stackoverflow.com/questions/20063401/stripe-event-gem-and-action-mailer-for-invoice-webhook – richetechguy Jun 19 '22 at 19:44
  • Thaankss my friend – StrikerGs Jun 20 '22 at 23:19