LoginSignup
6
3

More than 3 years have passed since last update.

【Rails】Stripeの実装(カード情報保存版)

Last updated at Posted at 2019-10-01

環境

ドキュメント(Stripe公式)

設定

KEYの設定(環境変数)

こちらを参照。

initializersの設定

こちらを参照。

コンソールでテスト

テスト結果は、stripeのダッシュボードで確認可能。
※ユーザ固有のキーを使っている場合のみ。
※ダッシュボードで 「テストデータを表示」 をチェックすること。

リクエスト例

## customer の作成
customer = Stripe::Customer.create({
    source: 'tok_mastercard',
    email: 'paying.user@example.com',
})

## customer にチャージする (カードではなく)
charge = Stripe::Charge.create({
    amount: 1000,
    currency: 'jpy',
    customer: customer.id,
})

## customer ID (customer.id) や、その他の情報をデータベースに保存する。

## 再度、customer にチャージする際に、↑で保存した customer ID を使う。以下の customer_id はデータベースに保存していたもの。
charge = Stripe::Charge.create({
    amount: 1500,
    currency: 'jpy',
    customer: customer_id,
})

レスポンス例(↑の例ではcustomerに入るもの)

> customer.class
=> Stripe::Customer < Stripe::APIResource

> customer.id
=> "cus_FuW98aPiB42PLY"

> customer
=> {
                       :id => "cus_FuW98aPiB42PLY",
                   :object => "customer",
          :account_balance => 0,
                  :address => nil,
                  :balance => 0,
                  :created => 1569920002,
                 :currency => nil,
           :default_source => "card_1FOh6k2eZvKYlo2C86LDl8BE",
               :delinquent => false,
              :description => nil,
                 :discount => nil,
                    :email => "paying.user@example.com",
           :invoice_prefix => "032DE63F",
         :invoice_settings => {
                 :custom_fields => nil,
        :default_payment_method => nil,
                        :footer => nil
    },
                 :livemode => false,
                 :metadata => {},
                     :name => nil,
                    :phone => nil,
        :preferred_locales => [],
                 :shipping => nil,
                  :sources => {
             :object => "list",
               :data => [
            [0] {
                                 :id => "card_1FOh6k2eZvKYlo2C86LDl8BE",
                             :object => "card",
                       :address_city => nil,
                    :address_country => nil,
                      :address_line1 => nil,
                :address_line1_check => nil,
                      :address_line2 => nil,
                      :address_state => nil,
                        :address_zip => nil,
                  :address_zip_check => nil,
                              :brand => "MasterCard",
                            :country => "US",
                           :customer => "cus_FuW98aPiB42PLY",
                          :cvc_check => nil,
                      :dynamic_last4 => nil,
                          :exp_month => 10,
                           :exp_year => 2020,
                        :fingerprint => "7a9bk9ncM08SXfua",
                            :funding => "credit",
                              :last4 => "4444",
                           :metadata => {},
                               :name => nil,
                :tokenization_method => nil
            }
        ],
           :has_more => false,
        :total_count => 1,
                :url => "/v1/customers/cus_FuW98aPiB42PLY/sources"
    },
            :subscriptions => {
             :object => "list",
               :data => [],
           :has_more => false,
        :total_count => 0,
                :url => "/v1/customers/cus_FuW98aPiB42PLY/subscriptions"
    },
               :tax_exempt => "none",
                  :tax_ids => {
             :object => "list",
               :data => [],
           :has_more => false,
        :total_count => 0,
                :url => "/v1/customers/cus_FuW98aPiB42PLY/tax_ids"
    },
                 :tax_info => nil,
    :tax_info_verification => nil
}

レスポンス例(↑の例ではchargeに入るもの)

> charge.class
=> Stripe::Charge < Stripe::APIResource

> charge
=> {
                             :id => "ch_1FOh8a2eZvKYlo2CqJvuzqx2",
                         :object => "charge",
                         :amount => 1000,
                :amount_refunded => 0,
                    :application => nil,
                :application_fee => nil,
         :application_fee_amount => nil,
            :balance_transaction => "txn_1FOh8b2eZvKYlo2C0J9XmWSz",
                :billing_details => {
        :address => {
                   :city => nil,
                :country => nil,
                  :line1 => nil,
                  :line2 => nil,
            :postal_code => nil,
                  :state => nil
        },
          :email => nil,
           :name => nil,
          :phone => nil
    },
                       :captured => true,
                        :created => 1569920116,
                       :currency => "jpy",
                       :customer => "cus_FuW98aPiB42PLY",
                    :description => nil,
                    :destination => nil,
                        :dispute => nil,
                   :failure_code => nil,
                :failure_message => nil,
                  :fraud_details => {},
                        :invoice => nil,
                       :livemode => false,
                       :metadata => {},
                   :on_behalf_of => nil,
                          :order => nil,
                        :outcome => {
        :network_status => "approved_by_network",
                :reason => nil,
            :risk_level => "normal",
            :risk_score => 34,
        :seller_message => "Payment complete.",
                  :type => "authorized"
    },
                           :paid => true,
                 :payment_intent => nil,
                 :payment_method => "card_1FOh6k2eZvKYlo2C86LDl8BE",
         :payment_method_details => {
        :card => {
                     :brand => "mastercard",
                    :checks => {
                      :address_line1_check => nil,
                :address_postal_code_check => nil,
                                :cvc_check => nil
            },
                   :country => "US",
                 :exp_month => 10,
                  :exp_year => 2020,
               :fingerprint => "7a9bk9ncM08SXfua",
                   :funding => "credit",
                     :last4 => "4444",
            :three_d_secure => nil,
                    :wallet => nil
        },
        :type => "card"
    },
                  :receipt_email => nil,
                 :receipt_number => nil,
                    :receipt_url => "https://pay.stripe.com/receipts/acct_1032D82eZvKYlo2C/ch_1FOh8a2eZvKYlo2CqJvuzqx2/rcpt_FuWAT1u6h54pfgGz0uOTzqZbV8EgbPX",
                       :refunded => false,
                        :refunds => {
             :object => "list",
               :data => [],
           :has_more => false,
        :total_count => 0,
                :url => "/v1/charges/ch_1FOh8a2eZvKYlo2CqJvuzqx2/refunds"
    },
                         :review => nil,
                       :shipping => nil,
                         :source => {
                         :id => "card_1FOh6k2eZvKYlo2C86LDl8BE",
                     :object => "card",
               :address_city => nil,
            :address_country => nil,
              :address_line1 => nil,
        :address_line1_check => nil,
              :address_line2 => nil,
              :address_state => nil,
                :address_zip => nil,
          :address_zip_check => nil,
                      :brand => "MasterCard",
                    :country => "US",
                   :customer => "cus_FuW98aPiB42PLY",
                  :cvc_check => nil,
              :dynamic_last4 => nil,
                  :exp_month => 10,
                   :exp_year => 2020,
                :fingerprint => "7a9bk9ncM08SXfua",
                    :funding => "credit",
                      :last4 => "4444",
                   :metadata => {},
                       :name => nil,
        :tokenization_method => nil
    },
                :source_transfer => nil,
           :statement_descriptor => nil,
    :statement_descriptor_suffix => nil,
                         :status => "succeeded",
                  :transfer_data => nil,
                 :transfer_group => nil
}

エラー処理

こちらを参照。

独自Exception

こちらを参照。

モック

こちらを参照。

実装時の注意点

こちらを参照。

おまけ

フリーランスで活動しています。
開発案件(Go、Rails、Blockchain、機械学習など)をお待ちしております。

職務経歴書(wantedlyへのリンク)

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3