#Stripe 구독 일정 등록을 통해 여러 단계를 지정하여 연속 계획 구독을 설정할 수 있을까요?

16041 단어 스트라이프
복수의 페이즈를 지정한 경우, 1개의 페이즈의 끝은 반드시 다음 페이즈의 시작이 된다. 즉, 복수의 서브스크립션을 연속적으로 실행할 수 있는 것 같다.

예를 들어, 플랜 A의 서브스크립션이 1회 끝나면, 다음은 플랜 B의 서브스크립션을 2회 반복해, 그 다음은 플랜 B의 서브스크립션을...등.
Stripe::SubscriptionSchedule.create 할 때는 SubscriptionSchedule 본체에 start_date를 지정할 수 있고, 각 단계에는 end_date를 지정할 수 있는 것 같다. 확실히 이 구조로 start_date/end_date 의 경계치가 정해지지 말아라.

각 단계에 반복 횟수 = iterations를 지정하면 자동으로 start_date/end_date도 정해지므로 부족하다.

Doc



If there are multiple phases, the end_date of one phase will always equal the start_date of the next phase. Note that past phases can be omitted.



대시보드






Ruby Code example


# Code

require 'stripe'

Stripe::api_key = ENV['STRIPE_SECRET_KEY']

product = Stripe::Product.create(name: "Gold plan #{rand(9999999999)}")
plan1 = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 1000, product: product.id, usage_type: 'licensed')
plan2 = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 1000, product: product.id, usage_type: 'licensed')
plan3 = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 1000, product: product.id, usage_type: 'licensed')

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_schedule = Stripe::SubscriptionSchedule.create(
  {
    customer: customer.id,
    start_date: Time.now.to_i + 5,
    phases: [
      {
        plans:
          [
            { plan: plan1.id, quantity: 1 },
          ],
          default_payment_method: customer_payment_method.id,
          default_tax_rates: [tax_rate],
          iterations: 1,
      },
      {
        plans:
          [
            { plan: plan2.id, quantity: 1 },
          ],
          default_payment_method: customer_payment_method.id,
          default_tax_rates: [tax_rate],
          iterations: 2,
      },
      {
        plans:
          [
            { plan: plan3.id, quantity: 1 },
          ],
          default_payment_method: customer_payment_method.id,
          default_tax_rates: [tax_rate],
          iterations: 3,
      }
    ],
  }
)

# https://stripe.com/docs/api/subscription_schedules/update
# updated_subscription_schedule = Stripe::SubscriptionSchedule.update(
#   subscription_schedule.id,
#   {
#     phases: [
#       {
#         plans:
#           [
#             { plan: plan.id, quantity: 1 },
#           ],
#           default_payment_method: customer_payment_method.id,
#           default_tax_rates: [tax_rate],
#       },
#     ],
#   }
# )

puts '-' * 100
puts "Subscription Schedule created"
puts "https://dashboard.stripe.com/test/subscription_schedules/#{subscription_schedule.id}"
puts '-' * 100
puts subscription_schedule

----------------------------------------------------------------------------------------------------
Subscription Schedule created
https://dashboard.stripe.com/test/subscription_schedules/sub_sched_1FuY4mCmti5jpytUGlSvoPZt
----------------------------------------------------------------------------------------------------
{
  "id": "sub_sched_1FuY4mCmti5jpytUGlSvoPZt",
  "object": "subscription_schedule",
  "canceled_at": null,
  "completed_at": null,
  "created": 1577511780,
  "current_phase": null,
  "customer": "cus_GRQwFxXeOBODZn",
  "default_settings": {
    "billing_thresholds": null,
    "collection_method": "charge_automatically",
    "default_payment_method": null,
    "default_source": null,
    "invoice_settings": null
  },
  "end_behavior": "release",
  "livemode": false,
  "metadata": {
  },
  "phases": [
    {
      "application_fee_percent": null,
      "billing_thresholds": null,
      "collection_method": null,
      "coupon": null,
      "default_payment_method": "pm_1FuY4lCmti5jpytU0RUFZPn6",
      "default_tax_rates": [
        {
          "id": "txr_1FuY4kCmti5jpytUxADMFwul",
          "object": "tax_rate",
          "active": true,
          "created": 1577511778,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1580190185,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GRQwJ86226UR2S",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": true,
      "start_date": 1577511785,
      "tax_percent": 10.0,
      "trial_end": null
    },
    {
      "application_fee_percent": null,
      "billing_thresholds": null,
      "collection_method": null,
      "coupon": null,
      "default_payment_method": "pm_1FuY4lCmti5jpytU0RUFZPn6",
      "default_tax_rates": [
        {
          "id": "txr_1FuY4kCmti5jpytUxADMFwul",
          "object": "tax_rate",
          "active": true,
          "created": 1577511778,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1585374185,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GRQwEi0AQiyzqG",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": true,
      "start_date": 1580190185,
      "tax_percent": 10.0,
      "trial_end": null
    },
    {
      "application_fee_percent": null,
      "billing_thresholds": null,
      "collection_method": null,
      "coupon": null,
      "default_payment_method": "pm_1FuY4lCmti5jpytU0RUFZPn6",
      "default_tax_rates": [
        {
          "id": "txr_1FuY4kCmti5jpytUxADMFwul",
          "object": "tax_rate",
          "active": true,
          "created": 1577511778,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1593322985,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GRQwsTlfq50PbV",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": true,
      "start_date": 1585374185,
      "tax_percent": 10.0,
      "trial_end": null
    }
  ],
  "released_at": null,
  "released_subscription": null,
  "renewal_interval": null,
  "revision": "sub_sched_rev_1FuY4mCmti5jpytUdkbMi55j",
  "status": "not_started",
  "subscription": null
}

Original by Github issue

좋은 웹페이지 즐겨찾기