Paypal反复出现问题ruby on rails

时间:2015-09-18 21:37:28

标签: ruby-on-rails paypal paypal-subscriptions recurring-billing

我一直在关注如何使用Paypal进行定期付款的Railscast 289教程。

我现在一切正常,除了我现在正在努力弄清楚我怎么能知道用户是否在paypal中取消了他们的订阅。

我做了什么

我设法在paypal商家帐户中设置IPN网址,每次有人订阅它都会将数据从paypal发送给我的webhook。

现在我存储了paypal发给我的所有参数,但是我无法确定我应该检查哪些参数,以便知道用户是否已取消订阅或者资金是否足够等。

至少这是我认为它有效的方式。

我正在使用Paypal Recurring Gem

问题

当paypal将我的IPN发送给我的webhook时,我如何注意到用户取消订阅的时间。

我的webhook atm

def create
    user_id = Subscription.find_by(paypal_customer_token: params[:payer_id]).user_id
    PaymentNotification.create!(params: params, user_id: user_id, user_role_id: params[:item_number], status: params[:payment_status], transaction_id: params[:txn_id])
    @user = User.find_by(id: user_id)
    if PaymentNotification.last.params[:profile_status] == "Cancelled"
        @user.update_attributes(user_role_id: 1)
    end
    render nothing: true
end

注意我不想立即更新用户属性我想等到他们的月订阅结束。

我目前正将他们的IPN通话存储在PaymentNotification表中。

create_table "payment_notifications", force: :cascade do |t|
  t.text     "params"
  t.integer  "user_role_id"
  t.string   "status"
  t.string   "transaction_id"
  t.datetime "created_at",     null: false
  t.datetime "updated_at",     null: false
  t.integer  "user_id"
end

此外,检查这些参数以便对尚未付款或取消订阅的用户采取措施的最佳方法是什么?

我有另一个存储订阅的表

create_table "subscriptions", force: :cascade do |t|
  t.integer  "user_role_id"
  t.integer  "user_id"
  t.string   "paypal_customer_token"
  t.string   "paypal_recurring_profile_token"
  t.datetime "created_at",                     null: false
  t.datetime "updated_at",                     null: false
end

user_role在这种情况下是他们订阅的plan

1 个答案:

答案 0 :(得分:2)

IPN可确保您在交易发生任何变更时获得通知,从定期付款的角度来看,当您的客户取消配置文件时,触发的事件将发送回您的IPN听众

取消配置文件取消的示例原始POST-Back消息将是这样的,

payment_cycle=Daily &txn_type=recurring_payment_profile_cancel &last_name=US &next_payme
nt_date=N/A &residence_country=US &initial_payment_amount=0.00 &currency_code=USD &t
ime_created=19:25:09 Sep 24, 2015 PDT &verify_sign=AgUGxKs4vGiEqeit6IyHkIjDJVpeAqCMRVC4wh9CZYotn
Jqrr-3oWsFe &period_type= Trial &payer_status=verified &test_ipn=1 &tax=0.00 &pa
yer_email=USP@email.com &first_name=Payer &receiver_email=USM@email.com &payer_id=8FMFQ2
KVYYHTY &product_type=1 &shipping=0.00 &amount_per_cycle=2.00 &profile_status=Cancel
led &charset=gb2312 &notify_version=3.8 &amount=2.00 &outstanding_balance=0.00 &recurring_payment_id=I-30FKJ55UV1RW &product_name=RP BA Test &ipn_track_id=65583f3a80f10
  1. 您如何能够注意到取消发生的时间?

    阅读IPN消息中的参数txn_type,并使用它确定事件类型(在您的情况下,事件类型为recurring_payment_profile_cancel,在txn_type列表中查找更多详细信息。 PayPal IPN Variables

  2. 将配置文件/订阅与您的数据库条目进行协调并采取措施

    将IPN邮件中的recurring_payment_id=I-30FKJ55UV1RW与预先存储的数据库条目paypal_recurring_profile_token进行匹配,然后采取措施更新user_role_id字段。 (立即停止订阅或根据您的业务模式更新/设置新的到期日期)