🖥

#Stripe API test with Ruby : create subscription schedule

2019/12/26に公開

Docs

https://stripe.com/docs/api/subscription_schedules/create

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)

Stripe::SubscriptionSchedule.create({ customer: customer.id, start_date: Time.now.to_i + 60, end_behavior: 'release', phases: [ { plans: [ {plan: plan.id, quantity: 1}, ], iterations: 12, }, ], })

Result

=> #<Stripe::SubscriptionSchedule:0x3fca5a4a43cc id=sub_sched_1FtVQkCmti5jpytUJWcwX3WH> JSON: {
  "id": "sub_sched_1FtVQkCmti5jpytUJWcwX3WH",
  "object": "subscription_schedule",
  "canceled_at": null,
  "completed_at": null,
  "created": 1577263282,
  "current_phase": null,
  "customer": "cus_GQM3ndcDYp63EB",
  "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":null,"default_tax_rates":[],"end_date":1608885741,"invoice_settings":null,"plans":[{"billing_thresholds":null,"plan":"plan_GQM3zPmxrr9VVB","quantity":1,"tax_rates":[]}],"prorate":true,"start_date":1577263341,"tax_percent":null,"trial_end":null}
  ],
  "released_at": null,
  "released_subscription": null,
  "renewal_interval": null,
  "revision": "sub_sched_rev_1FtVQkCmti5jpytUlFLxUreq",
  "status": "not_started",
  "subscription": null
}

Dashboard

image
image

Theres no events in subscription schedule

image

Subscription started

image

events happens

customer.subscription.created
invoice.created
subscription_schedule.updated

image

customer.subscription.created

{
  "object": {
    "id": "sub_GQMCpU1L9T6QEE",
    "object": "subscription",
    "application_fee_percent": null,
    "billing_cycle_anchor": 1577263462,
    "billing_thresholds": null,
    "cancel_at": 1608885862,
    "cancel_at_period_end": false,
    "canceled_at": 1577263462,
    "collection_method": "charge_automatically",
    "created": 1577263462,
    "current_period_end": 1579941862,
    "current_period_start": 1577263462,
    "customer": "cus_GQM3ndcDYp63EB",
    "days_until_due": null,
    "default_payment_method": null,
    "default_source": null,
    "default_tax_rates": [
    ],
    "discount": null,
    "ended_at": null,
    "invoice_customer_balance_settings": {
      "consume_applied_balance_on_void": true
    },
    "items": {
      "object": "list",
      "data": [
        {
          "id": "si_GQMCk8XuwSdc18",
          "object": "subscription_item",
          "billing_thresholds": null,
          "created": 1577263492,
          "metadata": {
          },
          "plan": {
            "id": "plan_GQM3zPmxrr9VVB",
            "object": "plan",
            "active": true,
            "aggregate_usage": null,
            "amount": 1000,
            "amount_decimal": "1000",
            "billing_scheme": "per_unit",
            "created": 1577262970,
            "currency": "jpy",
            "interval": "month",
            "interval_count": 1,
            "livemode": false,
            "metadata": {
            },
            "nickname": null,
            "product": "prod_GQM38XBWggK2Xc",
            "tiers": null,
            "tiers_mode": null,
            "transform_usage": null,
            "trial_period_days": null,
            "usage_type": "licensed"
          },
          "quantity": 1,
          "subscription": "sub_GQMCpU1L9T6QEE",
          "tax_rates": [
          ]
        }
      ],
      "has_more": false,
      "total_count": 1,
      "url": "/v1/subscription_items?subscription=sub_GQMCpU1L9T6QEE"
    },
    "latest_invoice": "in_1FtVU8Cmti5jpytUOah7vCvu",
    "livemode": false,
    "metadata": {
    },
    "next_pending_invoice_item_invoice": null,
    "pending_invoice_item_interval": null,
    "pending_setup_intent": null,
    "plan": {
      "id": "plan_GQM3zPmxrr9VVB",
      "object": "plan",
      "active": true,
      "aggregate_usage": null,
      "amount": 1000,
      "amount_decimal": "1000",
      "billing_scheme": "per_unit",
      "created": 1577262970,
      "currency": "jpy",
      "interval": "month",
      "interval_count": 1,
      "livemode": false,
      "metadata": {
      },
      "nickname": null,
      "product": "prod_GQM38XBWggK2Xc",
      "tiers": null,
      "tiers_mode": null,
      "transform_usage": null,
      "trial_period_days": null,
      "usage_type": "licensed"
    },
    "quantity": 1,
    "schedule": "sub_sched_1FtVShCmti5jpytUvDBkLdgX",
    "start_date": 1577263462,
    "status": "active",
    "tax_percent": null,
    "trial_end": null,
    "trial_start": null
  }
}

invoice.created

{
  "object": {
    "id": "in_1FtVU8Cmti5jpytUOah7vCvu",
    "object": "invoice",
    "account_country": "JP",
    "account_name": "yumainaura",
    "amount_due": 1000,
    "amount_paid": 0,
    "amount_remaining": 1000,
    "application_fee_amount": null,
    "attempt_count": 0,
    "attempted": false,
    "auto_advance": true,
    "billing_reason": "subscription_create",
    "charge": null,
    "collection_method": "charge_automatically",
    "created": 1577263492,
    "currency": "jpy",
    "custom_fields": null,
    "customer": "cus_GQM3ndcDYp63EB",
    "customer_address": null,
    "customer_email": null,
    "customer_name": null,
    "customer_phone": null,
    "customer_shipping": null,
    "customer_tax_exempt": "none",
    "customer_tax_ids": [
    ],
    "default_payment_method": null,
    "default_source": null,
    "default_tax_rates": [
    ],
    "description": null,
    "discount": null,
    "due_date": null,
    "ending_balance": null,
    "footer": null,
    "hosted_invoice_url": null,
    "invoice_pdf": null,
    "lines": {
      "object": "list",
      "data": [
        {
          "id": "sli_212ba72a420391",
          "object": "line_item",
          "amount": 1000,
          "currency": "jpy",
          "description": "1 × Gold plan (at ¥1,000 / month)",
          "discountable": true,
          "livemode": false,
          "metadata": {
          },
          "period": {
            "end": 1579941862,
            "start": 1577263462
          },
          "plan": {
            "id": "plan_GQM3zPmxrr9VVB",
            "object": "plan",
            "active": true,
            "aggregate_usage": null,
            "amount": 1000,
            "amount_decimal": "1000",
            "billing_scheme": "per_unit",
            "created": 1577262970,
            "currency": "jpy",
            "interval": "month",
            "interval_count": 1,
            "livemode": false,
            "metadata": {
            },
            "nickname": null,
            "product": "prod_GQM38XBWggK2Xc",
            "tiers": null,
            "tiers_mode": null,
            "transform_usage": null,
            "trial_period_days": null,
            "usage_type": "licensed"
          },
          "proration": false,
          "quantity": 1,
          "subscription": "sub_GQMCpU1L9T6QEE",
          "subscription_item": "si_GQMCk8XuwSdc18",
          "tax_amounts": [
          ],
          "tax_rates": [
          ],
          "type": "subscription",
          "unique_id": "il_1FtVU8Cmti5jpytUu5tA2q7N"
        }
      ],
      "has_more": false,
      "total_count": 1,
      "url": "/v1/invoices/in_1FtVU8Cmti5jpytUOah7vCvu/lines"
    },
    "livemode": false,
    "metadata": {
    },
    "next_payment_attempt": 1577267092,
    "number": "4B942154-0002",
    "paid": false,
    "payment_intent": null,
    "period_end": 1577263462,
    "period_start": 1577263462,
    "post_payment_credit_notes_amount": 0,
    "pre_payment_credit_notes_amount": 0,
    "receipt_number": null,
    "starting_balance": 0,
    "statement_descriptor": null,
    "status": "draft",
    "status_transitions": {
      "finalized_at": null,
      "marked_uncollectible_at": null,
      "paid_at": null,
      "voided_at": null
    },
    "subscription": "sub_GQMCpU1L9T6QEE",
    "subtotal": 1000,
    "tax": null,
    "tax_percent": null,
    "total": 1000,
    "total_tax_amounts": [
    ],
    "webhooks_delivered_at": null
  }
}

subscription_schedule.updated

{
  "object": {
    "id": "sub_sched_1FtVShCmti5jpytUvDBkLdgX",
    "object": "subscription_schedule",
    "canceled_at": null,
    "completed_at": null,
    "created": 1577263403,
    "current_phase": {
      "end_date": 1608885862,
      "start_date": 1577263462
    },
    "customer": "cus_GQM3ndcDYp63EB",
    "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": null,
        "default_tax_rates": [
        ],
        "end_date": 1608885862,
        "invoice_settings": null,
        "plans": [
          {
            "billing_thresholds": null,
            "plan": "plan_GQM3zPmxrr9VVB",
            "quantity": 1,
            "tax_rates": [
            ]
          }
        ],
        "prorate": true,
        "start_date": 1577263462,
        "tax_percent": null,
        "trial_end": null
      }
    ],
    "released_at": null,
    "released_subscription": null,
    "renewal_interval": null,
    "revision": "sub_sched_rev_1FtVShCmti5jpytUKyprUFFa",
    "status": "active",
    "subscription": "sub_GQMCpU1L9T6QEE"
  },
  "previous_attributes": {
    "current_phase": null,
    "status": "not_started",
    "subscription": null
  }
}

JApanese title

Ruby でSTripe API を叩いて定期支払いのスケジュール登録をする

Original by Github issue

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

チャットメンバー募集

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

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

Twitter

https://twitter.com/YumaInaura

公開日時

2019-12-26

Discussion