rails 上的 Paypal 重复问题 ruby
Paypal recurring questions ruby on rails
我一直在关注 Railscast 289 关于如何使用 Paypal 进行定期付款的教程。
我现在一切正常,除了我现在正在努力弄清楚我如何才能知道用户是否取消了他们在 paypal 中的订阅。
我做了什么
我已经在 paypal 商家帐户中设置了 IPN url,每次有人订阅时,它都会将数据从 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 table 中。
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
此外,检查这些参数以便对未付款或已取消订阅的用户采取措施的最佳方法是什么?
我有另一个 table 存储订阅
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
。
IPN 确保在交易发生任何变化时通知您,从经常性付款的角度来看,当您的客户取消个人资料时,触发的事件 POST-Back 将发送给您的 IPN 侦听器.
A sample raw POST-配置文件取消的返回消息将是这样的,
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 ¤cy_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 ¬ify_version=3.8 &amount=2.00 &outstanding_balance=0.00 &recurring_payment_id=I-30FKJ55UV1RW &product_name=RP BA Test &ipn_track_id=65583f3a80f10
当发生取消时,您如何能够注意到?
读取 IPN 消息中的参数 txn_type
,并用它确定事件类型(在您的情况下,事件类型将是 recurring_payment_profile_cancel
,在 txn_type
列表中找到更多详细信息在 PayPal IPN Variables)
将 profile/subscription 与您的数据库条目进行核对并采取行动
将 IPN 消息中的 recurring_payment_id=I-30FKJ55UV1RW
与您预先存储的数据库条目 paypal_recurring_profile_token
匹配,并采取措施更新 user_role_id
字段。 (立即停止订阅或 update/set 根据您的业务模式设置新的到期日期)
我一直在关注 Railscast 289 关于如何使用 Paypal 进行定期付款的教程。
我现在一切正常,除了我现在正在努力弄清楚我如何才能知道用户是否取消了他们在 paypal 中的订阅。
我做了什么
我已经在 paypal 商家帐户中设置了 IPN url,每次有人订阅时,它都会将数据从 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 table 中。
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
此外,检查这些参数以便对未付款或已取消订阅的用户采取措施的最佳方法是什么?
我有另一个 table 存储订阅
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
。
IPN 确保在交易发生任何变化时通知您,从经常性付款的角度来看,当您的客户取消个人资料时,触发的事件 POST-Back 将发送给您的 IPN 侦听器.
A sample raw POST-配置文件取消的返回消息将是这样的,
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 ¤cy_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 ¬ify_version=3.8 &amount=2.00 &outstanding_balance=0.00 &recurring_payment_id=I-30FKJ55UV1RW &product_name=RP BA Test &ipn_track_id=65583f3a80f10
当发生取消时,您如何能够注意到?
读取 IPN 消息中的参数
txn_type
,并用它确定事件类型(在您的情况下,事件类型将是recurring_payment_profile_cancel
,在txn_type
列表中找到更多详细信息在 PayPal IPN Variables)将 profile/subscription 与您的数据库条目进行核对并采取行动
将 IPN 消息中的
recurring_payment_id=I-30FKJ55UV1RW
与您预先存储的数据库条目paypal_recurring_profile_token
匹配,并采取措施更新user_role_id
字段。 (立即停止订阅或 update/set 根据您的业务模式设置新的到期日期)