Class: ActiveMerchant::Billing::Integrations::PaypalAdaptivePayment::Notification

Inherits:
Notification
  • Object
show all
Includes:
PostsData
Defined in:
lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb

Overview

Parser and handler for incoming Instant payment notifications from paypal. The Example shows a typical handler in a rails application. Note that this is an example, please read the Paypal API documentation for all the details on creating a safe payment controller.

Example

class BackendController < ApplicationController
  include ActiveMerchant::Billing::Integrations

  def paypal_ipn
    notify = PaypalAdaptivePayment::Notification.new(request.raw_post)

    order = Order.find(notify.item_id)

    if notify.acknowledge
      begin

        if notify.complete? and order.total == notify.amount
          order.status = 'COMPLETED'

          shop.ship(order)
        else
          logger.error("Failed to verify Paypal's notification, please investigate")
        end

      rescue => e
        order.status = 'ERROR'
        raise
      ensure
        order.save
      end
    end

    render :nothing
  end
end

Instance Method Summary collapse

Instance Method Details

#accountObject



96
97
98
# File 'lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb', line 96

def 
  params['business'] || params['transaction[0].receiver']
end

#acknowledgeObject

Acknowledge the transaction to paypal. This method has to be called after a new ipn arrives. Paypal will verify that all the information we received are correct and will return a ok or a fail.

Example:

def paypal_ipn
  notify = PaypalAdaptivePaymentNotification.new(request.raw_post)

  if notify.acknowledge
    ... process order ... if notify.complete?
  else
    ... log possible hacking attempt ...
  end

Raises:

  • (StandardError)


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb', line 114

def acknowledge
  payload =  raw

  response = ssl_post(Paypal.service_url + '?cmd=_notify-validate', payload,
                 'Content-Length' => "#{payload.size}",
                 'User-Agent'     => "Active Merchant -- http://activemerchant.org"
                 )

  raise StandardError.new("Faulty paypal result: #{response}") unless ["VERIFIED", "INVALID"].include?(response)

  response == "VERIFIED"
end

#amountObject

This is the amount which you passed to paypal



87
88
89
# File 'lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb', line 87

def amount
  params['transaction[0].amount']
end

#complete?Boolean

Was the transaction complete?

Returns:

  • (Boolean)


49
50
51
# File 'lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb', line 49

def complete?
  status == "COMPLETED"
end

#invoiceObject

This is the invoice which you passed to paypal



82
83
84
# File 'lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb', line 82

def invoice
  params['transaction[0].invoiceId']
end

#item_idObject

This is the item number which we submitted to paypal The custom field is also mapped to item_id because PayPal doesn’t return item_number in dispute notifications



77
78
79
# File 'lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb', line 77

def item_id
  params['item_number'] || params['custom']
end

#statusObject

Status of transaction. List of possible values:

CREATED
COMPLETED
INCOMPLETE
ERROR
REVERSALERROR
PROCESSING
PENDING


61
62
63
# File 'lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb', line 61

def status
  params['status']
end

#test?Boolean

Was this a test transaction?

Returns:

  • (Boolean)


92
93
94
# File 'lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb', line 92

def test?
  params['test_ipn'] == '1'
end

#transaction_idObject

Id of this transaction (paypal number)



66
67
68
# File 'lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb', line 66

def transaction_id
  params['transaction[0].id_for_sender_txn']
end

#typeObject



70
71
72
# File 'lib/active_merchant/billing/integrations/paypal_adaptive_payment/notification.rb', line 70

def type
  params['action_type']
end