Module: Shoppe::Paypal::OrderExtensions

Defined in:
lib/shoppe/paypal/order_extensions.rb

Instance Method Summary collapse

Instance Method Details

#accept_paypal_payment(payment_id, token, payer_id) ⇒ Object

Accept a PayPal payment after redirected back to the Rails app



29
30
31
32
33
34
35
36
# File 'lib/shoppe/paypal/order_extensions.rb', line 29

def accept_paypal_payment(payment_id, token, payer_id)
  self.payments.create(amount: self.total, method: "PayPal", reference: payment_id, refundable: true, confirmed: false)

  self.properties["paypal_payment_id"] = payment_id
  self.properties["paypal_payment_token"] = token
  self.properties["paypal_payer_id"] = payer_id
  self.save
end

#redirect_to_paypal(return_url, cancel_url) ⇒ Object

Create a PayPal payment and respond with the approval URL Requires return_url and cancel_url parameters



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/shoppe/paypal/order_extensions.rb', line 7

def redirect_to_paypal(return_url, cancel_url)
  @payment = PayPal::SDK::REST::Payment.new({
    :intent => "sale",
    :payer => {
      :payment_method => "paypal" },
    :redirect_urls => {
      :return_url => return_url,
      :cancel_url => cancel_url },
    :transactions => [ {
      :amount => {
        :total => '%.2f' % self.total,
        :currency => Shoppe::Paypal.currency },
      :description => "Total: #{ '%.2f' % self.total }#{Shoppe::Paypal.currency} for Order #{self.number}" } ] } )

  if @payment.create
    @payment.links.find{|v| v.method == "REDIRECT" }.href
  elsif @payment.error
    raise Shoppe::Errors::PaymentDeclined, "There was an error contacting the payment processor"
  end
end