Is there a PayPal IPN code sample for Ruby on Rails?

前端 未结 7 448
渐次进展
渐次进展 2021-02-01 05:27

There are official code samples for several languages but couldn\'t find one for Rails.

相关标签:
7条回答
  • 2021-02-01 05:51

    I have implement IPN in one of my project and your code look fine. So what is the problem u r facing ?

    0 讨论(0)
  • 2021-02-01 05:52

    you can just do this to get ipn details. result will show you verified or not. you can get all details from body

    post '/english/ipn' do

    url = "https://sandbox.paypal.com/cgi-bin/webscr?cmd=_notify-validate&#{@query}"

    body = request.body.string

    result = RestClient.post url, body

    end

    0 讨论(0)
  • 2021-02-01 06:03

    I post here my working code sample for a Rails controller. It does verification. I hope it will be useful.

    class PaymentNotificationsController < ApplicationController
      protect_from_forgery :except => [:create] #Otherwise the request from PayPal wouldn't make it to the controller
      def create
        response = validate_IPN_notification(request.raw_post)
        case response
        when "VERIFIED"
          # check that paymentStatus=Completed
          # check that txnId has not been previously processed
          # check that receiverEmail is your Primary PayPal email
          # check that paymentAmount/paymentCurrency are correct
          # process payment
        when "INVALID"
          # log for investigation
        else
          # error
        end
        render :nothing => true
      end 
      protected 
      def validate_IPN_notification(raw)
        live = 'https://ipnpb.paypal.com/cgi-bin'
        sandbox = 'https://ipnpb.sandbox.paypal.com/cgi-bin'
        uri = URI.parse(sandbox + '/webscr?cmd=_notify-validate')
        http = Net::HTTP.new(uri.host, uri.port)
        http.open_timeout = 60
        http.read_timeout = 60
        http.verify_mode = OpenSSL::SSL::VERIFY_PEER
        http.use_ssl = true
        response = http.post(uri.request_uri, raw,
                             'Content-Length' => "#{raw.size}",
                             'User-Agent' => "My custom user agent"
                           ).body
      end
    end
    

    Code is inspired by Railscast 142 and this post by Tanel Suurhans

    0 讨论(0)
  • 2021-02-01 06:03

    PayPal's Ruby Merchant SDK provides an ipn_valid? boolean method to make this super easy on you.

    def notify
      @api = PayPal::SDK::Merchant.new
      if @api.ipn_valid?(request.raw_post)  # return true or false
        # params contains the data
      end
    end
    

    https://github.com/paypal/merchant-sdk-ruby/blob/master/samples/IPN-README.md

    0 讨论(0)
  • 2021-02-01 06:03

    There are a few PayPal gems, and at least one of them (paypal-sdk-rest) includes the PayPal::SDK::Core::API::IPN.valid? method.

    Here's how to use it:

    class YourController < ApplicationController
    
      skip_before_action :verify_authenticity_token, only: :your_action
    
      def your_action
        verified = PayPal::SDK::Core::API::IPN.valid?(request.raw_post)
    
        if verified
          # Verification passed, do something useful here.
          render nothing: true, status: :ok
        else
          # Verification failed!
          render nothing: true, status: :unprocessable_entity
        end
      end
    
    end
    
    0 讨论(0)
  • 2021-02-01 06:04

    Have a look at the ActiveMerchant gem, which includes multiple gateway implementations, amongst which is Paypal's IPN.

    HTH

    0 讨论(0)
提交回复
热议问题