Class: PayPal::ExpressCheckout::Request

Inherits:
Request
  • Object
show all
Defined in:
lib/paypal/express_checkout/request.rb

Constant Summary collapse

METHODS =
{
  :address_verify       => "AddressVerify",
  :authorize            => "DoAuthorization",
  :balance              => "GetBalance",
  :pal_details          => "GetPalDetails",
  :bill_outstanding     => "BillOutstandingAmount",
  :capture              => "DoCapture",
  :reauthorize          => "DoReauthorization",
  :checkout             => "SetExpressCheckout",
  :pay                  => "DoExpressCheckoutPayment",
  :details              => "GetExpressCheckoutDetails",
  :create_profile       => "CreateRecurringPaymentsProfile",
  :profile              => "GetRecurringPaymentsProfileDetails",
  :manage_profile       => "ManageRecurringPaymentsProfileStatus",
  :update_profile       => "UpdateRecurringPaymentsProfile",
  :refund               => "RefundTransaction",
  :search               => "TransactionSearch",
  :update_status        => "ManagePendingTransactionStatus",
  :transaction_details  => "GetTransactionDetails",
  :reference            => "DoReferenceTransaction",
  :callback_response    => "CallbackResponse"
}
INITIAL_AMOUNT_ACTIONS =
{
  :cancel   => "CancelOnFailure",
  :continue => "ContinueOnFailure"
}
ACTIONS =
{
  :accept     => "Accept",
  :deny       => "Deny",
  :cancel     => "Cancel",
  :suspend    => "Suspend",
  :reactivate => "Reactivate"
}
PERIOD =
{
  :daily        => "Day",
  :weekly       => "Week",
  :semi_monthly => "SemiMonth",
  :monthly      => "Month",
  :yearly       => "Year"
}
TRIAL_PERIOD =
{
  :daily        => "Day",
  :weekly       => "Week",
  :semi_monthly => "SemiMonth",
  :monthly      => "Month",
  :yearly       => "Year"
}
OUTSTANDING =
{
  :next_billing => "AddToNextBilling",
  :no_auto      => "NoAutoBill"
}
REFUND_TYPE =
{
  :full     => "Full",
  :partial  => "Partial",
  :external => "ExternalDispute",
  :other    => "Other"
}

Constants inherited from Request

Request::CA_FILE

Instance Method Summary collapse

Methods inherited from Request

#default_headers, #post, #prepare_headers

Instance Method Details

#api_methodsObject



105
106
107
# File 'lib/paypal/express_checkout/request.rb', line 105

def api_methods
  METHODS
end

#build_action(value) ⇒ Object

:nodoc:



148
149
150
# File 'lib/paypal/express_checkout/request.rb', line 148

def build_action(value) # :nodoc:
  ACTIONS.fetch(value.to_sym, value) if value
end

#build_auto_bill_outstanding(value) ⇒ Object

:nodoc:



140
141
142
# File 'lib/paypal/express_checkout/request.rb', line 140

def build_auto_bill_outstanding(value) # :nodoc:
  OUTSTANDING.fetch(value.to_sym, value) if value
end

#build_billings(attr_name, values, buffer) ⇒ Object



179
180
181
# File 'lib/paypal/express_checkout/request.rb', line 179

def build_billings(attr_name, values, buffer)
  build_object_array(attr_name, values, buffer, "L_", "n")
end

#build_end_date(value) ⇒ Object

:nodoc:



136
137
138
# File 'lib/paypal/express_checkout/request.rb', line 136

def build_end_date(value) # :nodoc:
  build_time(value)
end

#build_failed_initial_action(value) ⇒ Object

:nodoc:



152
153
154
# File 'lib/paypal/express_checkout/request.rb', line 152

def build_failed_initial_action(value) # :nodoc:
  INITIAL_AMOUNT_ACTIONS.fetch(value.to_sym, value) if value
end

#build_locale(value) ⇒ Object

:nodoc:



156
157
158
# File 'lib/paypal/express_checkout/request.rb', line 156

def build_locale(value) # :nodoc:
  value.to_s.upcase
end

#build_object_array(attr_name, values, buffer, prefix = "L_", suffix = nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/paypal/express_checkout/request.rb', line 200

def build_object_array(attr_name, values, buffer, prefix = "L_", suffix = nil)
  Array(values).each_with_index do |value, idx|
    attr_name.each do |attribute, name|
      name = "#{prefix}#{name}#{suffix}"
      unless (v = value.send(attribute)).nil?
        buffer[name.gsub(/n/, idx.to_s).to_sym] = v
      end
    end
  end
end

#build_payment_items(attr_name, values, buffer) ⇒ Object



196
197
198
# File 'lib/paypal/express_checkout/request.rb', line 196

def build_payment_items(attr_name, values, buffer)
  build_object_array(attr_name, values, buffer, "L_", "n")
end

#build_payments(attr_name, values, buffer) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/paypal/express_checkout/request.rb', line 183

def build_payments(attr_name, values, buffer)
  build_object_array(attr_name, values, buffer, "PAYMENTREQUEST_n_")
  Array(values).each_with_index do |payment, idx|
    build_object_array(
      PayPal::ExpressCheckout::Fields::ASSOCIATIONS[:payment_items],
      payment.payment_items,
      buffer,
      "L_PAYMENTREQUEST_#{idx}_",
      "n"
    )
  end
end

#build_period(value) ⇒ Object

:nodoc:



120
121
122
# File 'lib/paypal/express_checkout/request.rb', line 120

def build_period(value) # :nodoc:
  PERIOD.fetch(value.to_sym, value) if value
end

#build_refund_type(value) ⇒ Object

:nodoc:



144
145
146
# File 'lib/paypal/express_checkout/request.rb', line 144

def build_refund_type(value) # :nodoc:
  REFUND_TYPE.fetch(value.to_sym, value) if value
end

#build_shipping_options(attr_name, values, buffer) ⇒ Object



175
176
177
# File 'lib/paypal/express_checkout/request.rb', line 175

def build_shipping_options(attr_name, values, buffer)
  build_object_array(attr_name, values, buffer, "L_", "n")
end

#build_start_at(value) ⇒ Object

:nodoc:



128
129
130
# File 'lib/paypal/express_checkout/request.rb', line 128

def build_start_at(value) # :nodoc:
  build_time(value)
end

#build_start_date(value) ⇒ Object

:nodoc:



132
133
134
# File 'lib/paypal/express_checkout/request.rb', line 132

def build_start_date(value) # :nodoc:
  build_time(value)
end

#build_survey_choices(attr_name, values, buffer) ⇒ Object



160
161
162
163
164
# File 'lib/paypal/express_checkout/request.rb', line 160

def build_survey_choices(attr_name, values, buffer)
  Array(values).each_with_index do |value, idx|
    buffer[attr_name.gsub(/n/, idx.to_s).to_sym] = value
  end
end

#build_time(value) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/paypal/express_checkout/request.rb', line 166

def build_time(value)
  if value.respond_to?(:strftime)
    value = value.utc if value.respond_to?(:utc)
    value.strftime("%Y-%m-%dT%H:%M:%SZ")
  else
    value
  end
end

#build_trial_period(value) ⇒ Object



124
125
126
# File 'lib/paypal/express_checkout/request.rb', line 124

def build_trial_period(value)
  TRIAL_PERIOD.fetch(value.to_sym, value) if value
end

#default_paramsObject



68
69
70
71
72
73
74
75
# File 'lib/paypal/express_checkout/request.rb', line 68

def default_params
  {
    :USER         => PayPal::ExpressCheckout::Api.username,
    :PWD          => PayPal::ExpressCheckout::Api.password,
    :SIGNATURE    => PayPal::ExpressCheckout::Api.signature,
    :VERSION      => PayPal::ExpressCheckout::Api.api_version
  }
end

#field_map(object) ⇒ Object



77
78
79
# File 'lib/paypal/express_checkout/request.rb', line 77

def field_map(object)
  object.class.field_map
end

#normalize_params(object, params) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/paypal/express_checkout/request.rb', line 81

def normalize_params(object, params)
  attributes = field_map(object)
  params.inject({}) do |buffer, (name, value)|
    attr_names = [attributes[name.to_sym]].flatten.compact
    attr_names << name if attr_names.empty?

    attr_names.each do |attr_name|
      if (attr_name =~ /n/ or attr_name.is_a?(Hash)) and respond_to?("build_#{name}")
        send("build_#{name}", attr_name, value, buffer)
      else
        buffer[attr_name.to_sym] = respond_to?("build_#{name}") ? send("build_#{name}", value) : value
      end
    end

    buffer
  end
end

#prepare_params(object, params) ⇒ Object

:nodoc:



116
117
118
# File 'lib/paypal/express_checkout/request.rb', line 116

def prepare_params(object, params) # :nodoc:
  normalize_params(object, default_params.merge(params))
end

#run(object, method, params = {}, headers = {}) ⇒ Object



109
110
111
112
113
114
# File 'lib/paypal/express_checkout/request.rb', line 109

def run(object, method, params = {}, headers = {})
  params = prepare_params(object, params.merge(:method => api_methods.fetch(method, method.to_s)))
  headers = prepare_headers(headers)
  response = post(params, headers)
  PayPal::ExpressCheckout::Response.process(method, response)
end

#uriObject

Parse current API url.



101
102
103
# File 'lib/paypal/express_checkout/request.rb', line 101

def uri # :nodoc:
  @uri ||= PayPal::ExpressCheckout::Api.api_endpoint
end