Class: PayPal::Recurring::Request

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

Constant Summary collapse

METHODS =
{
  :checkout       => "SetExpressCheckout",
  :payment        => "DoExpressCheckoutPayment",
  :details        => "GetExpressCheckoutDetails",
  :create_profile => "CreateRecurringPaymentsProfile",
  :profile        => "GetRecurringPaymentsProfileDetails",
  :manage_profile => "ManageRecurringPaymentsProfileStatus",
  :update_profile => "UpdateRecurringPaymentsProfile",
  :refund         => "RefundTransaction"
}
INITIAL_AMOUNT_ACTIONS =
{
  :cancel   => "CancelOnFailure",
  :continue => "ContinueOnFailure"
}
ACTIONS =
{
  :cancel     => "Cancel",
  :suspend    => "Suspend",
  :reactivate => "Reactivate"
}
PERIOD =
{
  :daily   => "Day",
  :weekly  => "Weekly",
  :monthly => "Month",
  :yearly  => "Year"
}
TRIAL_PERIOD =
{
  :daily    => "Day",
  :weekly   => "Weekly",
  :monthly  => "Month",
  :yearly   => "Year"
}
OUTSTANDING =
{
  :next_billing => "AddToNextBilling",
  :no_auto      => "NoAutoBill"
}
REFUND_TYPE =
{
  :full     => "Full",
  :partial  => "Partial",
  :external => "ExternalDispute",
  :other    => "Other"
}
ATTRIBUTES =
{
  :action                => "ACTION",
  :amount                => ["PAYMENTREQUEST_0_AMT", "AMT"],
  :billing_type          => "L_BILLINGTYPE0",
  :cancel_url            => "CANCELURL",
  :currency              => ["PAYMENTREQUEST_0_CURRENCYCODE", "CURRENCYCODE"],
  :description           => ["DESC", "PAYMENTREQUEST_0_DESC", "L_BILLINGAGREEMENTDESCRIPTION0"],
  :note                  => "NOTE",
  :item_category         => "L_PAYMENTREQUEST_0_ITEMCATEGORY0",
  :item_name             => "L_PAYMENTREQUEST_0_NAME0",
  :item_amount           => "L_PAYMENTREQUEST_0_AMT0",
  :item_quantity         => "L_PAYMENTREQUEST_0_QTY0",
  :email                 => "EMAIL",
  :failed                => "MAXFAILEDPAYMENTS",
  :frequency             => "BILLINGFREQUENCY",
  :initial_amount        => "INITAMT",
  :initial_amount_action => "FAILEDINITAMTACTION",
  :ipn_url               => ["PAYMENTREQUEST_0_NOTIFYURL", "NOTIFYURL"],
  :locale                => "LOCALECODE",
  :method                => "METHOD",
  :no_shipping           => "NOSHIPPING",
  :outstanding           => "AUTOBILLOUTAMT",
  :password              => "PWD",
  :payer_id              => "PAYERID",
  :payment_action        => "PAYMENTREQUEST_0_PAYMENTACTION",
  :period                => "BILLINGPERIOD",
  :profile_id            => "PROFILEID",
  :reference             => ["PROFILEREFERENCE", "PAYMENTREQUEST_0_CUSTOM", "PAYMENTREQUEST_0_INVNUM"],
  :refund_type           => "REFUNDTYPE",
  :return_url            => "RETURNURL",
  :signature             => "SIGNATURE",
  :start_at              => "PROFILESTARTDATE",
  :token                 => "TOKEN",
  :transaction_id        => "TRANSACTIONID",
  :trial_amount          => "TRIALAMT",
  :trial_frequency       => "TRIALBILLINGFREQUENCY",
  :trial_length          => "TRIALTOTALBILLINGCYCLES",
  :trial_period          => "TRIALBILLINGPERIOD",
  :username              => "USER",
  :version               => "VERSION"
}
CA_FILE =
File.dirname(__FILE__) + "/cacert.pem"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#uriObject

Parse current API url.



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

def uri
  @uri
end

Instance Method Details

#build_action(value) ⇒ Object

:nodoc:



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

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

#build_initial_amount_action(value) ⇒ Object

:nodoc:



200
201
202
# File 'lib/paypal/recurring/request.rb', line 200

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

#build_locale(value) ⇒ Object

:nodoc:



204
205
206
# File 'lib/paypal/recurring/request.rb', line 204

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

#build_outstanding(value) ⇒ Object

:nodoc:



188
189
190
# File 'lib/paypal/recurring/request.rb', line 188

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

#build_period(value) ⇒ Object

:nodoc:



176
177
178
# File 'lib/paypal/recurring/request.rb', line 176

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

#build_refund_type(value) ⇒ Object

:nodoc:



192
193
194
# File 'lib/paypal/recurring/request.rb', line 192

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

#build_start_at(value) ⇒ Object

:nodoc:



184
185
186
# File 'lib/paypal/recurring/request.rb', line 184

def build_start_at(value) # :nodoc:
  value.respond_to?(:strftime) ? value.strftime("%Y-%m-%dT%H:%M:%SZ") : value
end

#build_trial_period(value) ⇒ Object



180
181
182
# File 'lib/paypal/recurring/request.rb', line 180

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

#clientObject



144
145
146
147
148
149
150
151
152
# File 'lib/paypal/recurring/request.rb', line 144

def client
  @client ||= begin
    Net::HTTP.new(uri.host, uri.port).tap do |http|
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.ca_file = CA_FILE
    end
  end
end

#default_paramsObject



154
155
156
157
158
159
160
161
# File 'lib/paypal/recurring/request.rb', line 154

def default_params
  {
    :username    => PayPal::Recurring.username,
    :password    => PayPal::Recurring.password,
    :signature   => PayPal::Recurring.signature,
    :version     => PayPal::Recurring.api_version
  }
end

#normalize_params(params) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/paypal/recurring/request.rb', line 163

def normalize_params(params)
  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|
      buffer[attr_name.to_sym] = respond_to?("build_#{name}") ? send("build_#{name}", value) : value
    end

    buffer
  end
end

#post(params = {}) ⇒ Object



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

def post(params = {})
  request.form_data = params
  client.request(request)
end

#prepare_params(params) ⇒ Object

Join params and normalize attribute names.



134
135
136
# File 'lib/paypal/recurring/request.rb', line 134

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

#requestObject



119
120
121
122
123
# File 'lib/paypal/recurring/request.rb', line 119

def request
  @request ||= Net::HTTP::Post.new(uri.request_uri).tap do |http|
    http["User-Agent"] = "PayPal::Recurring/#{PayPal::Recurring::Version::STRING}"
  end
end

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

Do a POST request to PayPal API. The method argument is the name of the API method you want to invoke. For instance, if you want to request a new checkout token, you may want to do something like:

response = request.run(:express_checkout)

We normalize the methods name. For a list of what’s being covered, refer to PayPal::Recurring::Request::METHODS constant.

The params hash can use normalized names. For a list, check the PayPal::Recurring::Request::ATTRIBUTES constant.



111
112
113
114
115
# File 'lib/paypal/recurring/request.rb', line 111

def run(method, params = {})
  params = prepare_params(params.merge(:method => METHODS.fetch(method, method.to_s)))
  response = post(params)
  Response.process(method, response)
end