我正在努力将Rails应用程序与Paymill支付网关集成(使用paymill-
ruby gem),并且想知道是否有人可以在标准订单处理过程中提供与API交互的最佳方式的建议.我已经设法使基础工作正常,但是关于如何构建整个过程的资源非常少.
我有一个User模型,我想存储paymill客户端ID和支付ID,以及Payment模型,我存储每个事务的详细信息(引用订单ID).我的付款型号代码目前如下:
class Payment < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
attr_accessor :paymill_card_token, :email, :paymill_client_id, :paymill_payment_id
belongs_to :order
belongs_to :user
validates_presence_of :order_id
validates_presence_of :user_id
def save_with_payment
if valid?
if paymill_client_id.blank?
#if user hasn't paid before, create paymill client
client = Paymill::Client.create email: email, description: user_id
paymill_client_id = client.id
# update current user with paymill client ID
User.where("user_id = ?", user_id).update_attributes(:paymill_client_id => paymill_client_id)
end
if paymill_payment_id.blank?
#if paymill_payment_id isn't present, create new payment
payment = Paymill::Payment.create token: paymill_card_token, client: paymill_client_id
paymill_payment_id = payment.id
# update current user with paymill payment ID
User.where("user_id = ?", user_id).update_attributes(:paymill_payment_id => paymill_payment_id)
end
transaction = Paymill::Transaction.create client: paymill_client_id, amount: "#{amount.to_s.gsub('.', '')}0", currency: 'GBP', description: "ORDER ID #{order_id}", payment: paymill_payment_id
self.paymill_id = transaction.id
save!
end
rescue Paymill::PaymillError => e
logger.error "Paymill error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card. Please try again."
false
end
end
我在文档中的某处读到paymill支付对象(客户端保存的信用卡详细信息)仅有效期为一年 – 这是正确的吗?如果是这样,应如何处理?在我的用户模型中存储此到期日期以及用户信用卡到期日期是一个好主意,因此我知道何时要求他们再次输入他们的卡信息?
最佳答案 建议的更改:
>使用付款模式存储payment_card_token和payment_id.这将允许您为每个客户端支持多张卡,以备将来使用时使用
>创建PaymentTransaction模型和db表.每次用户付款时,都会将详细信息存储在PaymentTransaction模型中,而不是付款模式中.
>查看webhooks框架.我自己没有使用它,但它可能有助于解决有关过期令牌等问题
另外,关于rails代码的一般观察
>而不是User.where(“user_id =?”,user_id).update_attributes,只需说出user.update_attributes,因为您已经定义了关联.
>如果您可以将if paymill_client_id代码块移动到User模型,那就更好了.