#Stripe API で定期支払い=サブスクリプションのトライアル期間のテストをする ( #Ruby )


NOTE

  • 公式にも、すぐ終わるトライアル期間を設定してのテストが勧められている
  • trial_end を数秒後に設定してサブスクリプションを作成する
  • 0円の請求がすぐに発生して、請求成功扱いになるようだ
  • すぐにサブスクリプションの課金サイクルが始まるが、その前に一旦スケジュール登録状態になるみたいだ
  • trial_end と billing_cycle_anchor は全く同じ時刻になるようだ

Code

require 'stripe'

Stripe.api_key = 'sk_test_xxxxxxxxxxx'

product = Stripe::Product.create(name: 'Gold plan')
plan = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 1000, product: product.id)
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)

subscription = Stripe::Subscription::create( customer: customer.id, default_payment_method: customer_payment_method.id, items: [{ plan: plan.id }], default_tax_rates: [tax_rate], trial_end: (Time.now.to_i + 5) )

Result e.g

=> #<Stripe::Subscription:0x3fcd2c9f3128 id=sub_GQKh4eG0ZMm7JQ> JSON: {
  "id": "sub_GQKh4eG0ZMm7JQ",
  "object": "subscription",
  "application_fee_percent": null,
  "billing_cycle_anchor": 1577257942,
  "billing_thresholds": null,
  "cancel_at": null,
  "cancel_at_period_end": false,
  "canceled_at": null,
  "collection_method": "charge_automatically",
  "created": 1577257937,
  "current_period_end": 1577257942,
  "current_period_start": 1577257937,
  "customer": "cus_GQKbTifs2Duhwe",
  "days_until_due": null,
  "default_payment_method": "pm_1FtTwUCmti5jpytUxyBlAksI",
  "default_source": null,
  "default_tax_rates": [
    {"id":"txr_1FtTwTCmti5jpytUnwwi2w3L","object":"tax_rate","active":true,"created":1577257561,"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_GQKhUmcJsQVkTh","object":"subscription_item","billing_thresholds":null,"created":1577257937,"metadata":{},"plan":{"id":"plan_GQKbfgYG7NGBrH","object":"plan","active":true,"aggregate_usage":null,"amount":1000,"amount_decimal":"1000","billing_scheme":"per_unit","created":1577257561,"currency":"jpy","interval":"month","interval_count":1,"livemode":false,"metadata":{},"nickname":null,"product":"prod_GQKbZMaRTjX4Wa","tiers":null,"tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"licensed"},"quantity":1,"subscription":"sub_GQKh4eG0ZMm7JQ","tax_rates":[]}],"has_more":false,"total_count":1,"url":"/v1/subscription_items?subscription=sub_GQKh4eG0ZMm7JQ"},
  "latest_invoice": "in_1FtU2XCmti5jpytUzWEhiZLq",
  "livemode": false,
  "metadata": {},
  "next_pending_invoice_item_invoice": null,
  "pending_invoice_item_interval": null,
  "pending_setup_intent": null,
  "plan": {"id":"plan_GQKbfgYG7NGBrH","object":"plan","active":true,"aggregate_usage":null,"amount":1000,"amount_decimal":"1000","billing_scheme":"per_unit","created":1577257561,"currency":"jpy","interval":"month","interval_count":1,"livemode":false,"metadata":{},"nickname":null,"product":"prod_GQKbZMaRTjX4Wa","tiers":null,"tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"licensed"},
  "quantity": 1,
  "schedule": null,
  "start_date": 1577257937,
  "status": "trialing",
  "tax_percent": 10.0,
  "trial_end": 1577257942,
  "trial_start": 1577257937
}

Dashboard



Ref

Using trial periods on subscriptions | Stripe Billing
https://stripe.com/docs/billing/subscriptions/trials

Original by Github issue