🖥

#Stripe CLI で #API にリクエストして プラン・顧客・デフォルトの支払い方法・定期支払を作成する手順

2019/12/07に公開
  • プラン と product を同時に作成する
  • 顧客を作成する
  • 支払い方法を作成する - クレジットカード
  • 支払い方法を顧客に紐付ける
  • 定期支払いを作成して顧客と、その顧客のデフォルトの支払い方法を紐づける

Script

# source this-script.sh
# or
# bash this-script.sh

customer=$(stripe customers create)
echo "$customer" | jq

customer_id=$(echo "$customer" | jq -r '.id')
echo "$customer_id"

# Create Plan and Product in both time
# https://stripe.com/docs/api/plans/create?lang=curl
# https://stripe.com/docs/billing/subscriptions/products-and-plans
plan=$(stripe plans create --interval='month' --currency='jpy' --amount='1000' --data="product[name]"="Gold special")
echo "$plan" | jq

plan_id=$(echo "$plan" | jq -r .id)

# Create payment method as credit card
# https://stripe.com/docs/api/payment_methods/create
payment_method=$(stripe payment_methods create --type="card" --data "card[number]"=4242424242424242 --data "card[exp_month]"=12 --data "card[exp_year]"=2020 --data "card[cvc]"=314)
echo "$payment_method" | jq
payment_method_id=$(echo "$payment_method" | jq -r .id)

# Attach payment method to customer
# https://stripe.com/docs/api/payment_methods/attach
stripe payment_methods attach "$payment_method_id" --data "customer=$customer_id"

# Set default payment method to customer
# https://stripe.com/docs/api/customers/update#update_customer-invoice_settings-default_payment_method

# Create subscription for customer
# https://stripe.com/docs/api/subscriptions/create
stripe subscriptions create --customer="$customer_id" --default-payment-method="$payment_method_id" --data="items[0][plan]"="$plan_id"

open https://dashboard.stripe.com/test/customers/"$customer_id"

webhook example

$ stripe listen
> Ready! Your webhook signing secret is whsec_xxxxxxxxxxxxxxxx (^C to quit)

2019-12-06 19:13:30   --> customer.created [evt_1FmdoQCmti5jpytUn94dirAr]
2019-12-06 19:13:30   --> plan.created [evt_1FmdoRCmti5jpytUdR5JqzfN]
2019-12-06 19:13:30   --> payment_method.attached [evt_1FmdoTCmti5jpytUq04MEPAt]
2019-12-06 19:13:31   --> charge.succeeded [evt_1FmdoVCmti5jpytUy2GGcFVW]
2019-12-06 19:13:31   --> payment_intent.succeeded [evt_1FmdoVCmti5jpytUvUdGNate]
2019-12-06 19:13:31   --> customer.updated [evt_1FmdoVCmti5jpytU52592eIo]
2019-12-06 19:13:31   --> invoice.created [evt_1FmdoVCmti5jpytUb6eoi9a2]
2019-12-06 19:13:31   --> payment_intent.created [evt_1FmdoVCmti5jpytUkdOvJCTo]
2019-12-06 19:13:31   --> invoice.payment_succeeded [evt_1FmdoVCmti5jpytUehyLKpcl]
2019-12-06 19:13:31   --> invoice.finalized [evt_1FmdoVCmti5jpytU8l0ELzz5]
2019-12-06 19:13:33   --> customer.subscription.created [evt_1FmdoVCmti5jpytU64GBo2Ss]

Run script result example


bundle rspec spec/api/v1/resources/sessions_api_spec.rb
bundle exec rspec spec/api/v1/resources/sessions_api_spec.rb
bundle exec rspec spec/api/v1/resources/staffs_api_spec.rb
bundle exec rspec spec/api/v1/resources/clients/accounts_api_spec.rb:375
bundle exec rspec spec/api/v1/resources/clients/accounts_api_spec.rb:385
bundle exec rspec spec/api/v1/resources/clients/accounts_api_spec.rb
source ~/.rc
source ~/.zshrc && ruby -v
source /tmp/.env
gem install nokogiri -v '1.10.4' --source 'https://rubygems.org/'
curl https://api.stripe.com/v1/charges -u sk_test_Er0l3tHJ2xia4k1HbfVWQ3Ek00kKQd4nOp: -d amount=1000 -d currency=usd -d source=tok_visa -d rece
source ~/.zshrc
stripe resources help
source ~/tmp/stripe.sh
QUERY> source                                                                                                             IgnoreCase [71 (1/3)]
$ source ~/tmp/stripe.sh
{
  "id": "cus_GJG6JjNjHlId6R",
  "object": "customer",
  "address": null,
  "balance": 0,
  "created": 1575626315,
  "currency": null,
  "default_source": null,
  "delinquent": false,
  "description": null,
  "discount": null,
  "email": null,
  "invoice_prefix": "89A1AD55",
  "invoice_settings": {
    "custom_fields": null,
    "default_payment_method": null,
    "footer": null
  },
  "livemode": false,
  "metadata": {},
  "name": null,
  "phone": null,
  "preferred_locales": [],
  "shipping": null,
  "sources": {
    "object": "list",
    "data": [],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_GJG6JjNjHlId6R/sources"
  },
  "subscriptions": {
    "object": "list",
    "data": [],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_GJG6JjNjHlId6R/subscriptions"
  },
  "tax_exempt": "none",
  "tax_ids": {
    "object": "list",
    "data": [],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_GJG6JjNjHlId6R/tax_ids"
  },
  "tax_info": null,
  "tax_info_verification": null
}
cus_GJG6JjNjHlId6R
{
  "id": "plan_GJG6tibrhabw5v",
  "object": "plan",
  "active": true,
  "aggregate_usage": null,
  "amount": 1000,
  "amount_decimal": "1000",
  "billing_scheme": "per_unit",
  "created": 1575626315,
  "currency": "jpy",
  "interval": "month",
  "interval_count": 1,
  "livemode": false,
  "metadata": {},
  "nickname": null,
  "product": "prod_GJG6bC2xdFsxuh",
  "tiers": null,
  "tiers_mode": null,
  "transform_usage": null,
  "trial_period_days": null,
  "usage_type": "licensed"
}
{
  "id": "pm_1Fmda4Cmti5jpytUNMXA6RXF",
  "object": "payment_method",
  "billing_details": {
    "address": {
      "city": null,
      "country": null,
      "line1": null,
      "line2": null,
      "postal_code": null,
      "state": null
    },
    "email": null,
    "name": null,
    "phone": null
  },
  "card": {
    "brand": "visa",
    "checks": {
      "address_line1_check": null,
      "address_postal_code_check": null,
      "cvc_check": "unchecked"
    },
    "country": "US",
    "exp_month": 12,
    "exp_year": 2020,
    "fingerprint": "3Dnj9E30BUfyFTcl",
    "funding": "credit",
    "generated_from": null,
    "last4": "4242",
    "three_d_secure_usage": {
      "supported": true
    },
    "wallet": null
  },
  "created": 1575626316,
  "customer": null,
  "livemode": false,
  "metadata": {},
  "type": "card"
}
{
  "id": "pm_1Fmda4Cmti5jpytUNMXA6RXF",
  "object": "payment_method",
  "billing_details": {
    "address": {
      "city": null,
      "country": null,
      "line1": null,
      "line2": null,
      "postal_code": null,
      "state": null
    },
    "email": null,
    "name": null,
    "phone": null
  },
  "card": {
    "brand": "visa",
    "checks": {
      "address_line1_check": null,
      "address_postal_code_check": null,
      "cvc_check": "pass"
    },
    "country": "US",
    "exp_month": 12,
    "exp_year": 2020,
    "fingerprint": "3Dnj9E30BUfyFTcl",
    "funding": "credit",
    "generated_from": null,
    "last4": "4242",
    "three_d_secure_usage": {
      "supported": true
    },
    "wallet": null
  },
  "created": 1575626316,
  "customer": "cus_GJG6JjNjHlId6R",
  "livemode": false,
  "metadata": {
  },
  "type": "card"
}
{
  "id": "sub_GJG6wZwWaLAlg8",
  "object": "subscription",
  "application_fee_percent": null,
  "billing_cycle_anchor": 1575626318,
  "billing_thresholds": null,
  "cancel_at": null,
  "cancel_at_period_end": false,
  "canceled_at": null,
  "collection_method": "charge_automatically",
  "created": 1575626318,
  "current_period_end": 1578304718,
  "current_period_start": 1575626318,
  "customer": "cus_GJG6JjNjHlId6R",
  "days_until_due": null,
  "default_payment_method": "pm_1Fmda4Cmti5jpytUNMXA6RXF",
  "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_GJG6tKpDK4diBR",
        "object": "subscription_item",
        "billing_thresholds": null,
        "created": 1575626318,
        "metadata": {
        },
        "plan": {
          "id": "plan_GJG6tibrhabw5v",
          "object": "plan",
          "active": true,
          "aggregate_usage": null,
          "amount": 1000,
          "amount_decimal": "1000",
          "billing_scheme": "per_unit",
          "created": 1575626315,
          "currency": "jpy",
          "interval": "month",
          "interval_count": 1,
          "livemode": false,
          "metadata": {
          },
          "nickname": null,
          "product": "prod_GJG6bC2xdFsxuh",
          "tiers": null,
          "tiers_mode": null,
          "transform_usage": null,
          "trial_period_days": null,
          "usage_type": "licensed"
        },
        "quantity": 1,
        "subscription": "sub_GJG6wZwWaLAlg8",
        "tax_rates": [

        ]
      }
    ],
    "has_more": false,
    "total_count": 1,
    "url": "/v1/subscription_items?subscription=sub_GJG6wZwWaLAlg8"
  },
  "latest_invoice": "in_1Fmda6Cmti5jpytU9mmp4ath",
  "livemode": false,
  "metadata": {
  },
  "next_pending_invoice_item_invoice": null,
  "pending_invoice_item_interval": null,
  "pending_setup_intent": null,
  "plan": {
    "id": "plan_GJG6tibrhabw5v",
    "object": "plan",
    "active": true,
    "aggregate_usage": null,
    "amount": 1000,
    "amount_decimal": "1000",
    "billing_scheme": "per_unit",
    "created": 1575626315,
    "currency": "jpy",
    "interval": "month",
    "interval_count": 1,
    "livemode": false,
    "metadata": {
    },
    "nickname": null,
    "product": "prod_GJG6bC2xdFsxuh",
    "tiers": null,
    "tiers_mode": null,
    "transform_usage": null,
    "trial_period_days": null,
    "usage_type": "licensed"
  },
  "quantity": 1,
  "schedule": null,
  "start_date": 1575626318,
  "status": "active",
  "tax_percent": null,
  "trial_end": null,
  "trial_start": null
}
$

Dashboard image

image
image

Original by Github issue

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

チャットメンバー募集

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

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

Twitter

https://twitter.com/YumaInaura

公開日時

2019-12-07

Discussion