Ruby on Rails Paypal REST API guest checkout -
i'm trying debug rails app developer. keep in mind have little experience both ruby , rails. goal allow users pay credit card when go paypal payment page without need registration.
the actual code in model/shopping-card.rb is:
include paypal::sdk::rest class shoppingcart def initialize(session) @session = session @session["delivery"] ||= "shipping" @session[:coupon] ||= {"token" => nil, "discount" => nil} @session[:shopping_cart] ||= [] end def delivery=(v) @session["delivery"] = v end def delivery @session["delivery"] end def should_add_shipping_costs? @session["delivery"] == "shipping" end def cart @session[:shopping_cart] end def discount val = @session["coupon"]["discount"] if val.nil? nil else val.to_f end end def token @session["coupon"]["token"] end def coupon=(c) @session["coupon"]["token"] = c.try("token") @session["coupon"]["discount"] = c.try("discount") end def subtotal_discount subtotal.to_f / 100 * discount.to_f end def subtotal_after_discount [(subtotal.to_f - subtotal_discount), 0].max end def subtotal_after_discount_with_vat subtotal_after_discount + subtotal_after_discount / 100 * 21 end def add(product, quantity) quantity = quantity.to_i index = cart.find_index |item| item["id"] == product.id end # not in chart if index.nil? item = product.to_cart item[:quantity] = quantity cart << item else item = cart[index] item["quantity"] = item["quantity"].to_i + quantity end end def remove(id) cart.delete_if |item| item["id"].to_s == id.to_s end end def clear cart.clear end def each(&block) cart.each(&block) end def each_with_index(&block) cart.each_with_index(&block) end def empty? cart.empty? end def to_paypal f = cart.map |item| { name: item["name"], price: "%.2f" % item["price_or_promo"], quantity: item["quantity"], currency: "eur", } end f end def subtotal amount = 0 each |item| amount += item["price_or_promo"].to_f * item["quantity"].to_i end "%.2f" % amount end def total_iva subtotal.to_f * 0.21 end def total_for(user) t = subtotal_after_discount.to_f + total_iva + surcharge_for(user) if self.should_add_shipping_costs? t + shipping_cost else t end end def surcharge_for(user) user.surcharge ? subtotal.to_f * 0.052 : 0 end def shipping_cost total = 0 each |item| total += (item['shipping'].to_f * item['quantity'].to_i) end total end def create_payment(description, user) payment = payment.new({ :intent => :sale, :payer => { :payment_method => :paypal }, :redirect_urls => { :return_url => env['paypal_return_url'], :cancel_url => env['paypal_cancel_url'] }, :transactions => [{ :item_list => { :items => { :name => description, :price => "%.2f" % total_for(user), :quantity => 1, :currency => "eur" } }, :amount => { :total => "%.2f" % total_for(user), :currency => "eur" }, :description => description }], }) if payment.create @session[:payment_id] = payment.id end payment end def build_order order = order.new(user_id: @session[:user_id]) each |item| order.operations << operation.new( product_id: item["id"], quantity: item["quantity"].to_i ) end order end def complete_payment(params) payment = payment.find(@session[:payment_id]) if payment.execute(payer_id: params[:payerid]) build_order.save! @session[:payment] = nil @session[:coupon] = nil clear end if(coupon = coupon.find_by(token: token)) coupon.used = true coupon.save! end payment end end
and in controllers/payment-controller.rb
include paypal::sdk::rest class paymentscontroller < applicationcontroller def checkout @payment = shopping_cart.create_payment("cultius purchase", current_user) if @payment.success? redirect_to @payment.links.find{|v| v.method == "redirect" }.href else logger.error "error while creating payment:" logger.error @payment.error.inspect end end def success shopping_cart.complete_payment(params) end end
so finally, speaking pp credit card option hast enabled checking option on pp account settings. option 'elegible buyers'.
Comments
Post a Comment