From c76c03fa6bda9f90793986082030ef7b052d12c5 Mon Sep 17 00:00:00 2001 From: Ayush Newatia Date: Thu, 12 Sep 2024 15:33:15 +0100 Subject: [PATCH 1/8] Change meter event to use the customer's processor ID (#1064) * Change meter event to use the customer's processor ID * Update docs --- app/models/pay/stripe/customer.rb | 12 ++++++++++++ app/models/pay/stripe/subscription.rb | 15 --------------- docs/stripe/6_metered_billing.md | 2 +- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/app/models/pay/stripe/customer.rb b/app/models/pay/stripe/customer.rb index c714233f..62b03727 100644 --- a/app/models/pay/stripe/customer.rb +++ b/app/models/pay/stripe/customer.rb @@ -236,6 +236,18 @@ def authorize(amount, options = {}) charge(amount, options.merge(capture_method: :manual)) end + # Creates a meter event to bill for usage + # + # create_meter_event(:api_request, value: 1) + # create_meter_event(:api_request, token: 7) + def create_meter_event(event_name, payload: {}, **options) + api_record unless processor_id? + ::Stripe::Billing::MeterEvent.create({ + event_name: event_name, + payload: {stripe_customer_id: processor_id}.merge(payload) + }.merge(options)) + end + private # Options for Stripe requests diff --git a/app/models/pay/stripe/subscription.rb b/app/models/pay/stripe/subscription.rb index 75899a67..e89a935e 100644 --- a/app/models/pay/stripe/subscription.rb +++ b/app/models/pay/stripe/subscription.rb @@ -301,21 +301,6 @@ def swap(plan, **options) raise Pay::Stripe::Error, e end - # Creates a meter event to bill for usage - # - # create_meter_event(:api_request, value: 1) - # create_meter_event(:api_request, token: 7) - def create_meter_event(event_name, **payload) - api_record unless processor_id? - ::Stripe::Billing::MeterEvent.create({ - event_name: event_name, - payload: { - stripe_customer_id: processor_id, - **payload - } - }) - end - # Creates a metered billing usage record # # Uses the first subscription_item ID unless `subscription_item_id: "si_1234"` is passed diff --git a/docs/stripe/6_metered_billing.md b/docs/stripe/6_metered_billing.md index b995e6c4..41e67f9b 100644 --- a/docs/stripe/6_metered_billing.md +++ b/docs/stripe/6_metered_billing.md @@ -9,7 +9,7 @@ Metered billing are subscriptions where the price fluctuates monthly. For exampl This will create a new metered billing subscription. You can then create meter events to bill for usage: ```ruby -pay_subscription.create_meter_event(:api_request, value: 1) +@user.payment_processor.create_meter_event(:api_request, payload: { value: 1 }) ``` If your price is using the legacy usage records system, you will need to use the below method: From b6bc868707ffa19eb51795b067e305ce84f484e4 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Fri, 13 Sep 2024 13:31:17 -0500 Subject: [PATCH 2/8] Handle String or Mail::Address for support_email in invoices --- lib/pay/receipts.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pay/receipts.rb b/lib/pay/receipts.rb index 09ceb87a..6aa6e9a3 100644 --- a/lib/pay/receipts.rb +++ b/lib/pay/receipts.rb @@ -155,7 +155,7 @@ def invoice_pdf(**options) company: { name: Pay.business_name, address: Pay.business_address, - email: Pay.support_email.address, + email: Pay.support_email&.address, logo: Pay.business_logo }, line_items: pdf_line_items From 9c7ba256e04e1a0eae8c30366490da9b36ce8a2a Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Fri, 13 Sep 2024 16:40:00 -0500 Subject: [PATCH 3/8] Rename product method for receipts to prevent conflicts --- lib/pay/receipts.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/pay/receipts.rb b/lib/pay/receipts.rb index 6aa6e9a3..2b11c6b6 100644 --- a/lib/pay/receipts.rb +++ b/lib/pay/receipts.rb @@ -1,9 +1,5 @@ module Pay module Receipts - def product - Pay.application_name - end - def receipt_filename "receipt-#{created_at.strftime("%Y-%m-%d")}.pdf" end @@ -21,6 +17,10 @@ def receipt_details ] end + def pdf_product_name + Pay.application_name + end + def pdf_line_items items = [ [ @@ -45,7 +45,7 @@ def pdf_line_items end end else - items << [product, 1, Pay::Currency.format(amount, currency: currency), Pay::Currency.format(amount, currency: currency)] + items << [pdf_product_name, 1, Pay::Currency.format(amount, currency: currency), Pay::Currency.format(amount, currency: currency)] end # If no subtotal, we will display the total From f44a72a88711856b5854b5bb17f5cf44d4e2695c Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Mon, 16 Sep 2024 12:22:47 -0500 Subject: [PATCH 4/8] Add Stripe customer_session helper --- app/models/pay/stripe/customer.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/models/pay/stripe/customer.rb b/app/models/pay/stripe/customer.rb index 62b03727..e7b1d9b5 100644 --- a/app/models/pay/stripe/customer.rb +++ b/app/models/pay/stripe/customer.rb @@ -232,6 +232,12 @@ def billing_portal(**options) ::Stripe::BillingPortal::Session.create(args.merge(options), stripe_options) end + def customer_session(**options) + api_record unless processor_id? + args = { customer: processor_id } + ::Stripe::CustomerSession.create(args.merge(options), stripe_options) + end + def authorize(amount, options = {}) charge(amount, options.merge(capture_method: :manual)) end From 7dc3a95f076094488a44ef865a75d605233a5ca6 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Mon, 16 Sep 2024 12:31:14 -0500 Subject: [PATCH 5/8] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37119913..22a10468 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Add `Pay.sync(params)` for automatically syncing Stripe Checkout Sessions and Paddle Billing transactions. * Lock Pay::Customer record when creating or updating Stripe customer to handle race conditions. #1027 * LemonSqueezy & Paddle Billing will now find Customers by email since they do not allow duplicates. #1043 +* Add `Pay::Stripe::Customer#customer_session` for creating pricing tables, etc ### 7.3.0 From 1d7eae0cc4a302b4594b06ee4b2bc4e3c46549af Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Mon, 16 Sep 2024 12:32:36 -0500 Subject: [PATCH 6/8] Standardize --- app/models/pay/stripe/customer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/pay/stripe/customer.rb b/app/models/pay/stripe/customer.rb index e7b1d9b5..74981207 100644 --- a/app/models/pay/stripe/customer.rb +++ b/app/models/pay/stripe/customer.rb @@ -234,7 +234,7 @@ def billing_portal(**options) def customer_session(**options) api_record unless processor_id? - args = { customer: processor_id } + args = {customer: processor_id} ::Stripe::CustomerSession.create(args.merge(options), stripe_options) end From 88625ed0d317d008d9321e2dab916515b0d8b727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20K=C3=B6hlbrugge?= Date: Mon, 16 Sep 2024 19:14:51 +0100 Subject: [PATCH 7/8] Add link to create webhook with all the required events (#1065) --- docs/stripe/5_webhooks.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/stripe/5_webhooks.md b/docs/stripe/5_webhooks.md index 8fdfb9ce..789f2dd5 100644 --- a/docs/stripe/5_webhooks.md +++ b/docs/stripe/5_webhooks.md @@ -54,6 +54,8 @@ checkout.session.completed checkout.session.async_payment_succeeded ``` +[Click here](https://dashboard.stripe.com/webhooks/create?events=charge.succeeded%2Ccharge.refunded%2Cpayment_intent.succeeded%2Cinvoice.upcoming%2Cinvoice.payment_action_required%2Ccustomer.subscription.created%2Ccustomer.subscription.updated%2Ccustomer.subscription.deleted%2Ccustomer.subscription.trial_will_end%2Ccustomer.updated%2Ccustomer.deleted%2Cpayment_method.attached%2Cpayment_method.updated%2Cpayment_method.automatically_updated%2Cpayment_method.detached%2Caccount.updated%2Ccheckout.session.completed%2Ccheckout.session.async_payment_succeeded) to create a new Stripe webhook with all the events pre-filled. + ## Next See [Metered Billing](6_metered_billing.md) From 70a6be12abe2e065998a9d41f195e6eea9799459 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 20 Sep 2024 10:10:18 -0500 Subject: [PATCH 8/8] Bump puma from 6.4.2 to 6.4.3 (#1067) Bumps [puma](https://github.com/puma/puma) from 6.4.2 to 6.4.3. - [Release notes](https://github.com/puma/puma/releases) - [Changelog](https://github.com/puma/puma/blob/master/History.md) - [Commits](https://github.com/puma/puma/compare/v6.4.2...v6.4.3) --- updated-dependencies: - dependency-name: puma dependency-type: direct:production ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 31ad4a46..7789530b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -181,7 +181,7 @@ GEM psych (5.1.2) stringio public_suffix (6.0.1) - puma (6.4.2) + puma (6.4.3) nio4r (~> 2.0) racc (1.8.1) rack (3.1.7)