活跃的商家贝宝定期付款
Posted
技术标签:
【中文标题】活跃的商家贝宝定期付款【英文标题】:Active Merchant paypal recurring payments 【发布时间】:2016-07-31 10:34:07 【问题描述】:我正在使用 Active Merchant gem 通过网站处理付款。但现在我想每月重复这些付款。有没有办法使用活跃的商家或?
subscription_controller.rb
class SubscriptionsController < ApplicationController
def new
@home_page = true
@white = true
@subscription = Subscription.new(token: params[:token])
if !logged_in?
redirect_to signup_url
end
end
def create
@subscription = Subscription.new(subscription_params)
@subscription.remote_ip = request.remote_ip
@subscription.user_id = current_user.id
if @subscription.save
if @subscription.purchase
@subscription.send_thank_you_email
redirect_to thankyou_path
else
raise ActiveRecord::Rollback
flash[:notice] = "It seems something went wrong with the paypal transaction. Please check that your credit card is valid and has credit in it and try again."
redirect_to :back
end
else
flash[:notice] = "Something went wrong with marking your purchase as complete. Please contact support to check it out."
redirect_to :back
end
end
def purchase
response = GATEWAY.setup_purchase(999,
ip: request.remote_ip,
return_url: new_subscription_url,
cancel_return_url: root_url,
currency: "USD",
items: [name: "Order", description: "Recurring payment for ******", quantity: "1", amount: 999]
)
redirect_to GATEWAY.redirect_url_for(response.token)
end
def thank_you
@home_page = true
@white = true
end
private
def subscription_params
params.require(:subscription).permit(:token)
end
end
subscription.rb 模型
def purchase
response = GATEWAY.purchase(999, express_purchase_options)
response.success?
end
def token=(token)
self[:token] = token
if new_record? && !token.blank?
# you can dump details var if you need more info from buyer
details = GATEWAY.details_for(token)
puts details.params["PayerInfo"]["PayerName"].inspect
self.payer_id = details.payer_id
self.first_name = details.params["PayerInfo"]["PayerName"]["FirstName"]
self.last_name = details.params["PayerInfo"]["PayerName"]["LastName"]
end
end
# send thank you email
def send_thank_you_email
UserMailer.thank_you(self).deliver_now
end
private
def express_purchase_options
:ip => remote_ip,
:token => token,
:payer_id => payer_id
end
production.rb 环境
config.after_initialize do
ActiveMerchant::Billing::Base.mode = :production
::GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(
:login => ENV['PAYPAL_LOGIN'],
:password => ENV['PAYPAL_PASSWORD'],
:signature => ENV['PAYPAL_SIGNATURE']
)
end
【问题讨论】:
【参考方案1】:我认为 ActiveMerchant 曾经有这样的东西:
subscription = PAYPAL_EXPRESS_GATEWAY.recurring(@subscription.price_in_cents, nil,
:description => 'blah',
:start_date => Date.tomorrow,
:period => 'Year',
:frequency => 1,
:amount => price,
:currency => 'USD'
)
看到这个答案Does ActiveMerchant support Subscription Based transaction
另见:https://github.com/activemerchant/active_merchant/blob/master/lib/active_merchant/billing/gateways/paypal_express.rb
https://github.com/activemerchant/active_merchant/blob/master/lib/active_merchant/billing/gateways/paypal/paypal_recurring_api.rb
【讨论】:
但是我该如何使用它呢?在我的情况下,我应该在哪里实现它? 似乎您可以在其中一个控制器中创建一个操作,需要 ActiveMerchant,然后使用您的产品信息构建请求。 经过研究,我认为贝宝经常性已被弃用。因此,我现在已经实现了很好地完成这项工作的 paypal_recurring gem。 更多关于 ActiveMerchant 弃用定期计费:github.com/activemerchant/active_merchant/pull/1178以上是关于活跃的商家贝宝定期付款的主要内容,如果未能解决你的问题,请参考以下文章