格子条纹 Rails 应用程序
Plaid with Stripe Rails app
我有一个应用程序成功地使用 Stripe 向用户收取访问应用程序的费用。在此基础上,我想在我的结账过程中实施 Plaid。由于这是我第一次实现 Stripe,现在是我第一次使用 Plaid,所以我对方向以及如何推动此功能向前发展感到有些迷茫。
我安装了 plaid-ruby gem,并通过 figaro 添加了我的格子 secret_key 和 client_id,但现在我迷路了。
Plaid.rb:
Plaid.configuration.stripe = {
p.client_id = ENV['PLAID_CLIENT_ID'],
p.secret = ENV['PLAID_SECRET_KEY'],
p.env = :tartan # or :production
end
格纹 Link:
<button id='linkButton'>Open Plaid Link</button>
<script src="https://cdn.plaid.com/link/stable/link-initialize.js"></script>
<script>
var linkHandler = Plaid.create({
env: 'tartan',
clientName: 'ACCR',
key: '<<< MY_PUBLIC_KEY >>>',
product: 'auth',
selectAccount: true,
env: 'tartan',
onSuccess: function(public_token, metadata) {
// Send the public_token and account ID to your app server.
console.log('public_token: ' + public_token);
console.log('account ID: ' + metadata.account_id);
},
});
// Trigger the Link UI
document.getElementById('linkButton').onclick = function() {
linkHandler.open();
};
</script>
这是我的 Stripe 控制器中的内容:
def new
@stripe_btn_data = {
key: "#{ Rails.configuration.stripe[:publishable_key] }",
}
end
#1 Create a charge
customer = if current_user.stripe_id?
Stripe::Customer.retrieve(current_user.stripe_id)
else
Stripe::Customer.create(email: :stripeEmail)
end
current_user.update(
stripe_id: customer.id,
)
stripe_charge = Stripe::Charge.create(
customer: customer.id,
amount: (current_order.subtotal * 100).to_i,
currency: 'usd',
)
... more information to create an order then send email after charge.
我需要在我的控制器或其他地方添加什么才能通过 Plaid 和 Stripe 创建费用?
格子也有 writeup on how to wire this up - though the provided example is in Node.js - and Stripe has a guide。
也就是说,您需要在 onSuccess
处理程序中添加代码以将 public_token
和 metadata.account_id
发送到服务器的 'token exchange' endpoint and once you've got that, you can exchange those for the Stripe token,最后,您'需要将该令牌附加到客户。
所以,像这样:
plaid_user = Plaid::User.exchange_token(
public_token,
metadata_dot_account_id,
product: :auth
)
puts plaid_user.stripe_bank_account_token
customer = Stripe::Customer.retrieve("<customer-id>")
customer.sources.create({
:source => plaid_user.stripe_bank_account_token
})
然后你可以Stripe::Charge.create()
。
我有一个应用程序成功地使用 Stripe 向用户收取访问应用程序的费用。在此基础上,我想在我的结账过程中实施 Plaid。由于这是我第一次实现 Stripe,现在是我第一次使用 Plaid,所以我对方向以及如何推动此功能向前发展感到有些迷茫。
我安装了 plaid-ruby gem,并通过 figaro 添加了我的格子 secret_key 和 client_id,但现在我迷路了。
Plaid.rb:
Plaid.configuration.stripe = {
p.client_id = ENV['PLAID_CLIENT_ID'],
p.secret = ENV['PLAID_SECRET_KEY'],
p.env = :tartan # or :production
end
格纹 Link:
<button id='linkButton'>Open Plaid Link</button>
<script src="https://cdn.plaid.com/link/stable/link-initialize.js"></script>
<script>
var linkHandler = Plaid.create({
env: 'tartan',
clientName: 'ACCR',
key: '<<< MY_PUBLIC_KEY >>>',
product: 'auth',
selectAccount: true,
env: 'tartan',
onSuccess: function(public_token, metadata) {
// Send the public_token and account ID to your app server.
console.log('public_token: ' + public_token);
console.log('account ID: ' + metadata.account_id);
},
});
// Trigger the Link UI
document.getElementById('linkButton').onclick = function() {
linkHandler.open();
};
</script>
这是我的 Stripe 控制器中的内容:
def new
@stripe_btn_data = {
key: "#{ Rails.configuration.stripe[:publishable_key] }",
}
end
#1 Create a charge
customer = if current_user.stripe_id?
Stripe::Customer.retrieve(current_user.stripe_id)
else
Stripe::Customer.create(email: :stripeEmail)
end
current_user.update(
stripe_id: customer.id,
)
stripe_charge = Stripe::Charge.create(
customer: customer.id,
amount: (current_order.subtotal * 100).to_i,
currency: 'usd',
)
... more information to create an order then send email after charge.
我需要在我的控制器或其他地方添加什么才能通过 Plaid 和 Stripe 创建费用?
格子也有 writeup on how to wire this up - though the provided example is in Node.js - and Stripe has a guide。
也就是说,您需要在 onSuccess
处理程序中添加代码以将 public_token
和 metadata.account_id
发送到服务器的 'token exchange' endpoint and once you've got that, you can exchange those for the Stripe token,最后,您'需要将该令牌附加到客户。
所以,像这样:
plaid_user = Plaid::User.exchange_token(
public_token,
metadata_dot_account_id,
product: :auth
)
puts plaid_user.stripe_bank_account_token
customer = Stripe::Customer.retrieve("<customer-id>")
customer.sources.create({
:source => plaid_user.stripe_bank_account_token
})
然后你可以Stripe::Charge.create()
。