Module: EZPaypal::Request

Defined in:
lib/core/request.rb

Class Method Summary (collapse)

Class Method Details

+ (Hash) ConfirmPurchase(token, payer_id)

Confirm payment, only works if token and payer_id is obtained

Parameters:

  • token (String)
  • payer_id (String)

Returns:

  • (Hash)

    payment details associated with the given token and payer_id



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/core/request.rb', line 74

def self.ConfirmPurchase(token, payer_id)
  unless (token.nil? && payer_id.nil?)
    begin
      # Setup default options
      setting = EZPaypal::Config.Setting.clone
      setting.merge!({"METHOD" => "DoExpressCheckoutPayment"})
      setting.merge!({"PAYMENTREQUEST_0_PAYMENTACTION" => "Sale"})
      setting.merge!({"TOKEN" => token})
      setting.merge!({"PAYERID" => payer_id})


      # Get checkout details
      checkout_details_origin = EZPaypal::Request.GetCheckoutDetails(token)
      checkout_details_origin.each do |key, value|
        key = CGI::unescape(key)
        value = CGI::unescape(value)
      end

      # Submit checkout request to confirm the purchase
      payment_response_origin = HashWithIndifferentAccess.new()
      if (checkout_details_origin["ACK"].downcase == "success")
        checkout_details_origin.merge!(setting)
        query_string = EZPaypal::Helper.ConvertHashToQueryString(checkout_details_origin)
        payment_response_origin = EZHttp.Send(EZPaypal::Config.EndPoint["express_checkout_endpoint"], query_string, "post").body
      end

      # Convert response to Hash to return
      payment_response = EZPaypal::Helper.ConvertParamToHash(payment_response_origin)

      return payment_response

    rescue
      throw "Error occured in ConfirmPurchase: TOKEN=#{token}, PAYERID=#{payer_id}"
    end
  end
end

+ (Hash) CreateRecurringProfile(profile)

Create a recurring profile for a customer

Parameters:

Returns:

  • (Hash)

    profile creation confirmation from paypal



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/core/request.rb', line 114

def self.CreateRecurringProfile(profile)
  begin
    # Setup default options
    setting = EZPaypal::Config.Setting.clone
    default = {"METHOD" => "CreateRecurringPaymentsProfile"}
    setting.merge!(default).merge!(profile)

    # Http call to create profile and get response
    query_string = EZPaypal::Helper.ConvertHashToQueryString(setting)
    profile_response_origin = EZHttp.Send(EZPaypal::Config.EndPoint["express_checkout_endpoint"], query_string, "post").body

    # Convert response to Hash to return
    profile_response = EZPaypal::Helper.ConvertParamToHash(profile_response_origin)

    return profile_response
  rescue
    throw "Error occured in CreateRecurringProfile"
  end
end

+ (Hash) GetCheckoutDetails(token)

Get current checkout details, only works if token has obtained

Parameters:

  • token (String)

Returns:

  • (Hash)

    checkout details associated with the given token



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/core/request.rb', line 48

def self.GetCheckoutDetails(token)
  unless (token.nil?)
    begin
      # Setup default options
      setting = EZPaypal::Config.Setting.clone
      setting.merge!({"METHOD" => "GetExpressCheckoutDetails"})
      setting.merge!({"TOKEN" => token})

      # Get checkout details
      query_string = EZPaypal::Helper.ConvertHashToQueryString(setting)
      checkout_details_origin = EZHttp.Send(EZPaypal::Config.EndPoint["express_checkout_endpoint"], query_string, "post").body

      # Convert checkout details to Hash to return
      checkout_details = EZPaypal::Helper.ConvertParamToHash(checkout_details_origin)

      return checkout_details
    rescue
      throw "Error occured in GetExpressCheckoutDetails: token=#{token}"
    end
  end
end

+ (String) GetCheckoutURL(token)

Get current checkout url to redirect user to, only works if token has obtained

Parameters:

  • token (String)

Returns:

  • (String)

    paypal checkout url to redirect user to



41
42
43
# File 'lib/core/request.rb', line 41

def self.GetCheckoutURL(token)
  EZPaypal::Config.EndPoint["express_checkout_url"]+ "&token=" + token unless token.nil?
end

+ (Hash) Refund(transaction_id, refund_type, amount, currency, note)

Refund money to a transaction

Parameters:

  • all (String)

Returns:

  • (Hash)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/core/request.rb', line 137

def self.Refund(transaction_id, refund_type, amount, currency, note)
  # Setup default options
  setting = EZPaypal::Config.Setting.clone
  options = {
      "METHOD" => "RefundTransaction",
      "TRANSACTIONID" => transaction_id,
      "REFUNDTYPE" => refund_type || "Partial",
      "AMT" => amount || "0",
      "CURRENCYCODE" => currency || "USD",
      "NOTE" => note || ""
  }
  setting.merge!(options)

  # Http call to create profile and get response
  query_string = EZPaypal::Helper.ConvertHashToQueryString(setting)
  refund_response_origin = EZHttp.Send(EZPaypal::Config.EndPoint["express_checkout_endpoint"], query_string, "post").body

  # Convert response to Hash to return
  profile_response = EZPaypal::Helper.ConvertParamToHash(refund_response_origin)
end

+ (Hash) SetupExpressCheckout(cart, returnUrl, cancelUrl)

Setup express checkout cart

Parameters:

Returns:

  • (Hash)

    response = "TOKEN"



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/core/request.rb', line 15

def self.SetupExpressCheckout (cart, returnUrl, cancelUrl)
  begin
    # Setup default options
    setting = EZPaypal::Config.Setting.clone
    default = {"METHOD" => "SetExpressCheckout",
               "RETURNURL" => returnUrl,
               "CANCELURL" => cancelUrl
    }
    setting.merge!(default).merge!(cart)

    # Get setup express checkout details
    query_string = EZPaypal::Helper.ConvertHashToQueryString(setting)
    setup_ec_response_origin = EZHttp.Send(EZPaypal::Config.EndPoint["express_checkout_endpoint"], query_string, "post").body

    # Convert express checkout details to Hash to return
    setup_ec_response = EZPaypal::Helper.ConvertParamToHash(setup_ec_response_origin)

    return setup_ec_response
  rescue
    throw "Error occured in SetupExpressCheckout"
  end
end