Sha256: b83a4b62c7a7a267dfe0be3756cf5159ecf29b24a4a3ba5986883aade68fc85d

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module Shoppe
  module Paypal
    module OrderExtensions
      
      # Create a PayPal payment and respond with the approval URL
      # Requires return_url and cancel_url parameters
      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 => "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

      # Accept a PayPal payment after redirected back to the Rails app
      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

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shoppe-paypal-1.0.0 lib/shoppe/paypal/order_extensions.rb