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",
  :brand_name            => "BRANDNAME",
  :page_style            => "PAGESTYLE",
}
CA_FILE =
File.dirname(__FILE__) + "/cacert.pem"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#uriObject

Parse current API url.



142
143
144
# File 'lib/paypal/recurring/request.rb', line 142

def uri
  @uri
end

Instance Method Details

#build_action(value) ⇒ Object

:nodoc:



198
199
200
# File 'lib/paypal/recurring/request.rb', line 198

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

#build_initial_amount_action(value) ⇒ Object

:nodoc:



202
203
204
# File 'lib/paypal/recurring/request.rb', line 202

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

#build_locale(value) ⇒ Object

:nodoc:



206
207
208
# File 'lib/paypal/recurring/request.rb', line 206

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

#build_outstanding(value) ⇒ Object

:nodoc:



190
191
192
# File 'lib/paypal/recurring/request.rb', line 190

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

#build_period(value) ⇒ Object

:nodoc:



178
179
180
# File 'lib/paypal/recurring/request.rb', line 178

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

#build_refund_type(value) ⇒ Object

:nodoc:



194
195
196
# File 'lib/paypal/recurring/request.rb', line 194

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

#build_start_at(value) ⇒ Object

:nodoc:



186
187
188
# File 'lib/paypal/recurring/request.rb', line 186

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



182
183
184
# File 'lib/paypal/recurring/request.rb', line 182

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

#clientObject



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

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



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

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



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

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



129
130
131
132
# File 'lib/paypal/recurring/request.rb', line 129

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

#prepare_params(params) ⇒ Object

Join params and normalize attribute names.



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

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

#requestObject



121
122
123
124
125
# File 'lib/paypal/recurring/request.rb', line 121

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.



113
114
115
116
117
# File 'lib/paypal/recurring/request.rb', line 113

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