Class: TradoPaypalModule::Paypaler
- Inherits:
-
Object
- Object
- TradoPaypalModule::Paypaler
- Defined in:
- lib/trado_paypal_module/paypaler.rb
Class Method Summary collapse
-
.assign_paypal_token(token, payer_id, order) ⇒ Object
Assign PayPal token to order after user logs into their account.
-
.build(cart, order, ip_address) ⇒ String
Builds the a PayPal purchase request from the order data If successful, redirect to PayPal for the user to login If unsuccessful, redirect to failed order page.
-
.complete(order, session, ip_address) ⇒ Object
Completes the order process by communicating with PayPal; receives a response and in turn creates the relevant transaction records, sends a confirmation email and redirects the user.
-
.currency_codes ⇒ Array
A list of available currency codes for the PayPal payment system.
-
.express_items(items) ⇒ Array
Creates an aray of items which represent cart_items or order_items This is passed into the express_setup_options and express_purchase_options methods.
-
.express_purchase_options(order, ip_address) ⇒ Object
Creates the payment information object for PayPal to parse in the confirmation step and complete the purchase.
-
.express_setup_options(order, cart, ip_address, return_url, cancel_url) ⇒ Object
Creates the payment information object for PayPal to parse in the login.
-
.failed(response, order) ⇒ Object
When an order has failed to complete, a new transaction record is created with a logged status reason.
-
.fatal_error_code?(error_code) ⇒ Boolean
A list of fatal error codes for an order If the passed in error code parameter is included in the fatal codes array, return true.
-
.successful(response, order) ⇒ Object
Upon successfully completing an order with a PayPal payment option a new transaction record is created, stock is updated for the relevant SKU.
- .valid_tokens?(params) ⇒ Boolean
Class Method Details
.assign_paypal_token(token, payer_id, order) ⇒ Object
Assign PayPal token to order after user logs into their account
99 100 101 102 103 |
# File 'lib/trado_paypal_module/paypaler.rb', line 99 def self.assign_paypal_token token, payer_id, order order.paypal_express_token = token order.paypal_express_payer_id = payer_id order.save(validate: false) end |
.build(cart, order, ip_address) ⇒ String
Builds the a PayPal purchase request from the order data If successful, redirect to PayPal for the user to login If unsuccessful, redirect to failed order page
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/trado_paypal_module/paypaler.rb', line 15 def self.build cart, order, ip_address response = EXPRESS_GATEWAY.setup_purchase( Store::Price.new(price: order.gross_amount, tax_type: 'net').singularize, TradoPaypalModule::Paypaler.( order, cart, ip_address, Rails.application.routes.url_helpers.confirm_order_url(order, host: Trado::Application.config.action_mailer.[:host]), Rails.application.routes.url_helpers.mycart_carts_url(host: Trado::Application.config.action_mailer.[:host]) ) ) if response.success? return EXPRESS_GATEWAY.redirect_url_for(response.token) else TradoPaypalModule::Paypaler.failed(response, order) Payatron4000.decommission_order(order) return Rails.application.routes.url_helpers.failed_order_url(order, host: Trado::Application.config.action_mailer.[:host]) end end |
.complete(order, session, ip_address) ⇒ Object
Completes the order process by communicating with PayPal; receives a response and in turn creates the relevant transaction records, sends a confirmation email and redirects the user.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/trado_paypal_module/paypaler.rb', line 110 def self.complete order, session, ip_address order.transfer(order.cart) response = EXPRESS_GATEWAY.purchase(Store::Price.new(price: order.gross_amount, tax_type: 'net').singularize, TradoPaypalModule::Paypaler.(order, ip_address) ) if response.success? TradoPaypalModule::Paypaler.successful(response, order) Payatron4000.destroy_cart(session) Payatron4000.decommission_order(order) order.reload Mailatron4000::Orders.confirmation_email(order) return Rails.application.routes.url_helpers.success_order_url(order, host: Trado::Application.config.action_mailer.[:host]) else TradoPaypalModule::Paypaler.failed(response, order) order.reload Mailatron4000::Orders.confirmation_email(order) return Rails.application.routes.url_helpers.failed_order_url(order, host: Trado::Application.config.action_mailer.[:host]) end end |
.currency_codes ⇒ Array
A list of available currency codes for the PayPal payment system
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/trado_paypal_module/paypaler.rb', line 174 def self.currency_codes return [ "AUD", "CAD", "CZK", "DKK", "EUR", "HKD", "HUF", "ILS", "JPY", "MXN", "NOK", "NZD", "PHP", "PLN", "GBP", "RUB", "SGD", "SEK", "CHF", "TWD", "THB", "USD" ] end |
.express_items(items) ⇒ Array
Creates an aray of items which represent cart_items or order_items This is passed into the express_setup_options and express_purchase_options methods
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/trado_paypal_module/paypaler.rb', line 83 def self.express_items items items.collect do |item| { :name => "#{item.sku.product.name} (#{item.sku.variants.map{|v| v.name.titleize}.join(' / ')})", :description => "#{item.sku.product.name} (#{item.sku.variants.map{|v| v.name.titleize}.join(' / ')})", :amount => Store::Price.new(price: item.price, tax_type: 'net').singularize, :quantity => item.quantity } end end |
.express_purchase_options(order, ip_address) ⇒ Object
Creates the payment information object for PayPal to parse in the confirmation step and complete the purchase
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/trado_paypal_module/paypaler.rb', line 65 def self. order, ip_address { :subtotal => Store::Price.new(price: (order.net_amount - order.delivery.price), tax_type: 'net').singularize, :shipping => Store::Price.new(price: order.delivery.price, tax_type: 'net').singularize, :tax => Store::Price.new(price: order.tax_amount, tax_type: 'net').singularize, :handling => 0, :items => TradoPaypalModule::Paypaler.express_items(order.order_items), :token => order.paypal_express_token, :payer_id => order.paypal_express_payer_id, :currency => Store.settings.currency_code, :ip => ip_address, } end |
.express_setup_options(order, cart, ip_address, return_url, cancel_url) ⇒ Object
Creates the payment information object for PayPal to parse in the login
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/trado_paypal_module/paypaler.rb', line 43 def self. order, cart, ip_address, return_url, cancel_url { :subtotal => Store::Price.new(price: (order.net_amount - order.delivery.price), tax_type: 'net').singularize, :shipping => Store::Price.new(price: order.delivery.price, tax_type: 'net').singularize, :tax => Store::Price.new(price: order.tax_amount, tax_type: 'net').singularize, :handling => 0, :order_id => order.id, :items => TradoPaypalModule::Paypaler.express_items(cart.cart_items), :address_override => 1, :shipping_address => order.delivery_address.full_address, :req_confirm_shipping => 0, :ip => ip_address, :return_url => return_url, :cancel_return_url => cancel_url, :currency => Store.settings.currency_code, } end |
.failed(response, order) ⇒ Object
When an order has failed to complete, a new transaction record is created with a logged status reason
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/trado_paypal_module/paypaler.rb', line 155 def self.failed response, order Transaction.new( :fee => 0, :gross_amount => order.gross_amount, :order_id => order.id, :payment_status => 'failed', :transaction_type => 'Credit', :tax_amount => order.tax_amount, :paypal_id => nil, :payment_type => 'paypal', :net_amount => order.net_amount, :status_reason => response., :error_code => response.params["error_codes"].to_i ).save(validate: false) Payatron4000.increment_product_order_count(order.products) end |
.fatal_error_code?(error_code) ⇒ Boolean
A list of fatal error codes for an order If the passed in error code parameter is included in the fatal codes array, return true
206 207 208 209 210 211 212 213 |
# File 'lib/trado_paypal_module/paypaler.rb', line 206 def self.fatal_error_code? error_code @fatal_codes = [ 10412, # PayPal: Payment has already been made for this InvoiceID. 10415 # PayPal: A successful transaction has already been completed for this token. ] return @fatal_codes.include?(error_code) ? true : false end |
.successful(response, order) ⇒ Object
Upon successfully completing an order with a PayPal payment option a new transaction record is created, stock is updated for the relevant SKU
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/trado_paypal_module/paypaler.rb', line 134 def self.successful response, order Transaction.new( :fee => response.params['PaymentInfo']['FeeAmount'], :order_id => order.id, :payment_status => response.params['PaymentInfo']['PaymentStatus'].downcase, :transaction_type => 'Credit', :tax_amount => response.params['PaymentInfo']['TaxAmount'], :paypal_id => response.params['PaymentInfo']['TransactionID'], :payment_type => 'paypal', :net_amount => response.params['PaymentInfo']['GrossAmount'].to_d - response.params['PaymentInfo']['TaxAmount'].to_d, :gross_amount => response.params['PaymentInfo']['GrossAmount'], :status_reason => response.params['PaymentInfo']['PendingReason'] ).save(validate: false) Payatron4000.update_stock(order) Payatron4000.increment_product_order_count(order.products) end |
.valid_tokens?(params) ⇒ Boolean
215 216 217 |
# File 'lib/trado_paypal_module/paypaler.rb', line 215 def self.valid_tokens? params params[:token].present? && params[:PayerID].present? end |