🖥

#Stripe API + #Ruby gem で定期支払いの請求を作成する例 ( 顧客作成・デフォルトの支払い方法を登録・プランの作成・税

2019/12/21に公開

Code

No need to payment intent, payment source attache.

require 'stripe'

Stripe.api_key = ENV['STRIPE_SECRET_KEY']

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]
              )

customer = Stripe::Customer.retrieve(customer.id)
subscription_invoice = Stripe::Invoice.retrieve(customer.subscriptions.data[0].latest_invoice)

p subscription_invoice

Result

$ STRIPE_SECRET_KEY=sk_test_xxxxxxxxxxxxxxxxxxxxxx pry ~/tmp/stripe.rb
#<Stripe::Invoice:0x3fc7e35d01ac id=in_1FrZnyCmti5jpytUqUqqcjVX> JSON: {
  "id": "in_1FrZnyCmti5jpytUqUqqcjVX",
  "object": "invoice",
  "account_country": "JP",
  "account_name": "yumainaura",
  "amount_due": 1100,
  "amount_paid": 1100,
  "amount_remaining": 0,
  "application_fee_amount": null,
  "attempt_count": 1,
  "attempted": true,
  "auto_advance": false,
  "billing_reason": "subscription_create",
  "charge": "ch_1FrZnyCmti5jpytUQ9PMH3z1",
  "collection_method": "charge_automatically",
  "created": 1576803442,
  "currency": "jpy",
  "custom_fields": null,
  "customer": "cus_GOMXfIqzJKSF12",
  "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": [
    {"id":"txr_1FrZnwCmti5jpytUlv7jF2HT","object":"tax_rate","active":true,"created":1576803440,"description":null,"display_name":"Tax Rate","inclusive":false,"jurisdiction":null,"livemode":false,"metadata":{},"percentage":10.0}
  ],
  "description": null,
  "discount": null,
  "due_date": null,
  "ending_balance": 0,
  "footer": null,
  "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_uCN38P2xjLu5iSsXHOU6LCozKT",
  "invoice_pdf": "https://pay.stripe.com/invoice/invst_uCN38P2xjLu5iSsXHOU6LCozKT/pdf",
  "lines": {"object":"list","data":[{"id":"sli_fe1f1f1bb0aa55","object":"line_item","amount":1000,"currency":"jpy","description":"1 × Gold plan (at ¥1,000 / month)","discountable":true,"livemode":false,"metadata":{},"period":{"end":1579481842,"start":1576803442},"plan":{"id":"plan_GOMXJzgxs31NbS","object":"plan","active":true,"aggregate_usage":null,"amount":1000,"amount_decimal":"1000","billing_scheme":"per_unit","created":1576803440,"currency":"jpy","interval":"month","interval_count":1,"livemode":false,"metadata":{},"nickname":null,"product":"prod_GOMWB1WfirJ9E5","tiers":null,"tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"licensed"},"proration":false,"quantity":1,"subscription":"sub_GOMXwLV5Qyelwu","subscription_item":"si_GOMXiUNouc5U5J","tax_amounts":[{"amount":100,"inclusive":false,"tax_rate":"txr_1FrZnwCmti5jpytUlv7jF2HT"}],"tax_rates":[],"type":"subscription","unique_id":"il_1FrZnyCmti5jpytUt0gmfTrU"}],"has_more":false,"total_count":1,"url":"/v1/invoices/in_1FrZnyCmti5jpytUqUqqcjVX/lines"},
  "livemode": false,
  "metadata": {},
  "next_payment_attempt": null,
  "number": "4506ADA5-0001",
  "paid": true,
  "payment_intent": "pi_1FrZnyCmti5jpytUPbn6rlhH",
  "period_end": 1576803442,
  "period_start": 1576803442,
  "post_payment_credit_notes_amount": 0,
  "pre_payment_credit_notes_amount": 0,
  "receipt_number": null,
  "starting_balance": 0,
  "statement_descriptor": null,
  "status": "paid",
  "status_transitions": {"finalized_at":1576803442,"marked_uncollectible_at":null,"paid_at":1576803443,"voided_at":null},
  "subscription": "sub_GOMXwLV5Qyelwu",
  "subtotal": 1000,
  "tax": 100,
  "tax_percent": 10.0,
  "total": 1100,
  "total_tax_amounts": [
    {"amount":100,"inclusive":false,"tax_rate":"txr_1FrZnwCmti5jpytUlv7jF2HT"}
  ],
  "webhooks_delivered_at": null
}

Original by Github issue

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

チャットメンバー募集

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

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

Twitter

https://twitter.com/YumaInaura

公開日時

2019-12-21

Discussion