Class: PaymentRecipes::PayPal::REST::Action::CreatePayment

Inherits:
Utils::Action
  • Object
show all
Includes:
Utils::Converters
Defined in:
lib/payment_recipes/paypal/rest/action/create_payment.rb

Instance Method Summary collapse

Methods included from Utils::Converters

#convert_to_money, #convert_to_string, #convert_to_symbol, #convert_to_time

Methods inherited from Utils::Action

#debug, debug, #ensure_presence, #execute, #failure?, #get, #initialize, #make_instance_variables_available, prepare, rules, #set, #store, #stored_variables, #success?, variable

Constructor Details

This class inherits a constructor from PaymentRecipes::Utils::Action

Instance Method Details

#authorizationObject


49
50
51
52
53
# File 'lib/payment_recipes/paypal/rest/action/create_payment.rb', line 49

def authorization
  return nil unless payment

  payment.authorization
end

#create_paypal_payment!Object


24
25
26
27
28
29
30
31
# File 'lib/payment_recipes/paypal/rest/action/create_payment.rb', line 24

def create_paypal_payment!
  store(:paypal_payment) do
    new_paypal_payment = ::PayPal::SDK::REST::Payment.new(payment_attributes)
    new_paypal_payment.create

    new_paypal_payment
  end
end

#find_payment!Object


33
34
35
36
37
# File 'lib/payment_recipes/paypal/rest/action/create_payment.rb', line 33

def find_payment!
  store(:payment) do
    ::PaymentRecipes::PayPal::REST::Payment.find(paypal_payment.id)
  end
end

#payment_attributesObject


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/payment_recipes/paypal/rest/action/create_payment.rb', line 55

def payment_attributes
  {
    "intent" => convert_to_string(intent),
    "transactions" => [
      {
        "amount" => {
          "total" => amount.to_s,
          "currency" => amount.currency.iso_code
        },
        "description" => description
      }
    ]
  }.merge(attributes)
end

#performObject


13
14
15
16
17
18
19
20
21
22
# File 'lib/payment_recipes/paypal/rest/action/create_payment.rb', line 13

def perform
  unless [:sale, :authorize].include?(intent)
    raise Exception, "Allowed values for intent: :sale, :authorize"
  end

  create_paypal_payment!
  find_payment!

  payment
end

#redirect_urlObject


39
40
41
# File 'lib/payment_recipes/paypal/rest/action/create_payment.rb', line 39

def redirect_url
  paypal_payment.links.select { |link| link.rel == 'approval_url' }.first.href rescue nil
end

#saleObject


43
44
45
46
47
# File 'lib/payment_recipes/paypal/rest/action/create_payment.rb', line 43

def sale
  return nil unless payment

  payment.sale
end