Skip to content

Commit

Permalink
Merge branch 'main' of github.com:pay-rails/pay
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Sep 23, 2024
2 parents 8c49d54 + 70a6be1 commit 8ef68e5
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions app/models/pay/stripe/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,28 @@ 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

# 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
Expand Down
15 changes: 0 additions & 15 deletions app/models/pay/stripe/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/stripe/5_webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion docs/stripe/6_metered_billing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions lib/pay/receipts.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,6 +17,10 @@ def receipt_details
]
end

def pdf_product_name
Pay.application_name
end

def pdf_line_items
items = [
[
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8ef68e5

Please sign in to comment.