Skip to content

Commit 8ef68e5

Browse files
committed
Merge branch 'main' of github.com:pay-rails/pay
2 parents 8c49d54 + 70a6be1 commit 8ef68e5

File tree

7 files changed

+29
-23
lines changed

7 files changed

+29
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Add `Pay.sync(params)` for automatically syncing Stripe Checkout Sessions and Paddle Billing transactions.
1515
* Lock Pay::Customer record when creating or updating Stripe customer to handle race conditions. #1027
1616
* LemonSqueezy & Paddle Billing will now find Customers by email since they do not allow duplicates. #1043
17+
* Add `Pay::Stripe::Customer#customer_session` for creating pricing tables, etc
1718

1819
### 7.3.0
1920

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ GEM
181181
psych (5.1.2)
182182
stringio
183183
public_suffix (6.0.1)
184-
puma (6.4.2)
184+
puma (6.4.3)
185185
nio4r (~> 2.0)
186186
racc (1.8.1)
187187
rack (3.1.7)

app/models/pay/stripe/customer.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,28 @@ def billing_portal(**options)
232232
::Stripe::BillingPortal::Session.create(args.merge(options), stripe_options)
233233
end
234234

235+
def customer_session(**options)
236+
api_record unless processor_id?
237+
args = {customer: processor_id}
238+
::Stripe::CustomerSession.create(args.merge(options), stripe_options)
239+
end
240+
235241
def authorize(amount, options = {})
236242
charge(amount, options.merge(capture_method: :manual))
237243
end
238244

245+
# Creates a meter event to bill for usage
246+
#
247+
# create_meter_event(:api_request, value: 1)
248+
# create_meter_event(:api_request, token: 7)
249+
def create_meter_event(event_name, payload: {}, **options)
250+
api_record unless processor_id?
251+
::Stripe::Billing::MeterEvent.create({
252+
event_name: event_name,
253+
payload: {stripe_customer_id: processor_id}.merge(payload)
254+
}.merge(options))
255+
end
256+
239257
private
240258

241259
# Options for Stripe requests

app/models/pay/stripe/subscription.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,21 +301,6 @@ def swap(plan, **options)
301301
raise Pay::Stripe::Error, e
302302
end
303303

304-
# Creates a meter event to bill for usage
305-
#
306-
# create_meter_event(:api_request, value: 1)
307-
# create_meter_event(:api_request, token: 7)
308-
def create_meter_event(event_name, **payload)
309-
api_record unless processor_id?
310-
::Stripe::Billing::MeterEvent.create({
311-
event_name: event_name,
312-
payload: {
313-
stripe_customer_id: processor_id,
314-
**payload
315-
}
316-
})
317-
end
318-
319304
# Creates a metered billing usage record
320305
#
321306
# Uses the first subscription_item ID unless `subscription_item_id: "si_1234"` is passed

docs/stripe/5_webhooks.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ checkout.session.completed
5454
checkout.session.async_payment_succeeded
5555
```
5656

57+
[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.
58+
5759
## Next
5860

5961
See [Metered Billing](6_metered_billing.md)

docs/stripe/6_metered_billing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Metered billing are subscriptions where the price fluctuates monthly. For exampl
99
This will create a new metered billing subscription. You can then create meter events to bill for usage:
1010

1111
```ruby
12-
pay_subscription.create_meter_event(:api_request, value: 1)
12+
@user.payment_processor.create_meter_event(:api_request, payload: { value: 1 })
1313
```
1414

1515
If your price is using the legacy usage records system, you will need to use the below method:

lib/pay/receipts.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
module Pay
22
module Receipts
3-
def product
4-
Pay.application_name
5-
end
6-
73
def receipt_filename
84
"receipt-#{created_at.strftime("%Y-%m-%d")}.pdf"
95
end
@@ -21,6 +17,10 @@ def receipt_details
2117
]
2218
end
2319

20+
def pdf_product_name
21+
Pay.application_name
22+
end
23+
2424
def pdf_line_items
2525
items = [
2626
[
@@ -45,7 +45,7 @@ def pdf_line_items
4545
end
4646
end
4747
else
48-
items << [product, 1, Pay::Currency.format(amount, currency: currency), Pay::Currency.format(amount, currency: currency)]
48+
items << [pdf_product_name, 1, Pay::Currency.format(amount, currency: currency), Pay::Currency.format(amount, currency: currency)]
4949
end
5050

5151
# If no subtotal, we will display the total
@@ -155,7 +155,7 @@ def invoice_pdf(**options)
155155
company: {
156156
name: Pay.business_name,
157157
address: Pay.business_address,
158-
email: Pay.support_email.address,
158+
email: Pay.support_email&.address,
159159
logo: Pay.business_logo
160160
},
161161
line_items: pdf_line_items

0 commit comments

Comments
 (0)