class Paypal::Masspay

Constants

API_PATH
API_VERSION
CURRENCY_MAX_LENGTH
DEFAULT_CURRENCY
DEFAULT_SUBJECT
IDENTIFIER
MAX_PER_REQUEST
METHOD
PRODUCTION_API_URL
SANDBOX_API_URL
SUBJECT_MAX_LENGTH
VERSION

Attributes

errors[R]

Public Class Methods

new(options = {}) { |self| ... } click to toggle source
# File lib/paypal/masspay.rb, line 28
def initialize(options = {}, &block)
  build_options(options)

  @success   = true
  @delivered = false
  @errors    = []

  yield(self) if block_given?
end

Public Instance Methods

add_recipient(recipient) click to toggle source
# File lib/paypal/masspay.rb, line 38
def add_recipient(recipient)
  @recipients << recipient
end
deliver() click to toggle source
# File lib/paypal/masspay.rb, line 42
def deliver
  validate

  connection = create_connection

  @recipients.each_slice(MAX_PER_REQUEST) do |recipients_slice|
    response = connection.post(API_PATH, request_data(recipients_slice))
    check_response(response)
  end

  @delivered = true
end
delivered?() click to toggle source
# File lib/paypal/masspay.rb, line 59
def delivered?
  @delivered
end
successful?() click to toggle source
# File lib/paypal/masspay.rb, line 55
def successful?
  @success
end

Private Instance Methods

api_url() click to toggle source
# File lib/paypal/masspay.rb, line 77
def api_url
  if Paypal::Masspay::Configuration.environment == :production
    PRODUCTION_API_URL
  else
    SANDBOX_API_URL
  end
end
build_options(options) click to toggle source
# File lib/paypal/masspay.rb, line 99
def build_options(options)
  @recipients  = options[:recipients] || []
  @currency    = options[:currency]   || DEFAULT_CURRENCY
  @subject     = options[:subject]    || DEFAULT_SUBJECT
end
build_payments(recipients) click to toggle source
# File lib/paypal/masspay.rb, line 105
def build_payments(recipients)
  Hash.new({}).tap do |output|
    recipients.each_with_index do |payment, index|
      output.merge!(payment.get_params(index))
    end
  end
end
check_response(response) click to toggle source
# File lib/paypal/masspay.rb, line 85
def check_response(response)
  response = Paypal::Masspay::ResponseParser.new(response.body)
  if response.successful?
    @success = true && @success
  else
    @success = false
    @errors.concat(response.errors)
  end
end
create_connection() click to toggle source
# File lib/paypal/masspay.rb, line 70
def create_connection
  Faraday.new(:url => api_url) do |con|
    con.use(Faraday::Request::UrlEncoded)
    con.use(Faraday::Adapter::NetHttp)
  end
end
credentials() click to toggle source
# File lib/paypal/masspay.rb, line 113
def credentials
  {
    'USER'      => Paypal::Masspay::Configuration.username,
    'PWD'       => Paypal::Masspay::Configuration.password,
    'SIGNATURE' => Paypal::Masspay::Configuration.signature
  }
end
default_options() click to toggle source
# File lib/paypal/masspay.rb, line 121
def default_options
  {
    'VERSION'      => API_VERSION,
    'METHOD'       => METHOD,
    'RECEIVERTYPE' => IDENTIFIER,
    'CURRENCYCODE' => @currency,
    'EMAILSUBJECT' => @subject
  }
end
request_data(recipients) click to toggle source
# File lib/paypal/masspay.rb, line 95
def request_data(recipients)
  (default_options.merge(credentials).merge(build_payments(recipients))).delete_if { |k, v| v.nil? }
end
validate() click to toggle source
# File lib/paypal/masspay.rb, line 65
def validate
  Paypal::Masspay::LengthValidator.validate(@currency, CURRENCY_MAX_LENGTH, :currency)
  Paypal::Masspay::LengthValidator.validate(@subject, SUBJECT_MAX_LENGTH, :subject)
end