質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Stripe

Stripeとは、米国のオンライン決済システム提供企業、及び同社が提供する決裁システムを指します。Webサイトやモバイルアプリにコードを組み込むことでクレジットカードなどの決済サービスが簡潔に追加できます。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

1248閲覧

RailsとStripeConnectで送金システムを作ろうとしているが、AccountAPIで取得したオブジェクトに情報を入れようとするとundefined methodエラーが出る

hitoyasablue

総合スコア8

Stripe

Stripeとは、米国のオンライン決済システム提供企業、及び同社が提供する決裁システムを指します。Webサイトやモバイルアプリにコードを組み込むことでクレジットカードなどの決済サービスが簡潔に追加できます。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/02/27 11:51

編集2021/02/28 12:17

前提・実現したいこと

RailsとStripeConnectを用いてユーザAからユーザBに送金するためのシステムを作ろうとしています。
想定している流れは以下です。

  1. ユーザAのカード情報を入力し、Stripe.jsによってStripeサーバからトークンを発行
  2. 発行したトークンとCustomerAPIを用いてユーザAのカード情報をStripeサーバに、顧客IDをローカルのデータベースに登録
  3. ユーザAがユーザBに対する送金ボタンを押す
  4. AccountAPIをCustomタイプで利用し、ユーザBの情報をStripeサーバに登録
  5. 送金する

現在1~3までは完了しているのですが、4の工程で詰まっています。

発生している問題・エラーメッセージ

取得したAccountのオブジェクトにコード内で住所や氏名を入れようとしていますが、
Stripe公式で記載されているメソッドを用いて情報を入れようとしても以下のエラーが生じてしまいます。

undefined method `individual' for #

公式の記事はこちらです。ページ中ごろの項目でcounrtyをJapanに、Business-typeをindividualに設定すると該当の情報が出てきます。

stripe_account.individual.address_kanji.state = "東京"

この記事ではAccountAPIをCustomerタイプで取得したオブジェクトはindividualの要素を持ち、その下でさらにaddress_kanjiやfirst_name_kanjiなどの要素を持つと説明があるのでそこに情報を入れようとしているのですがエラーが出ます。

関連すると思われるソースコード

ruby

1#ユーザAをCustomerAPIを用いて作成しているコントローラ 2 3class CardsController < ApplicationController 4 require "stripe" 5 6 def new 7 card = Card.where(user_id: current_user.id) 8 redirect_to new_post_gift_url(id: params[:id]) if card.exists? 9 end 10 11 def create 12 Stripe.api_key = '本来は秘密鍵を直書きしています' 13 if params['stripeToken'].blank? 14 redirect_to ports_url 15 else 16 sender = Stripe::Customer.create({ 17 name: current_user.name, 18 email: current_user.email, 19 source: params['stripeToken'], 20 }) 21 @card = Card.new( 22 user_id: current_user.id, 23 customer_id: sender.id, 24 card_id: params['stripeToken'], 25 ) 26 if @card.save 27 redirect_to new_post_gift_url 28 else 29 redirect_to posts_url 30 end 31 end 32 end 33 34end 35

ruby

1#ユーザBをAccountAPIを用いて作成しているコントローラ 2 3class GiftsController < ApplicationController 4 require 'stripe' 5 6 def new 7 end 8 9 def index 10 end 11 12 def create 13 Stripe.api_key = '本来は秘密鍵を直書きしています' 14 @amount = 100 15 @post = Post.find_by(id: params[:post_id]) 16 @reciever_local = User.find_by(id: @post.user_id) 17 @card_sender = Card.find_by(user_id: current_user.id) 18 19 # byebug 20 21 begin 22 23 reciever = Stripe::Account.create({ 24 type: 'custom', 25 country: 'JP', 26 business_type: 'individual', 27 # name: @reciever_local.name, 28 # email: @reciever_local.email, 29 capabilities: { 30 card_payments: {requested: true}, 31 transfers: {requested: true}, 32 }, 33 }) 34 35 stripe_account = Stripe::Account.retrieve(reciever.id) 36 37 # 事業者の種類(法人 or 個人) 38 stripe_account.business_type = "individual" #個人 39 stripe_account.external_accounts.create({ 40 :external_account => { 41 'object':'bank_account', 42 'account_number': '0001234', 43 'routing_number': '1100000', #銀行コード+支店コード 44 'account_holder_name':'トクテスト(カ', 45 'currency':'jpy', 46 'country':'jp' 47 } 48 }) 49 50 # byebug 51 52 # 事業者の住所(漢字) 53 # stripe_account.individual.address_kanji.postal_code = "1234567" 54 stripe_account.individual.address_kanji.state = "東京" 55 stripe_account.individual.address_kanji.city = "渋谷区" 56 stripe_account.individual.address_kanji.town = "恵比寿" 57 stripe_account.individual.address_kanji.line1 = "1-1-1" 58 stripe_account.individual.address_kanji.line2 = "テストビルディング101号" 59 60 # 事業者の住所(かな) 61 # stripe_account.individual.address_kana.postal_code = "1234567" 62 stripe_account.individual.address_kana.state = "とうきょうと" 63 stripe_account.individual.address_kana.city = "しぶやく" 64 stripe_account.individual.address_kana.town = "えびす" 65 stripe_account.individual.address_kana.line1 = "1-1-1" 66 stripe_account.individual.address_kana.line2 = "てすとびるでぃんぐ101ごう" 67 68 # 事業責任者の名前(漢字) 69 stripe_account.individual.first_name_kanji = "太郎" 70 stripe_account.individual.last_name_kanji = "田中" 71 72 # 事業責任者の名前(かな) 73 stripe_account.individual.first_name_kana = "たろう" 74 stripe_account.individual.last_name_kana = "たなか" 75 76 # 事業者責任者の誕生日 77 stripe_account.individual.dob.day = "1" 78 stripe_account.individual.dob.month = "1" 79 stripe_account.individual.dob.year = "2000" 80 81 # # 事業責任者の性別 82 stripe_account.individual.gender = "male" 83 84 # # 事業責任者の電話番号 85 stripe_account.individual.phone = "090-0000-0000" 86 87 # 受理された日付とグローバルIPアドレス 88 stripe_account.tos_acceptance.date = Time.now.to_i 89 stripe_account.tos_acceptance.ip = "192.168.0.1" #グローバルIPアドレスを入力 90 91 # Stripeに画像ファイルをアップロードするための処理 92 # 免許証やパスポートなどをアップロードする 93 verification_document = Stripe::FileUpload.create( 94 { 95 purpose: 'identity_document', 96 file: File.new("/Users/yusaku/works/komarigoto_hiroba/app/assets/images/inu.png") 97 }, 98 { 99 stripe_account: stripe_account_id 100 } 101 ) 102 103 # アップロードされたドキュメントのID番号 104 stripe_account.individual.verification.document = verification_document.id 105 106 stripe_account.save 107 108 gift = Stripe::PaymentIntent.create({ 109 payment_method_types: ['card'], 110 amount: @amount, 111 currency: "jpy", 112 customer: @sender_card.customer_id, #Stripe.jsで自動で付与されるカード情報のトークン 113 destination: { 114 account: reciever.id, 115 }, 116 }) 117 118 119 # stripe関連でエラーが起こった場合 120 rescue Stripe::CardError => e 121 flash[:error] = "#決済(stripe)でエラーが発生しました。{e.message}" 122 render :new 123 124 # Invalid parameters were supplied to Stripe's API 125 rescue Stripe::InvalidRequestError => e 126 flash.now[:error] = "決済(stripe)でエラーが発生しました(InvalidRequestError)#{e.message}" 127 render :new 128 129 # Authentication with Stripe's API failed(maybe you changed API keys recently) 130 rescue Stripe::AuthenticationError => e 131 flash.now[:error] = "決済(stripe)でエラーが発生しました(AuthenticationError)#{e.message}" 132 render :new 133 134 # Network communication with Stripe failed 135 rescue Stripe::APIConnectionError => e 136 flash.now[:error] = "決済(stripe)でエラーが発生しました(APIConnectionError)#{e.message}" 137 render :new 138 139 # Display a very generic error to the user, and maybe send yourself an email 140 rescue Stripe::StripeError => e 141 flash.now[:error] = "決済(stripe)でエラーが発生しました(StripeError)#{e.message}" 142 render :new 143 144 # stripe関連以外でエラーが起こった場合 145 rescue => e 146 flash.now[:error] = "エラーが発生しました#{e.message}" 147 render :new 148 end 149 150 posts_path and return 151 152 end 153 154 def delete 155 end 156 157 def show 158 end 159end 160

ruby

1#new.html.erb 送金ボタンを設置するためのコード 2 3<% provide(:button_text, '100円ギフトする') %> 4<%= button_to 'カード情報登録画面へ', new_post_card_path(id: params[:id]), method: :get, class: 'btn btn-outline-dark' %> 5 6<%= form_with url: post_gifts_path(id: params[:id]), method: :post, local: true do |f| %> 7 <%= f.submit yield(:button_text), id: 'card_button', class: 'btn btn-primary' %> 8<% end %>

試したこと

byebugで取得したAccountオブジェクトの中身を確認したところ、individualの代わりにrepresatativeという要素があり、その要素がaddress_kanjiやfirst_name_kanjiの要素を持っていました。
上記した公式のページを見ると、business-typeがcompaniesになっているとそのように要素がindividualではなくrepresatativeになるようなのですが、コードでは以下のようにbusiness-typeをindividualと設定しているため何故こうした状況になっているのか分かりません。

ruby

1#ユーザBをAccountAPIを用いて作成しているコントローラより再掲 2 3stripe_account.business_type = "individual"

補足情報(FW/ツールのバージョンなど)

StripeAPI:v3
Rails:6.0.3.5

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問