#Stripe API + #Ruby example code / Create subscription with plan usage_type "metered"

```rb
require 'stripe'

Stripe::api_key = ENV['STRIPE_SECRET_KEY']

product = Stripe::Product.create(name: "Gold plan #{rand(9999999999)}")

plan = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 1000, product: product.id, usage_type: 'metered')
# => #<Stripe::Plan:0x3fbfb88a3b64 id=plan_GRPV5x40cSOljT> JSON: {
#   "id": "plan_GRPV5x40cSOljT",
#   "object": "plan",
#   "active": true,
#   "aggregate_usage": null,
#   "amount": 1000,
#   "amount_decimal": "1000",
#   "billing_scheme": "per_unit",
#   "created": 1577506432,
#   "currency": "jpy",
#   "interval": "month",
#   "interval_count": 1,
#   "livemode": false,
#   "metadata": {},
#   "nickname": null,
#   "product": "prod_GRPVXv1nO5xifF",
#   "tiers": null,
#   "tiers_mode": null,
#   "transform_usage": null,
#   "trial_period_days": null,
#   "usage_type": "metered"
# }

tax_rate = Stripe::TaxRate.create(display_name: 'Tax Rate', percentage: 10.0, inclusive: false)
customer = Stripe::Customer.create
payment_method = Stripe::PaymentMethod.create(type: 'card', card: { number: '4242424242424242', exp_year: 2030, exp_month: 01})
customer_payment_method = Stripe::PaymentMethod.attach(payment_method.id, customer: customer.id)

# https://stripe.com/docs/api/subscription_schedules/create
# https://stripe.com/docs/api/subscriptions/create
subscription = Stripe::Subscription::create(
                 customer: customer.id,
                 default_payment_method: customer_payment_method.id,
                 items: [
                   { plan: plan.id }
                 ],
                 default_tax_rates: [tax_rate],
               )


# => #<Stripe::Subscription:0x3fbfb8d47134 id=sub_GRPbOzEMvVZBaR> JSON: {
#   "id": "sub_GRPbOzEMvVZBaR",
#   "object": "subscription",
#   "application_fee_percent": null,
#   "billing_cycle_anchor": 1577506762,
#   "billing_thresholds": null,
#   "cancel_at": null,
#   "cancel_at_period_end": false,
#   "canceled_at": null,
#   "collection_method": "charge_automatically",
#   "created": 1577506762,
#   "current_period_end": 1580185162,
#   "current_period_start": 1577506762,
#   "customer": "cus_GRPV2QzvTCpEFq",
#   "days_until_due": null,
#   "default_payment_method": "pm_1FuWgfCmti5jpytU5DhNn84y",
#   "default_source": null,
#   "default_tax_rates": [
#     {"id":"txr_1FuWgeCmti5jpytUOMnRqJo8","object":"tax_rate","active":true,"created":1577506440,"description":null,"display_name":"Tax Rate","inclusive":false,"jurisdiction":null,"livemode":false,"metadata":{},"percentage":10.0}
#   ],
#   "discount": null,
#   "ended_at": null,
#   "invoice_customer_balance_settings": {"consume_applied_balance_on_void":true},
#   "items": {"object":"list","data":[{"id":"si_GRPbF035CQdhfS","object":"subscription_item","billing_thresholds":null,"created":1577506763,"metadata":{},"plan":{"id":"plan_GRPV5x40cSOljT","object":"plan","active":true,"aggregate_usage":null,"amount":1000,"amount_decimal":"1000","billing_scheme":"per_unit","created":1577506432,"currency":"jpy","interval":"month","interval_count":1,"livemode":false,"metadata":{},"nickname":null,"product":"prod_GRPVXv1nO5xifF","tiers":null,"tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"metered"},"subscription":"sub_GRPbOzEMvVZBaR","tax_rates":[]}],"has_more":false,"total_count":1,"url":"/v1/subscription_items?subscription=sub_GRPbOzEMvVZBaR"},
#   "latest_invoice": "in_1FuWlqCmti5jpytUNDtHyVHR",
#   "livemode": false,
#   "metadata": {},
#   "next_pending_invoice_item_invoice": null,
#   "pending_invoice_item_interval": null,
#   "pending_setup_intent": null,
#   "plan": {"id":"plan_GRPV5x40cSOljT","object":"plan","active":true,"aggregate_usage":null,"amount":1000,"amount_decimal":"1000","billing_scheme":"per_unit","created":1577506432,"currency":"jpy","interval":"month","interval_count":1,"livemode":false,"metadata":{},"nickname":null,"product":"prod_GRPVXv1nO5xifF","tiers":null,"tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"metered"},
#   "quantity": 1,
#   "schedule": null,
#   "start_date": 1577506762,
#   "status": "active",
#   "tax_percent": 10.0,
#   "trial_end": null,
#   "trial_start": null
# }
```

従量課金のサブスクリプションを作成する例

# Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2887








<!-- Update From Qiita API -->

# チャットメンバー募集


何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->