简体   繁体   中英

PayPal IPN - Paypal Website Standard

I've been following Railscasts on implementing Paypal Standard payments. On receiving IPN notifications, i realized i wasn't receiving Item specifics like item_name, item_number, & quantity . After some investigation, i figured i had written them wrongly in my controller as PayPal sends the variables as item_name1 , item_number1 , quantity1 , item_name2 , item_number2 , quantity2 and so on

I've got the Railscasts setup. The notifications come through a controller

  PaymentNotificationsController < ApplicationController
    protect_from_forgery except: [:create]
     def create
       PaymentNotification.create!(params: params, 
       item_number: params[:item_number], item_name: params[:item_name], quantity: params[:quantity] 
       render nothing: true
     end

In a case where an order has multiple items, it would be item_name1 , item_name2 , item_name3 and so on. What's the right way to name these variables, to be able to accept the paypal IPN notifications without adding a column for every extra item?

Thanks in advance!

You can look at the params before calling PaymentNotification.create! . Using a loop to check what fields are within params , you can count how many items there are and extract all their properties to an array. After that you can use the data in that array however you need to.

This code assumes that PayPal never sends a response with a field item_name – that if there is one item, they still send a field item_name1 . I don't know if that is actually true.

ITEM_PARAM_PREFIXES = ["item_name", "item_number", "quantity"]

def extract_ipn_items_params(params)
  item_params = []

  loop do
    item_num_to_test = item_params.length + 1
    item_num_suffix = item_num_to_test.to_s
    possible_param_name = ITEM_PARAM_PREFIXES[0] + item_num_suffix
    if params.include?(possible_param_name)
      this_item_params = {}
      ITEM_PARAM_PREFIXES.each do |prefix|
        this_item_params[prefix] = params[prefix + item_num_suffix]
      end
      item_params.push this_item_params
    else
      return item_params
    end
  end
end

Given some params like

{"item_name1"=>"cards", "item_number1"=>"123", "quantity1"=>"1",
 "item_name2"=>"book", "item_number2"=>"55", "quantity2"=>"2",
 "other_fields"=>"whatever"}

The above method should return this array of hashes:

[{"item_name"=>"cards", "item_number"=>"123", "quantity"=>"1"},
 {"item_name"=>"book",  "item_number"=>"55",  "quantity"=>"2"}]

Hopefully that format allows you to easily do whatever you need to with that information. For example, you could call .length on it to count how many items there were, and you could iterate over each element and pass each hash to PaymentNotification.create! if you wanted a separate notification for each item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM