{"id":9051,"date":"2023-12-04T10:50:54","date_gmt":"2023-12-04T10:50:54","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9051"},"modified":"2023-12-04T10:50:54","modified_gmt":"2023-12-04T10:50:54","slug":"send-email-to-user-after-purchase-on-stripe-with-ruby-on-rails","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/send-email-to-user-after-purchase-on-stripe-with-ruby-on-rails","title":{"rendered":"How to Send Email to User After Purchase on STRIPE with Ruby \u200bon Rails"},"content":{"rendered":"<p><strong>Set up a Stripe webhook:<\/strong><\/p>\n<ul>\n<li aria-level=\"2\">Log in to your Stripe dashboard.<\/li>\n<li aria-level=\"2\">Navigate to &#8220;Developers&#8221; &gt; &#8220;Webhooks&#8221; in the left sidebar.<\/li>\n<li aria-level=\"2\">Click the &#8220;Add endpoint&#8221; button to create a new webhook endpoint.<\/li>\n<li aria-level=\"2\">Enter the URL of your Rails application where you&#8217;ll receive Stripe webhook events (e.g., https:\/\/your-app.com\/webhooks\/stripe).<\/li>\n<li aria-level=\"2\">Select the events you want to listen to, such as &#8220;checkout.session.completed&#8221; for successful payments.<\/li>\n<li aria-level=\"2\">Save the webhook endpoint.<\/li>\n<\/ul>\n<p>In the app\/controllers\/webhooks_controller.rb file, create an action to handle the webhook events:<\/p>\n<p><strong>To send email to user after purchase:<\/strong><\/p>\n<p>1. In the webhooks controller, you can send an email to the user after a checkout is completed. To obtain the user&#8217;s email address for sending the email, you can retrieve it from the event object.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class WebhooksController &lt; ApplicationController\r\n  skip_before_action :verify_authenticity_token\r\n  def create\r\n    payload = request.body.read\r\n    sig_header = request.env['HTTP_STRIPE_SIGNATURE']\r\n    event = nil\r\n    begin\r\n      event = Stripe::Webhook.construct_event(\r\n      payload, sig_header,Rails.application.credentials[:stripe][:webhook]\r\n     )\r\n    rescue JSON::ParserError =&gt; e\r\n      status 400\r\n      return\r\n    rescue Stripe::SignatureVerificationError =&gt; e\r\n      # Invalid signature\r\n      puts \"Signature error\"\r\n      return\r\n    end\r\n    # Handle the event\r\n    case event.type\r\n    when 'checkout.session.completed'\r\n      session = event.data.object\r\n      session_with_expand = Stripe::Checkout::Session.retrieve({id:     session.id, expand: [\"line_items\"] })\r\n      session_with_expand.line_items.data.each do |line_item|\r\n      product = Product.find_by(stripe_product_id: line_item.price.product)\r\n      product.increment!(:sales_count)\r\n    end\r\n      #Send email to user after successful payment\r\n      user_id = event.data.object.client_reference_id\r\n      User = User.find_by(id: user_id)\r\n      if user\r\n        UserMailer.payment_success_email(user).deliver_now\r\n      end\r\n    end\r\n    render json: { message: 'success' }\r\n  end\r\nend<\/pre>\n<p>2. If you are creating the payment intent with the Ruby SDK you can add the receipt_email field to add a customers email.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Stripe.api_key = 'API_KEY'\r\n\r\n\r\nintent = Stripe::PaymentIntent.create({\r\n  amount: 1099,\r\n  currency: 'cad',\r\n  payment_method_types: ['card'],\r\n  receipt_email: 'recipient_email@example.com',\r\n})<\/pre>\n<p>3. If you are creating the checkout session server side you can preconfigure the properties\/settings like for the checkout page.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">require 'stripe'\r\nStripe.api_key ='API_KEY'\r\n\r\nStripe::Checkout::Session.create({\r\n  success_url: 'https:\/\/example.com\/success',\r\n  cancel_url: 'https:\/\/example.com\/cancel',\r\n  customer_email:'email',\r\n  line_items: [\r\n    {price: 'price_H5ggYwtDq4fbrJ', quantity: 2},\r\n  ],\r\n  mode: 'payment',\r\n})\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Set up a Stripe webhook: Log in to your Stripe dashboard. Navigate to &#8220;Developers&#8221; &gt; &#8220;Webhooks&#8221; in the left sidebar. Click the &#8220;Add endpoint&#8221; button to create a new webhook endpoint. Enter the URL of your Rails application where you&#8217;ll receive Stripe webhook events (e.g., https:\/\/your-app.com\/webhooks\/stripe). Select the events you want to listen to, such [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9052,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-9051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9051"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9051"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9051\/revisions"}],"predecessor-version":[{"id":9054,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9051\/revisions\/9054"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9052"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}