I've installed the gem and GitHub says to do. I've made a separate model for creating a payment part of the instructions but I'm not sure how to call making a payment from within my orders controller . Also I need to be able to set the Payment attributes dynamically from my order but I've tried a couple of different ways of trying to achieve this with no luck.
I've tried calling Payments.pay in my orders controller and I get an error saying the method pay doesn't exist but I do have the method in the Payments class. The partial code for the Payments class is:
class Payments < PayPal::SDK::REST::Payment
def pay
@payment = Payment.new({
:intent => "sale",
:payer => {
:payment_method => "credit_card",
:funding_instruments => [{
:credit_card => {
:type => "visa",
:number => "4567516310777851",
:expire_month => "11",
:expire_year => "2018",
:cvv2 => "874",
:first_name => order.first_name, # Need to do something similar to this but doesn't work
# etc...
end
end
To sum up what I need help with is how can I make a call to this method in another controller or model so the payment can be processed and also how can i set the fields dynamically?
I see a few potential issues.
First of all, you're using the method as a class method, so it should be
def self.pay end
in order for Payments.pay to work. The way you define it currently is as an instance method.
If you wanted to call this method in a controller and pass in field dynamically you'd need to edit the pay function to accept arguments. And possible import this class in to the controller.
You might just want to create a before_action :pay callback in one of your controllers and define the method / call Paypal SDK like below. This would allow you to grab params and use them in the method (although I don't think you should be handling CC numbers, Paypal will probably handle this and pass you some sort of token that you then use). Also look out for the way Paypal namespaces the library, it can be tricky when you're calling Paypal::SDK::REST etc.
class PaymentsController < ApplicationController
before_action :pay
def create
@order = Order.find(params[:id])
end
private
def pay
@payment = PayPal::SDK::REST::Payment.new({
:intent => "sale",
:payer => {
:payment_method => "credit_card",
:funding_instruments => [{
:credit_card => {
:type => "visa",
:number => params[:number],
:expire_month => params[:month],
:expire_year => params[:year],
:cvv2 => params[:cvv],
:first_name => @order.first_name, # Need to do something similar to this but doesn't work
# etc...
end
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.