Links:
Contents:
Create an account on stripe.com and note the secret_key and publication_key.
API version: 2017-04-06
Add following to Gemfile.
gem 'stripe', '~> 1.58.0'
Run bundle install
. Now, you are ready to use Stripe API through stripe-ruby.
Set global secret_key
for Stripe.
require 'stripe'
Stripe.api_key = secret_key
Register your platform on Dashboard. Note down the client_id
for all environments.
Only attribute required to create a managed account on Stripe is country
.
account = Stripe::Account.create(country: 'US', managed: true) # => #<Stripe::Account:0x3ffc2e288ccc id=acct_19oAnaC7ST68TeIn> JSON: { ... }
Save the keys from the response. These keys will not be returned in any further requests.
account.keys.secret # => "sk_test_3m1beY55s2dgsuu8tSRsaopV"
account.keys.publishable # => "pk_test_0DhzuYi6lmhHUO9jKoUukY4O"
You can get information about test bank accounts for various countries here:
Also, see all the arguments that you can pass to create a managed account here.
There are different stages of a managed account on Stripe.
If you have all the verification fields needed to fulfill first two stages of managed account initially, you can pass them when creating the account.
external_account = { object: 'bank_account',
country: 'US',
currency: 'usd',
routing_number: '110000000',
account_number: '000123456789' }
tos_acceptance = { date: Time.now.to_i, ip: '119.82.78.186' }
dob = { day: 20, month: 1, year: 1990 }
address = { line1: 'Metropolitan Life North Building',
line2: '11 Madison Ave',
city: 'New York',
state: 'CA',
postal_code: '10010' }
legal_entity = { dob: dob,
first_name: 'Lance',
last_name: 'Melia',
type: 'company',
business_name: 'Eleven Madison Park',
address: address,
ssn_last_4: 1234,
business_tax_id: 123456789 }
account = Stripe::Account.create(country: 'US', managed: true,
external_account: external_account,
tos_acceptance: tos_acceptance,
legal_entity: legal_entity)
account.charges_enabled # => true
account.transfers_enabled # => true
account.verification.fields_needed # => ["legal_entity.personal_id_number", "legal_entity.verification.document"]
Download the test success image. I have also stored the image on S3. Download it to your local and connect it to your Stripe account.
file_obj = Stripe::FileUpload.create({ purpose: 'identity_document',
file: File.new('success.png') },
stripe_account: acc_id) # => #<Stripe::FileUpload:0x3fd9b48e9ae0 id=file_19pFt6FeV5ZvsmJc3dKeAfaU> JSON: { ... }
file = file_obj.id # => "file_19pFt6FeV5ZvsmJc3dKeAfaU"
Now, connect this file to your managed account and provide other remaining fields.
account.legal_entity.personal_id_number = 123456789
account.legal_entity.verification.document = file
account.save
account.verification.fields_needed # => []
This information is needed for both individual and company accounts.
external_account = { object: 'bank_account',
country: 'HK',
currency: 'hkd',
routing_number: '123456',
account_number: '000123-456' }
tos_acceptance = { date: Time.now.to_i, ip: '119.82.78.186' }
dob = { day: 20, month: 1, year: 1990 }
address = { line1: 'Pacific Place', city: 'Admiralty', state: 'Hong Kong', postal_code: '999077' }
personal_address = { line1: 'Four Seasons',
line2: '8 Finance St',
city: 'Central',
state: 'Hong Kong',
postal_code: '999077' }
legal_entity = { dob: dob,
first_name: 'Miesha',
last_name: 'Lahr',
type: 'company',
business_name: 'Cafe Grey Deluxe',
address: address,
personal_address: personal_address,
business_tax_id: 123456789 }
account = Stripe::Account.create(country: 'HK', managed: true,
external_account: external_account,
tos_acceptance: tos_acceptance,
legal_entity: legal_entity)
Upload the verification document and connect it to managed account.
account.legal_entity.personal_id_number = 123456789
account.legal_entity.verification.document = file
account.save
account.verification.fields_needed # => []
external_account = { object: 'bank_account',
country: 'SG',
currency: 'sgd',
routing_number: '1100-000',
account_number: '000123456' }
tos_acceptance = { date: Time.now.to_i, ip: '119.82.78.186' }
dob = { day: 20, month: 1, year: 1984 }
additional_owners = [{ first_name: 'Lenny', last_name: 'Maurry', dob: dob,
address: { line1: '438 Serangoon Rd', postal_code: 218133 } },
{ first_name: 'Gia', last_name: 'Scott', dob: dob,
address: { line1: '7 Raffles Ave', postal_code: 039799 } }]
address = { line1: '1 Cuscaden Rd', postal_code: '249715' }
personal_address = { line1: '10 Dempsey Rd', postal_code: 247700 }
legal_entity = { additional_owners: additional_owners,
dob: dob,
first_name: 'Shila',
last_name: 'Banks',
type: 'company',
business_name: 'Basilico Restaurant',
business_tax_id: 123456789,
address: address,
personal_address: personal_address }
account = Stripe::Account.create(country: 'SG', managed: true,
external_account: external_account,
tos_acceptance: tos_acceptance,
legal_entity: legal_entity)
Upload the verification document and connect it to managed account.
account.legal_entity.personal_id_number = 123456789
account.legal_entity.verification.document = file
Also upload the verification documents for all the additional owners.
file = Stripe::FileUpload.create({ purpose: 'identity_document', file: File.new('success.png') },
stripe_account: account.id)
additional_owner_params = [{ first_name: 'Lenny', last_name: 'Maurry', dob: dob,
address: { line1: '438 Serangoon Rd', postal_code: 218133 },
verification: { document: file.id } }
account.legal_entity.additional_owners = additional_owner_params
account.save
account.verification.fields_needed # => []
external_account = { object: 'bank_account',
country: 'JP',
currency: 'jpy',
account_holder_name: 'An Yutzy',
routing_number: '1100000',
account_number: '00012345' }
address = { line1: '4-4-13',
town: 'Shibakoen',
city: 'Tokyo',
state: 'Tokyo',
postal_code: '1050011' }
address_kana = { line1: '27-15 FLAG 3A',
town: 'ジングウマエ 3-',
city: 'シブヤク',
state: 'トウキョウト',
postal_code: '1500001' }
address_kanji = { line1: '27-15 FLAG 3A',
town: '神宮前 3丁目',
city: '渋谷区',
state: '東京都',
postal_code: '1500001' }
dob = { day: 20, month: 1, year: 1990 }
legal_entity = { type: 'company',
first_name_kana: 'アン',
first_name_kanji: 'ヤトジー',
last_name_kana: '丹几',
last_name_kanji: 'と凵卞乙と',
gender: 'female',
phone_number: '08012345678',
business_name: 'Robot Restaurant',
business_name_kana: ' ローボット・レストロント',
business_name_kanji: '尺回日回卞 尺ヨ己卞丹凵尺丹几卞',
business_tax_id: 123456789,
address_kana: address_kana,
address_kanji: address_kanji,
personal_address_kana: address_kana,
personal_address_kanji: address_kanji,
dob: dob }
tos_acceptance = { date: Time.now.to_i, ip: '119.82.78.186' }
account = Stripe::Account.create(country: 'JP', managed: true,
external_account: external_account,
tos_acceptance: tos_acceptance,
legal_entity: legal_entity)
Now, upload the verification document and connect it to managed account. See here.
account.legal_entity.personal_id_number = 123456789
account.legal_entity.verification.document = file
account.save
account.verification.fields_needed # => []
account = Stripe::Account.retrieve(account_id) # => #<Stripe::Account:0x3ffc2e288ccc id=acct_19oAnaC7ST68TeIn> JSON: { ... }
credit_card = { object: 'card',
number: '4242424242424242',
exp_year: Time.now.year + 1,
exp_month: 11,
cvc: '123',
name: 'Ammie Core' }
charge = Stripe::Charge.create(source: credit_card,
currency: 'JPY',
amount: 121843,
destination: { account: acc_id }) # => #<Stripe::Charge:0x3fdb74fb30bc id=ch_19ieOgAz1CWwcBCUZbMBRMhh> JSON: { ... }
charge.status # => "succeeded"
charge.on_behalf_of # => "acct_19pxWWLqhivFDn6r"
Other ways to create a charge are:
- Use the above credit card info to generate a token with Stripe.js and pass that token as
source
in params. - Create a customer on Stripe and pass
customer: CUSTOMER_ID
instead of passingsource
in params.
Pass capture: false
in the arguments hash to Stripe::Charge.create
along with other options.
You can call capture
on charge
to capture partial or full amount.
refund = Stripe::Refund.create(charge: charge.id, reverse_transfer: true) # => #<Stripe::Refund:0x3ff40d94c6b8 id=re_19pzDiAz1CWwcBCUeBYfoho0> JSON: { ... }
Same as void
.
account = Stripe::Charge.retrieve(account_id)
account.delete
You can manage webhooks for Stripe Connect here.
You will need a public url to access your local application through webhooks.
You can generate a public url using ngrok. Install the ngrok
with Homebrew.
$ brew update
$ brew cask install ngrok
Check your ip address by running ifconfig
.
Now, open a terminal and run the following command:
$ ngrok http 192.168.1.15:3000
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online
Version 2.1.18
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://8289e474.ngrok.io -> 192.168.1.15:3000
Forwarding https://8289e474.ngrok.io -> 192.168.1.15:3000
...
Your local application is now accessible at http://8289e474.ngrok.io
.
You can use this url for creating the webhook on Stripe.