Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pay multiple people #2

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pkg/*
*.gem
.bundle
.rvmrc*
.DS_Store
38 changes: 38 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
PATH
remote: .
specs:
paypal-ipn (0.0.2)
httparty

GEM
remote: http://rubygems.org/
specs:
addressable (2.3.5)
crack (0.4.1)
safe_yaml (~> 0.9.0)
diff-lcs (1.2.5)
httparty (0.12.0)
json (~> 1.8)
multi_xml (>= 0.5.2)
json (1.8.1)
multi_xml (0.5.5)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
safe_yaml (0.9.7)
webmock (1.17.1)
addressable (>= 2.2.7)
crack (>= 0.3.2)

PLATFORMS
ruby

DEPENDENCIES
paypal-ipn!
rspec
webmock
12 changes: 6 additions & 6 deletions lib/paypal/ipn/types/masspay.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module Paypal
module Ipn
module Masspay
def payment_status
params["status_1"] if params
def payment_status(i)
params["status_#{i}"] if params
end

def payment_unclaimed?
payment_status == "Unclaimed"
end

def txn_id
params["masspay_txn_id_1"] if params
def txn_id(i)
params["masspay_txn_id_#{i}"] if params
end
alias_method :transaction_id, :txn_id

private
def unique_id
params["unique_id_1"]
def unique_id(i)
params["unique_id_#{i}"]
end
end
end
Expand Down
107 changes: 83 additions & 24 deletions lib/paypal/masspay.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
module Paypal
module Masspay
include HTTParty

MASSPAY_LIMIT = 250

def self.pay(payments, payer_email, currency, email_subject = nil)
request_uri = URI.parse(Paypal.nvp_uri)
request_uri.scheme = "https" # force https

def self.masspay(payer_email, receiver_email, amount, currency, note, unique_id)
body = {
"METHOD" => "MassPay",
"VERSION" => "2.3",
"CURRENCYCODE" => currency,
"SUBJECT" => payer_email,
"USER" => Paypal.api_username,
"PWD" => Paypal.api_password,
"SIGNATURE" => Paypal.api_signature,
"RECEIVERTYPE" => "EmailAddress"
}
body.merge!("EMAILSUBJECT" => email_subject) if email_subject
response = ''

new_body = body.dup
Rails.logger.info(new_body)
payments.each_with_index do |payment, i|
new_body = new_body.merge({
"L_EMAIL#{i}" => payment[:email],
"L_AMT#{i}" => payment[:amount],
"L_UNIQUEID#{i}" => payment[:unique_id],
"L_NOTE#{i}" => payment[:note]
})
Rails.logger.info(new_body)
end

response = self.post(request_uri.to_s, :body => new_body).body

response
end

def self.masspay(payer_email, receiver_email, amount, currency, note, unique_id, email_subject = nil)
request_uri = URI.parse(Paypal.nvp_uri)
request_uri.scheme = "https" # force https

Expand All @@ -14,45 +50,68 @@ def self.masspay(payer_email, receiver_email, amount, currency, note, unique_id)
"USER" => Paypal.api_username,
"PWD" => Paypal.api_password,
"SIGNATURE" => Paypal.api_signature,
"RECEIVERTYPE" => "EmailAddress",
"L_EMAIL0" => receiver_email,
"L_AMT0" => amount,
"L_UNIQUEID0" => unique_id,
"L_NOTE0" => note
"RECEIVERTYPE" => "EmailAddress"
}
self.post(request_uri.to_s, :body => body).body
body.merge!("EMAILSUBJECT" => email_subject) if email_subject
response = ''
Rails.logger.info(receiver_email)
if receiver_email.is_a?(Array)
new_body = body.dup
Rails.logger.info(new_body)
(receiver_email.length.to_f / max).ceil.times do |group|
offset = group * MASSPAY_LIMIT
receiver_email[offset..(offset + MASSPAY_LIMIT - 1)].each_with_index do |email, i|
new_body = new_body.merge({
"L_EMAIL#{i}" => email,
"L_AMT#{i}" => amount.is_a?(Array) ? amount[i] : amount,
"L_UNIQUEID#{i}" => unique_id.is_a?(Array) ? unique_id[i] : unique_id,
"L_NOTE#{i}" => note.is_a?(Array) ? note[i] : note})
end
Rails.logger.info(new_body)
response = self.post(request_uri.to_s, :body => new_body).body
end
else
body = body.merge({
"L_EMAIL0" => receiver_email,
"L_AMT0" => amount,
"L_UNIQUEID0" => unique_id,
"L_NOTE0" => note})
response = self.post(request_uri.to_s, :body => body).body
end
response
end

def successful_payment?
payment_response["ACK"] == "Success"
end

def payment_error_type
case payment_response["L_ERRORCODE0"]
when "10002" || "10007"
:unauthorized
when "10321"
:insufficient_funds
else
:unknown
end
end

def payment_error_message
payment_response["L_LONGMESSAGE0"]
end

private
def masspay(payer_email, receiver_email, amount, currency, note, unique_id)
def masspay(payer_email, receiver_email, amount, currency, note, unique_id, email_subject = '')
Paypal::Masspay.masspay(
payer_email,
receiver_email,
amount,
currency,
note,
unique_id
unique_id,
email_subject
)
end

def payment_error_type
case payment_response["L_ERRORCODE0"]
when "10002" || "10007"
:unauthorized
when "10321"
:insufficient_funds
else
:unknown
end
end

def payment_error_message
payment_response["L_LONGMESSAGE0"]
end
end
end

2 changes: 1 addition & 1 deletion lib/paypal/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Paypal
VERSION = "0.0.2"
VERSION = "0.0.4"
end
7 changes: 5 additions & 2 deletions paypal-ipn.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ Gem::Specification.new do |s|
s.homepage = "https://github.com/dwilkie/paypal"
s.summary = %q{More than just IPNs}
s.description = %q{A ruby library for handling paypal api's including IPNs}
s.add_runtime_dependency("httparty")

s.rubyforge_project = "paypal"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

# Gemfile
s.add_runtime_dependency("httparty")
s.add_development_dependency 'rspec'
s.add_development_dependency 'webmock'
end