Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change meter event to use the customer's processor ID #1064

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/models/pay/stripe/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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: 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
Loading