gpt4 book ai didi

ruby-on-rails - ActiveMerchant 支持确定 PayPal Express Checkout 客户/买家的帐户状态(已验证/未验证)

转载 作者:太空宇宙 更新时间:2023-11-03 16:15:03 26 4
gpt4 key购买 nike

我目前正在开发一个 Ruby-on-Rails 网络应用程序,它通过 PayPal 的 Express Checkout 和 ActiveMerchant 接受 PayPal 付款。我对 ActiveMerchant 支持确定客户/买家是否使用经过验证或未经验证的 PayPal 帐户进行了多项研究,但我没有找到有用的指南。

我还发现 ActiveMerchant 目前没有很好的文档记录,所以它根本没有帮助。

下面是我的项目目前正在使用的相关代码。在 PaymentsController#purchase 上,我临时使用了 ActiveMerchant::Billing 的 #params['protection_eligibility']#params['protection_eligibility_type'] 方法: :PaypalExpressResponse 由 EXPRESS_GATEWAY.purchase 方法调用返回的对象,用于评估 PayPal 客户/买家是否拥有已验证/未验证的 PayPal 帐户。后来我发现这并不是了解客户账户状态的可靠依据。

我希望有人能告诉我 PayPal 客户/买家是否有使用 Ruby-on-Rails 的 ActiveMerchant 或使用 Rails 上的其他替代方案的已验证/未验证帐户的智慧。

# config/environments/development.rb
config.after_initialize do
ActiveMerchant::Billing::Base.mode = :test
paypal_options = {
# credentials removed for this StackOverflow question
:login => "",
:password => "",
:signature => ""
}
::EXPRESS_GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
end

# app/models/payment.rb
class Payment < ActiveRecord::Base
# ...
PAYPAL_CREDIT_TO_PRICE = {
# prices in cents(US)
1 => 75_00,
4 => 200_00,
12 => 550_00
}
STATUSES = ["pending", "complete", "failed"]
TYPES = ["paypal", "paypal-verified", "paypal-unverified", "wiretransfer", "creditcard"]
# ...
end

# app/controllers/payments_controller.rb
class PaymentsController < ApplicationController
# ...
def checkout
session[:credits_qty] = params[:credit_qty].to_i

total_as_cents = Payment::PAYPAL_CREDIT_TO_PRICE[session[:credits_qty]]
setup_purchase_params = {
:allow_guest_checkout => true,
:ip => request.remote_ip,
:return_url => url_for(:action => 'purchase', :only_path => false),
:cancel_return_url => url_for(:controller => 'payments', :action => 'new', :only_path => false),
:items => [{
:name => pluralize(session[:credits_qty], "Credit"),
:number => 1,
:quantity => 1,
:amount => Payment::PAYPAL_CREDIT_TO_PRICE[session[:credits_qty]]
}]
}

setup_response = EXPRESS_GATEWAY.setup_purchase(total_as_cents, setup_purchase_params)
redirect_to EXPRESS_GATEWAY.redirect_url_for(setup_response.token)
end

def purchase
if params[:token].nil? or params[:PayerID].nil?
redirect_to new_payment_url, :notice => I18n.t('flash.payment.paypal.error')
return
end

total_as_cents = Payment::PAYPAL_CREDIT_TO_PRICE[session[:credits_qty]]
purchase_params = {
:ip => request.remote_ip,
:token => params[:token],
:payer_id => params[:PayerID],
:items => [{
:name => pluralize(session[:credits_qty], "Credit"),
:number => 1,
:quantity => 1,
:amount => Payment::PAYPAL_CREDIT_TO_PRICE[session[:credits_qty]]
}]
}

purchase = EXPRESS_GATEWAY.purchase total_as_cents, purchase_params
if purchase.success?
payment = current_user.payments.new
payment.paypal_params = params
payment.credits = session[:credits_qty]
payment.amount = Payment::PAYPAL_CREDIT_TO_PRICE[session[:credits_qty]]
payment.currency = "USD"
payment.status = "complete"
if(purchase.params["receipt_id"] && (purchase.params["success_page_redirect_requested"] == "true"))
payment.payment_type = "creditcard"
else
if purchase.params['protection_eligibility'] == 'Eligible' && purchase.params['protection_eligibility_type'] == 'ItemNotReceivedEligible,UnauthorizedPaymentEligible'
payment.payment_type = 'paypal-verified'
else
payment.payment_type = 'paypal-unverified'
end
end
payment.ip_address = request.remote_ip.to_s
payment.save!

SiteMailer.paypal_payment(payment).deliver
SiteMailer.notice_payment(payment).deliver

notice = I18n.t('flash.payment.status.thanks')
redirect_to profile_url, :notice => notice
else
notice = I18n.t('flash.payment.status.failed', :message => purchase.message)
redirect_to new_payment_url, :notice => notice
end
end
# ...
end

最佳答案

我使用 PayPal 的 paypal-sdk-merchant gem 来完成 ActiveMerchant 无法帮助我的工作(获取付款人账户的状态:已验证/未验证的 PayPal 账户。我遵循了 paypal-sdk-merchant 的安装在 https://github.com/paypal/merchant-sdk-ruby

  • gem 'paypal-sdk-merchant'
  • 捆绑安装
  • rails 生成 paypal:sdk:install

然后我设置了他们的样本 https://github.com/paypal/merchant-sdk-ruby#samples .设置示例后,转到您的 Rails 应用程序的/samples,您会发现许多代码生成器,可满足您从 PayPal 的 API 中需要的任何功能。不要忘记配置您的 config/paypal.yml。我配置了用户名、密码和签名(可以从您的 PayPal 沙盒中获取,特别是您的测试卖家帐户)并在我的案例中删除了 app_id。

我使用 PayPal 的示例 (/samples) 获得了以下用于获取 PayPal 付款人帐户状态(已验证/未验证)的代码,并将其放在我的应用程序模型的类方法中:

def self.get_paypal_payer_status(transaction_id)
require 'paypal-sdk-merchant'
api = PayPal::SDK::Merchant::API.new

get_transaction_details = api.build_get_transaction_details({
:Version => "94.0",
:TransactionID => transaction_id
})
get_transaction_details_response = api.get_transaction_details(get_transaction_details)

get_transaction_details_response.payment_transaction_details.payer_info.payer_status
end

关于ruby-on-rails - ActiveMerchant 支持确定 PayPal Express Checkout 客户/买家的帐户状态(已验证/未验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14553933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com