Paypal::Masspay
Paypal::Masspay is a Ruby library that uses PayPals MassPay API to send Mass Payments.
The MassPay API provides a SOAP and NVP (Name-Value Pair) interface, however this library uses the NVP interface exclusively.
You can read more about PayPals MassPay API at the following link: PayPal MassPay API Documentationn
A introduction about PayPals NVP interface in general can be found here: PayPal NVP API Overview
Usage
In a nutshell the Paypal::Masspay library lets you send multiple payments at once using the following schema:
Paypal::Masspay::Configuration.configure do |config|
config.environment = :production
config.username = 'your_paypal_username'
config.password = 'your_paypal_password'
config.signature = 'your_paypal_signature'
end
masspay = Paypal::Masspay.new(:currency => 'USD') do |bucket|
bucket.add_recipient(Paypal::Masspay::Recipient.new(:recipient_email => '[email protected]', :amount => 10.00, :unique_id => 1234, :note => 'hello'))
bucket.add_recipient(Paypal::Masspay::Recipient.new(:recipient_email => '[email protected]', :amount => 2.95, :unique_id => 222))
end
masspay.deliver
It also supports being used like this:
Paypal::Masspay::Configuration.configure do |config|
config.environment = :production
config.username = 'your_paypal_username'
config.password = 'your_paypal_password'
config.signature = 'your_paypal_signature'
end
recipients = [
Paypal::Masspay::Recipient.new(
:recipient_email => '[email protected]',
:amount => 10.00,
:unique_id => 1234,
:note => 'hello'),
Paypal::Masspay::Recipient.new(
:recipient_email => '[email protected]',
:amount => 2.95,
:unique_id => 222)
]
masspay = Paypal::Masspay.new(:currency => 'USD', :recipients => recipients)
masspay.deliver
This sends a post request to the PayPal NVP API endpoint containing the following data:
USER=your_paypal_username&
PWD=your_paypal_password&
SIGNATURE=your_paypal_signature&
VERSION=2.3&
METHOD=MassPay&
RECEIVERTYPE=EmailAddress&
L_EMAIL0=chunky@example.com&
L_AMT0=10.00&
L_UNIQUEID0=1234&
L_NOTE0=hello&
L_EMAIL1=bacon@example.com&
L_AMT1=2.95&
L_UNIQUEID1=222&
CURRENCYCODE=USD
You are also able to check whether or not the request was successfully sent to paypal, and can check any returned error/warnings using the following code:
masspay.successful? # ===> true/false
masspay.errors # ===> [ <Paypal::Masspay::Error>, <Paypal::Masspay::Error>, ... ]
masspay.errors[0].methods # ===> [ :code, :short_message, :long_message, :severity ]
masspay.errors[0].code # ===> 10000
masspay.errors[0]. # ===> "insufficient funds"