ruby-paypal 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +51 -0
- data/lib/ruby-paypal/paypal.rb +129 -0
- data/lib/ruby-paypal.rb +5 -0
- data/test/ts_ruby-paypal.rb +20 -0
- metadata +50 -0
data/README
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
Ruby-PayPal
|
2
|
+
===========
|
3
|
+
|
4
|
+
A lightweight ruby wrapper for PayPal NVP APIs. To install type the following
|
5
|
+
at the command line:
|
6
|
+
|
7
|
+
$ gem install ruby-paypal
|
8
|
+
|
9
|
+
|
10
|
+
To use Ruby-PayPal
|
11
|
+
==================
|
12
|
+
It's critical that you understand how PayPal works and how the PayPal NVP API
|
13
|
+
works. You should be relatively well-versed in the NVP API Developer Guide and
|
14
|
+
Reference (https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/index.html).
|
15
|
+
You should also visit and register yourself with the PayPal Developer Network
|
16
|
+
and get a Sandbox account with in the PayPal Development Central
|
17
|
+
(https://developer.paypal.com/).
|
18
|
+
|
19
|
+
Note that this library only supports the API signature method of securing the API credentials.
|
20
|
+
|
21
|
+
Using the Ruby-PayPal library is relatively simple:
|
22
|
+
|
23
|
+
1. For the Direct Payment using credit card payment, you need to use the
|
24
|
+
DoDirectPayment APIs:
|
25
|
+
|
26
|
+
username = <PayPal API username>
|
27
|
+
password = <PayPal API password>
|
28
|
+
signature = <PayPal API signature>
|
29
|
+
|
30
|
+
ipaddress = '192.168.1.1' # can be any IP address
|
31
|
+
amount = '100.00' # amount paid
|
32
|
+
card_type = 'VISA' # can be Visa, Mastercard, Amex etc
|
33
|
+
card_no = '4512345678901234' # credit card number
|
34
|
+
exp_date = '022010' # expiry date of the credit card
|
35
|
+
first_name = 'Sau Sheong'
|
36
|
+
last_name = 'Chang'
|
37
|
+
|
38
|
+
paypal = Paypal.new(username, password, signature)
|
39
|
+
response = paypal.do_direct_payment_sale(ipaddress, amount, card_type,
|
40
|
+
card_no, exp_date, first_name, last_name)
|
41
|
+
if response.ack == 'Success' then
|
42
|
+
# do your thing
|
43
|
+
end
|
44
|
+
|
45
|
+
The above code is for a final sale only
|
46
|
+
|
47
|
+
2. For the Express Checkout using the customer's PayPal account for payment, you will need to use the ExpressCheckout APIs:
|
48
|
+
|
49
|
+
<to be documented>
|
50
|
+
|
51
|
+
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'pp'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
SANDBOX_SERVER = 'api-3t.sandbox.paypal.com/nvp'
|
7
|
+
LIVE_SERVER = 'api-3t.paypal.com/nvp'
|
8
|
+
API_VERSION = '3.2'
|
9
|
+
|
10
|
+
module Net
|
11
|
+
class HTTPS < HTTP
|
12
|
+
def initialize(address, port = nil, verify = :no_verify)
|
13
|
+
super(address, port)
|
14
|
+
self.use_ssl = true
|
15
|
+
self.verify_mode = OpenSSL::SSL::VERIFY_NONE if verify == :no_verify
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class PayPalResponse < Hash
|
21
|
+
def method_missing(m,*a)
|
22
|
+
if m.to_s.upcase =~ /=$/
|
23
|
+
self[$`] = a[0]
|
24
|
+
elsif a.empty?
|
25
|
+
self[m.to_s.upcase]
|
26
|
+
else
|
27
|
+
raise NoMethodError, "#{m}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Paypal
|
33
|
+
def initialize(user, password, signature, url=SANDBOX_SERVER, subject=nil)
|
34
|
+
@api_parameters = {'USER' => user,
|
35
|
+
'PWD' => password,
|
36
|
+
'VERSION' => API_VERSION,
|
37
|
+
'SIGNATURE' => signature }
|
38
|
+
@paypal_url = url
|
39
|
+
end
|
40
|
+
|
41
|
+
def do_direct_payment_authorization(ipaddress, amount, credit_card_type, credit_card_no, expiry_date,
|
42
|
+
first_name, last_name, cvv2=nil, other_params={})
|
43
|
+
do_direct_payment('Authorization', ipaddress, amount, credit_card_type, credit_card_no,
|
44
|
+
expiry_date, first_name, last_name, cvv2, other_params)
|
45
|
+
end
|
46
|
+
|
47
|
+
def do_direct_payment_sale(ipaddress, amount, credit_card_type, credit_card_no, expiry_date,
|
48
|
+
first_name, last_name, cvv2=nil, other_params={})
|
49
|
+
do_direct_payment('Sale', ipaddress, amount, credit_card_type, credit_card_no,
|
50
|
+
expiry_date, first_name, last_name, cvv2, other_params)
|
51
|
+
end
|
52
|
+
|
53
|
+
def do_direct_payment(payment_action, ipaddress, amount, credit_card_type,
|
54
|
+
credit_card_no, expiry_date, first_name, last_name, cvv2=nil, other_params={})
|
55
|
+
params = {
|
56
|
+
'METHOD' => 'DoDirectPayment',
|
57
|
+
'PAYMENTACTION' => payment_action,
|
58
|
+
'AMT' => amount.to_s,
|
59
|
+
'CREDITCARDTYPE' => credit_card_type,
|
60
|
+
'ACCT' => credit_card_no,
|
61
|
+
'EXPDATE' => expiry_date,
|
62
|
+
'FIRSTNAME' => first_name,
|
63
|
+
'LASTNAME' => last_name,
|
64
|
+
'IPADDRESS' => ipaddress }
|
65
|
+
params['CVV2'] = cvv2 unless cvv2.nil?
|
66
|
+
params.merge! other_params
|
67
|
+
make_nvp_call(params)
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
def set_express_checkout(return_url, cancel_url, amount)
|
72
|
+
params = {
|
73
|
+
'METHOD' => 'SetExpressCheckout',
|
74
|
+
'RETURNURL' => return_url,
|
75
|
+
'CANCELURL' => cancel_url,
|
76
|
+
'AMT' => amount.to_s
|
77
|
+
}
|
78
|
+
make_nvp_call(params)
|
79
|
+
end
|
80
|
+
|
81
|
+
def get_express_checkout_details(token)
|
82
|
+
params = {
|
83
|
+
'METHOD' => 'GetExpressCheckoutDetails',
|
84
|
+
'TOKEN' => token
|
85
|
+
}
|
86
|
+
make_nvp_call(params)
|
87
|
+
end
|
88
|
+
|
89
|
+
def do_express_checkout_payment(token, payment_action,payer_id, amount)
|
90
|
+
params = {
|
91
|
+
'METHOD' => 'DoExpressCheckoutPayment',
|
92
|
+
'TOKEN' => token,
|
93
|
+
'PAYMENTACTION' => payment_action,
|
94
|
+
'PAYERID' => payer_id,
|
95
|
+
'AMT' => amount
|
96
|
+
}
|
97
|
+
make_nvp_call(params)
|
98
|
+
end
|
99
|
+
|
100
|
+
def do_authorization(transaction_id, amount)
|
101
|
+
params = {
|
102
|
+
'METHOD' => 'DoAuthorization',
|
103
|
+
'TRANSACTIONID' => transaction_id,
|
104
|
+
'AMT' => amount.to_s,
|
105
|
+
'TRANSACTIONENTITY' => 'Order'
|
106
|
+
}
|
107
|
+
make_nvp_call(params)
|
108
|
+
end
|
109
|
+
|
110
|
+
def make_nvp_call(params)
|
111
|
+
@api_parameters.merge! params
|
112
|
+
parameters = URI.escape(@api_parameters.to_a.collect {|pair| pair.join('=')}.join('&'))
|
113
|
+
response = Net::HTTPS.post_form(URI.parse("https://#{@paypal_url}"), @api_parameters)
|
114
|
+
response.error! unless response.kind_of? Net::HTTPSuccess
|
115
|
+
PayPalResponse.new.merge get_hash(response.body)
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
|
121
|
+
def get_hash(string)
|
122
|
+
hash = {}
|
123
|
+
string.split('&').collect { |pair| pair.split('=') }.each { |a|
|
124
|
+
hash[a[0]] = URI.unescape(a[1])
|
125
|
+
}
|
126
|
+
return hash
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
data/lib/ruby-paypal.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pp'
|
3
|
+
require 'ruby-paypal'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class RubyPayPal < Test::Unit::TestCase
|
7
|
+
def test_do_direct_payment_sale
|
8
|
+
paypal = Paypal.new('css.pa_1195828529_biz_api1.gmail.com', '1195828547',
|
9
|
+
'AJsoTDMhNESFz43L9WKtkP2tVCZZAZ3Wl.IxVLj8IfJQsjg8WZYMENpS')
|
10
|
+
resp = paypal.do_direct_payment_sale('202.156.13.1', '14.34', 'VISA',
|
11
|
+
'4574806205610007', '022017', 'Test', 'User')
|
12
|
+
assert_equal("Success", resp.ack)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_do_direct_payment_authorization
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: ruby-paypal
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2007-12-23 00:00:00 +08:00
|
8
|
+
summary: An lightweight Ruby wrapper for PayPal NVP API
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: sausheong.chang@gmail.com
|
12
|
+
homepage: http://blog.saush.com
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: ruby-paypal
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Chang Sau Sheong
|
31
|
+
files:
|
32
|
+
- lib/ruby-paypal
|
33
|
+
- lib/ruby-paypal/paypal.rb
|
34
|
+
- lib/ruby-paypal.rb
|
35
|
+
- test/ts_ruby-paypal.rb
|
36
|
+
- README
|
37
|
+
test_files:
|
38
|
+
- test/ts_ruby-paypal.rb
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
executables: []
|
44
|
+
|
45
|
+
extensions: []
|
46
|
+
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
dependencies: []
|
50
|
+
|