tonyla-paypal_adaptive 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.markdown +127 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/cacert.pem +3987 -0
- data/config/paypal_adaptive.yml +31 -0
- data/lib/config.rb +74 -0
- data/lib/ipn_notification.rb +37 -0
- data/lib/pay_request_schema.json +39 -0
- data/lib/paypal_adaptive.rb +4 -0
- data/lib/request.rb +102 -0
- data/lib/response.rb +38 -0
- data/templates/paypal_ipn.rb +25 -0
- data/test/data/invalid_chain_pay_request.json +13 -0
- data/test/data/invalid_parallel_pay_request.json +13 -0
- data/test/data/invalid_preapproval.json +12 -0
- data/test/data/invalid_simple_pay_request_1.json +8 -0
- data/test/data/valid_chain_pay_request.json +12 -0
- data/test/data/valid_parallel_pay_request.json +14 -0
- data/test/data/valid_preapproval.json +11 -0
- data/test/data/valid_simple_pay_request_1.json +9 -0
- data/test/test_helper.rb +7 -0
- data/test/unit/config_test.rb +24 -0
- data/test/unit/pay_request_schema_test.rb +49 -0
- data/test/unit/pay_request_test.rb +104 -0
- data/test/unit/payment_details_test.rb +34 -0
- data/test/unit/preapproval_test.rb +43 -0
- data/tonyla-paypal_adaptive.gemspec +77 -0
- metadata +123 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
development:
|
2
|
+
environment: "sandbox"
|
3
|
+
username: "signupforuseratsandboxpaypal"
|
4
|
+
password: "signupforuseratsandboxpaypal"
|
5
|
+
signature: "signupforuseratsandboxpaypal"
|
6
|
+
application_id: "APP-TEST"
|
7
|
+
ssl_cert_file:
|
8
|
+
|
9
|
+
test:
|
10
|
+
environment: "sandbox"
|
11
|
+
username: "signupforuseratsandboxpaypal"
|
12
|
+
password: "signupforuseratsandboxpaypal"
|
13
|
+
signature: "signupforuseratsandboxpaypal"
|
14
|
+
application_id: "APP-TEST"
|
15
|
+
ssl_cert_file:
|
16
|
+
|
17
|
+
production:
|
18
|
+
environment: "production"
|
19
|
+
username: "my_production_username"
|
20
|
+
password: "my_production_password"
|
21
|
+
signature: "my_production_signature"
|
22
|
+
application_id: "my_production_app_id"
|
23
|
+
ssl_cert_file:
|
24
|
+
|
25
|
+
with_erb_tags:
|
26
|
+
environment: "sandbox"
|
27
|
+
username: <%= ENV['paypal.username'] %>
|
28
|
+
password: <%= ENV['paypal.password'] %>
|
29
|
+
signature: "signupforuseratsandboxpaypal"
|
30
|
+
application_id: "APP-TEST"
|
31
|
+
ssl_cert_file:
|
data/lib/config.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
module PaypalAdaptive
|
5
|
+
class Config
|
6
|
+
PAYPAL_BASE_URL_MAPPING = {
|
7
|
+
:production => "https://www.paypal.com",
|
8
|
+
:sandbox => "https://www.sandbox.paypal.com",
|
9
|
+
:beta_sandbox => "https://www.beta-sandbox.paypal.com"
|
10
|
+
} unless defined? PAYPAL_BASE_URL_MAPPING
|
11
|
+
|
12
|
+
API_BASE_URL_MAPPING = {
|
13
|
+
:production => "https://svcs.paypal.com",
|
14
|
+
:sandbox => "https://svcs.sandbox.paypal.com",
|
15
|
+
:beta_sandbox => "https://svcs.beta-sandbox.paypal.com"
|
16
|
+
} unless defined? API_BASE_URL_MAPPING
|
17
|
+
|
18
|
+
attr_accessor :config_filepath, :paypal_base_url, :api_base_url, :headers, :ssl_ca_cert_path, :ssl_cert_file
|
19
|
+
|
20
|
+
def initialize(env=nil, config_override=nil)
|
21
|
+
if env
|
22
|
+
#non-rails env
|
23
|
+
@config_filepath = File.join(File.dirname(__FILE__), "..", "config", "paypal_adaptive.yml")
|
24
|
+
load(env, config_override)
|
25
|
+
else
|
26
|
+
@config_filepath = File.join(Rails.root, "config", "paypal_adaptive.yml")
|
27
|
+
load(Rails.env, config_override)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def load(environment, config_override)
|
32
|
+
config = YAML.load(ERB.new(File.new(@config_filepath).read).result)[environment]
|
33
|
+
config.merge!(config_override) unless config_override.nil?
|
34
|
+
|
35
|
+
if config["retain_requests_for_test"] == true
|
36
|
+
@retain_requests_for_test = true
|
37
|
+
else
|
38
|
+
pp_env = config['environment'].to_sym
|
39
|
+
|
40
|
+
@ssl_ca_cert_path = nil
|
41
|
+
@ssl_cert_file = nil
|
42
|
+
@paypal_base_url = PAYPAL_BASE_URL_MAPPING[pp_env]
|
43
|
+
@api_base_url = API_BASE_URL_MAPPING[pp_env]
|
44
|
+
@headers = {
|
45
|
+
"X-PAYPAL-SECURITY-USERID" => config['username'],
|
46
|
+
"X-PAYPAL-SECURITY-PASSWORD" => config['password'],
|
47
|
+
"X-PAYPAL-APPLICATION-ID" => config['application_id'],
|
48
|
+
"X-PAYPAL-REQUEST-DATA-FORMAT" => "JSON",
|
49
|
+
"X-PAYPAL-RESPONSE-DATA-FORMAT" => "JSON"
|
50
|
+
}
|
51
|
+
|
52
|
+
if config['ssl_cert_file'] && config['ssl_cert_file'].length > 0
|
53
|
+
@ssl_cert_file = config['ssl_cert_file']
|
54
|
+
end
|
55
|
+
|
56
|
+
if File.exists?("/etc/ssl/certs")
|
57
|
+
@ssl_ca_cert_path = "/etc/ssl/certs"
|
58
|
+
else
|
59
|
+
@ssl_ca_cert_path = File.join(File.dirname(__FILE__), "..", "cacert.pem")
|
60
|
+
end
|
61
|
+
|
62
|
+
@headers.merge!({
|
63
|
+
"X-PAYPAL-SECURITY-SIGNATURE" => config['signature']
|
64
|
+
}) if @ssl_cert_file.blank?
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def retain_requests_for_test?
|
70
|
+
!!@retain_requests_for_test
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'json'
|
4
|
+
require 'config'
|
5
|
+
|
6
|
+
module PaypalAdaptive
|
7
|
+
class IpnNotification
|
8
|
+
|
9
|
+
def initialize(env=nil)
|
10
|
+
@env = env
|
11
|
+
@@config ||= PaypalAdaptive::Config.new(@env)
|
12
|
+
@@paypal_base_url ||= @@config.paypal_base_url
|
13
|
+
@@ssl_ca_cert_path ||= @@config.ssl_ca_cert_path
|
14
|
+
@@ssl_cert_file ||= @@config.ssl_cert_file
|
15
|
+
end
|
16
|
+
|
17
|
+
def send_back(data)
|
18
|
+
data = "cmd=_notify-validate&#{data}"
|
19
|
+
url = URI.parse @@paypal_base_url
|
20
|
+
http = Net::HTTP.new(url.host, 443)
|
21
|
+
http.use_ssl = true
|
22
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
23
|
+
http.ca_path = @@ssl_ca_cert_path unless @@ssl_ca_cert_path.nil?
|
24
|
+
http.ca_file = @@ssl_cert_file unless @@ssl_cert_file.nil?
|
25
|
+
|
26
|
+
path = "#{@@paypal_base_url}/cgi-bin/webscr"
|
27
|
+
resp, response_data = http.post(path, data)
|
28
|
+
|
29
|
+
@verified = response_data == "VERIFIED"
|
30
|
+
end
|
31
|
+
|
32
|
+
def verified?
|
33
|
+
@verified
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
{"description":"Paypal PayRequest API",
|
2
|
+
"type":"object",
|
3
|
+
"properties":
|
4
|
+
{
|
5
|
+
"actionType": {"type":"string", "options":[{"value":"PAY", "label":"PAY"}]},
|
6
|
+
"cancelUrl": {"type":"string"},
|
7
|
+
"clientDetails": {"type":"object", "optional": true},
|
8
|
+
"currencyCode": {"type":"string"},
|
9
|
+
"feesPayer": {"type":"string", "optional":true,
|
10
|
+
"options":[
|
11
|
+
{"value":"SENDER", "label":"SENDER"},
|
12
|
+
{"value":"PRIMARYRECEIVER", "label":"PRIMARYRECEIVER"},
|
13
|
+
{"value":"EACHRECEIVER", "label":"EACHRECEIVER"},
|
14
|
+
{"value":"SECONDARYONLY", "label":"SECONDARYONLY"}
|
15
|
+
]},
|
16
|
+
"fundingConstraint": {"type":"object", "optional": true},
|
17
|
+
"ipnNotificationUrl": {"type":"string", "optional": true},
|
18
|
+
"memo": {"type":"string", "optional": true},
|
19
|
+
"pin": {"type":"string", "optional": true},
|
20
|
+
"preapprovalKey": {"type":"string", "optional": true},
|
21
|
+
"receiverList": {
|
22
|
+
"type":"object", "properties":{
|
23
|
+
"receiver":{
|
24
|
+
"type":"array",
|
25
|
+
"items":{
|
26
|
+
"email":{"type":"string"},
|
27
|
+
"amount":{"type":"string"},
|
28
|
+
"primary":{"type":"string","optional": true}}
|
29
|
+
}
|
30
|
+
}
|
31
|
+
},
|
32
|
+
|
33
|
+
"requestEnvelope": {"type":"object", "properties":{"errorLanguage":{"type":"string"}}},
|
34
|
+
"returnUrl": {"type":"string"},
|
35
|
+
"reverseAllParallelPaymentsOnError": {"type":"boolean", "optional": true},
|
36
|
+
"senderEmail": {"type":"string", "optional": true},
|
37
|
+
"trackingId": {"type":"string", "optional": true}
|
38
|
+
}
|
39
|
+
}
|
@@ -0,0 +1,4 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "config"))
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "request"))
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "response"))
|
4
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "ipn_notification"))
|
data/lib/request.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'config'
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
require 'response'
|
6
|
+
|
7
|
+
module PaypalAdaptive
|
8
|
+
class NoDataError < Exception
|
9
|
+
end
|
10
|
+
|
11
|
+
class Request
|
12
|
+
def initialize(env = nil)
|
13
|
+
@env = env
|
14
|
+
@@config ||= PaypalAdaptive::Config.new(@env)
|
15
|
+
@@api_base_url ||= @@config.api_base_url
|
16
|
+
@@headers ||= @@config.headers
|
17
|
+
@@ssl_cert_file ||= @@config.ssl_cert_file
|
18
|
+
@@ssl_ca_cert_path ||= @@config.ssl_ca_cert_path
|
19
|
+
end
|
20
|
+
|
21
|
+
def validate
|
22
|
+
#TODO the receiverList field not validating properly
|
23
|
+
|
24
|
+
# @@schema_filepath = "../lib/pay_request_schema.json"
|
25
|
+
# @@schema = File.open(@@schema_filepath, "rb"){|f| JSON.parse(f.read)}
|
26
|
+
# see page 42 of PP Adaptive Payments PDF for explanation of all fields.
|
27
|
+
#JSON::Schema.validate(@data, @@schema)
|
28
|
+
end
|
29
|
+
|
30
|
+
def pay(data)
|
31
|
+
raise NoDataError unless data
|
32
|
+
|
33
|
+
response_data = call_api(data, "/AdaptivePayments/Pay")
|
34
|
+
PaypalAdaptive::Response.new(response_data, @env)
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_payment_options(data)
|
38
|
+
raise NoDataError unless data
|
39
|
+
|
40
|
+
response_data = call_api(data, "/AdaptivePayments/SetPaymentOptions")
|
41
|
+
PaypalAdaptive::Response.new(response_data, @env)
|
42
|
+
end
|
43
|
+
|
44
|
+
def payment_details(data)
|
45
|
+
raise NoDataError unless data
|
46
|
+
|
47
|
+
call_api(data, "/AdaptivePayments/PaymentDetails")
|
48
|
+
end
|
49
|
+
|
50
|
+
def preapproval(data)
|
51
|
+
raise NoDataError unless data
|
52
|
+
|
53
|
+
response_data = call_api(data, "/AdaptivePayments/Preapproval")
|
54
|
+
PaypalAdaptive::Response.new(response_data, @env)
|
55
|
+
end
|
56
|
+
|
57
|
+
def preapproval_details(data)
|
58
|
+
raise NoDataError unless data
|
59
|
+
|
60
|
+
call_api(data, "/AdaptivePayments/PreapprovalDetails")
|
61
|
+
end
|
62
|
+
|
63
|
+
def cancel_preapproval(data)
|
64
|
+
raise NoDataError unless data
|
65
|
+
|
66
|
+
call_api(data, "/AdaptivePayments/CancelPreapproval")
|
67
|
+
end
|
68
|
+
|
69
|
+
def convert_currency(data)
|
70
|
+
raise NoDataError unless data
|
71
|
+
|
72
|
+
call_api(data, "/AdaptivePayments/ConvertCurrency")
|
73
|
+
end
|
74
|
+
|
75
|
+
def refund(data)
|
76
|
+
raise NoDataError unless data
|
77
|
+
|
78
|
+
call_api(data, "/AdaptivePayments/Refund")
|
79
|
+
end
|
80
|
+
|
81
|
+
def call_api(data, path)
|
82
|
+
#hack fix: JSON.unparse doesn't work in Rails 2.3.5; only {}.to_json does..
|
83
|
+
api_request_data = JSON.unparse(data) rescue data.to_json
|
84
|
+
url = URI.parse @@api_base_url
|
85
|
+
http = Net::HTTP.new(url.host, 443)
|
86
|
+
http.use_ssl = true
|
87
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
88
|
+
|
89
|
+
if @@ssl_ca_cert_path
|
90
|
+
cert = File.read(@@ssl_cert_file)
|
91
|
+
http.cert = OpenSSL::X509::Certificate.new(cert)
|
92
|
+
http.key = OpenSSL::PKey::RSA.new(cert)
|
93
|
+
http.ca_path = @@ssl_ca_cert_path unless @@ssl_ca_cert_path.nil? #/etc/ssl/certs'
|
94
|
+
end
|
95
|
+
|
96
|
+
resp, response_data = http.post(path, api_request_data, @@headers)
|
97
|
+
|
98
|
+
JSON.parse(response_data)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
data/lib/response.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module PaypalAdaptive
|
2
|
+
class Response < Hash
|
3
|
+
def initialize(response, env=nil)
|
4
|
+
@@config ||= PaypalAdaptive::Config.new(env)
|
5
|
+
@@paypal_base_url ||= @@config.paypal_base_url
|
6
|
+
|
7
|
+
self.merge!(response)
|
8
|
+
end
|
9
|
+
|
10
|
+
def success?
|
11
|
+
self['responseEnvelope']['ack'] == 'Success'
|
12
|
+
end
|
13
|
+
|
14
|
+
def errors
|
15
|
+
if success?
|
16
|
+
return []
|
17
|
+
else
|
18
|
+
self['error']
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def error_message
|
23
|
+
if success?
|
24
|
+
return nil
|
25
|
+
else
|
26
|
+
self['error'].first['message'] rescue nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def approve_paypal_payment_url
|
31
|
+
self['payKey'].nil? ? nil : "#{@@paypal_base_url}/webscr?cmd=_ap-payment&paykey=#{self['payKey']}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def preapproval_paypal_payment_url
|
35
|
+
self['preapprovalKey'].nil? ? nil : "#{@@paypal_base_url}/webscr?cmd=_ap-preapproval&preapprovalkey=#{self['preapprovalKey']}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Allow the metal piece to run in isolation
|
2
|
+
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
|
3
|
+
|
4
|
+
class PaypalIpn
|
5
|
+
def self.call(env)
|
6
|
+
if env["PATH_INFO"] =~ /^\/paypal_ipn/
|
7
|
+
request = Rack::Request.new(env)
|
8
|
+
params = request.params
|
9
|
+
|
10
|
+
ipn = PaypalAdaptive::IpnNotification.new
|
11
|
+
ipn.send_back(env['rack.request.form_vars'])
|
12
|
+
if ipn.verified?
|
13
|
+
#mark transaction as completed in your DB
|
14
|
+
output = "Verified."
|
15
|
+
else
|
16
|
+
output = "Not Verified."
|
17
|
+
end
|
18
|
+
|
19
|
+
[200, {"Content-Type" => "text/html"}, [output]]
|
20
|
+
else
|
21
|
+
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
{
|
2
|
+
"returnUrl":"http://127.0.0.1:3000/payments/completed_payment",
|
3
|
+
"requestEnvelope":{"errorLanguage":"en_US"},
|
4
|
+
"currencyCode":"USD",
|
5
|
+
"receiverList":{"receiver":[
|
6
|
+
{"email":"testpp_1261697850_per@nextsprocket.com", "amount":"50.00", "primary": "true"},
|
7
|
+
{"email":"sender_1261713739_per@nextsprocket.com", "amount":"100.00", "primary": "false"},
|
8
|
+
{"email":"ppsell_1261697921_biz@nextsprocket.com", "amount":"100.00", "primary": "false"}
|
9
|
+
]},
|
10
|
+
"cancelUrl":"http://127.0.0.1:3000/payments/cancelled_payment",
|
11
|
+
"actionType":"PAY"
|
12
|
+
}
|
13
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
{
|
2
|
+
"returnUrl":"http://127.0.0.1:3000/payments/completed_payment",
|
3
|
+
"requestEnvelope":{"errorLanguage":"en_US"},
|
4
|
+
"currencyCode":"USD",
|
5
|
+
"receiverList":{"receiver":[
|
6
|
+
{"email":"dummy@account.com", "amount":"100.00"},
|
7
|
+
{"email":"dummy@account.com", "amount":"50.00"},
|
8
|
+
{"email":"dummy@account.com", "amount":"50.00"}
|
9
|
+
]},
|
10
|
+
"cancelUrl":"http://127.0.0.1:3000/payments/cancelled_payment",
|
11
|
+
"actionType":"PAY"
|
12
|
+
}
|
13
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
{
|
2
|
+
"returnUrl":"http://127.0.0.1:3000/payments/completed_payment_request",
|
3
|
+
"requestEnvelope":{"errorLanguage":"en_US"},
|
4
|
+
"currencyCode":"USD",
|
5
|
+
"cancelUrl":"http://127.0.0.1:3000/payments/canceled_payment_request",
|
6
|
+
"actionType":"PAY",
|
7
|
+
"maxTotalAmountOfAllPayments": "1500.00",
|
8
|
+
"maxNumberOfPayments":"30",
|
9
|
+
"startingDate":"2010-07-13T07sdf00.000Z",
|
10
|
+
"endingDate":"2010-12-13T07dfg0000.000Z"
|
11
|
+
}
|
12
|
+
|
@@ -0,0 +1,8 @@
|
|
1
|
+
{
|
2
|
+
"returnUrl":"http://127.0.0.1:3000/payments/completed_payment",
|
3
|
+
"requestEnvelope":{"errorLanguage":"en_US"},
|
4
|
+
"currencyCode":"USD",
|
5
|
+
"receiverList":{"receiver":[{"email":"testpp_1261697850_per@nextsprocket.com", "amount":"10.0"}]},
|
6
|
+
"cancelUrl":"http://127.0.0.1:3000/payments/canceled_payment",
|
7
|
+
"actionType":1
|
8
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
{
|
2
|
+
"returnUrl":"http://127.0.0.1:3000/payments/completed_payment_request",
|
3
|
+
"requestEnvelope":{"errorLanguage":"en_US"},
|
4
|
+
"currencyCode":"USD",
|
5
|
+
"receiverList":{"receiver":[
|
6
|
+
{"email":"testpp_1261697850_per@nextsprocket.com", "amount":"100.00", "primary": "true"},
|
7
|
+
{"email":"ppsell_1261697921_biz@nextsprocket.com", "amount":"90.00", "primary": "false"}
|
8
|
+
]},
|
9
|
+
"cancelUrl":"http://127.0.0.1:3000/payments/canceled_payment_request",
|
10
|
+
"actionType":"PAY"
|
11
|
+
}
|
12
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
{
|
2
|
+
"returnUrl":"http://127.0.0.1:3000/payments/completed_payment_request",
|
3
|
+
"requestEnvelope":{"errorLanguage":"en_US"},
|
4
|
+
"currencyCode":"USD",
|
5
|
+
"receiverList":{"receiver":[
|
6
|
+
{"email":"testpp_1261697850_per@nextsprocket.com", "amount":"100.00"},
|
7
|
+
{"email":"sender_1261713739_per@nextsprocket.com", "amount":"50.00"},
|
8
|
+
{"email":"ppsell_1261697921_biz@nextsprocket.com", "amount":"50.00"}
|
9
|
+
]},
|
10
|
+
"cancelUrl":"http://127.0.0.1:3000/payments/canceled_payment_request",
|
11
|
+
"actionType":"PAY",
|
12
|
+
"senderEmail": "a@a.com"
|
13
|
+
}
|
14
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
{
|
2
|
+
"returnUrl":"http://127.0.0.1:3000/payments/completed_payment_request",
|
3
|
+
"requestEnvelope":{"errorLanguage":"en_US"},
|
4
|
+
"currencyCode":"USD",
|
5
|
+
"cancelUrl":"http://127.0.0.1:3000/payments/canceled_payment_request",
|
6
|
+
"maxTotalAmountOfAllPayments": "1500.00",
|
7
|
+
"maxNumberOfPayments":"30",
|
8
|
+
"startingDate":"2020-07-13T07:00:00.000Z",
|
9
|
+
"endingDate":"2020-12-13T07:00:00.000Z"
|
10
|
+
}
|
11
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
{
|
2
|
+
"returnUrl":"http://127.0.0.1:3000/payments/completed_payment_request",
|
3
|
+
"requestEnvelope":{"errorLanguage":"en_US"},
|
4
|
+
"currencyCode":"USD",
|
5
|
+
"receiverList":{"receiver":[{"email":"testpp_1261697850_per@nextsprocket.com", "amount":"10.00"}]},
|
6
|
+
"cancelUrl":"http://127.0.0.1:3000/payments/canceled_payment_request",
|
7
|
+
"actionType":"PAY",
|
8
|
+
"ipnNotificationUrl":"http://127.0.0.1:3000/payments/ipn_notification"
|
9
|
+
}
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ConfigTest < Test::Unit::TestCase
|
4
|
+
def test_set_ssl_cert_file
|
5
|
+
@config = PaypalAdaptive::Config.new("test", { "ssl_cert_file" => "/path/to/cacert.pem" })
|
6
|
+
assert_equal "/path/to/cacert.pem", @config.ssl_cert_file
|
7
|
+
assert_equal nil, @config.ssl_ca_cert_path
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_default_ssl_cert_file
|
11
|
+
@config = PaypalAdaptive::Config.new("test", { "ssl_cert_file" => "" })
|
12
|
+
assert File.exists?(@config.ssl_cert_file)
|
13
|
+
assert_equal nil, @config.ssl_ca_cert_path
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_erb_tags
|
17
|
+
ENV['paypal.username'] = 'account@email.com'
|
18
|
+
ENV['paypal.password'] = 's3krit'
|
19
|
+
|
20
|
+
config = PaypalAdaptive::Config.new("with_erb_tags")
|
21
|
+
assert_equal 'account@email.com', config.headers["X-PAYPAL-SECURITY-USERID"]
|
22
|
+
assert_equal 's3krit', config.headers["X-PAYPAL-SECURITY-PASSWORD"]
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'json'
|
3
|
+
require 'jsonschema'
|
4
|
+
|
5
|
+
class PayRequestSchemaTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@schema_filepath = File.join(File.dirname(__FILE__),"..", "..", "lib","pay_request_schema.json")
|
8
|
+
@schema = File.open(@schema_filepath, "rb"){|f| JSON.parse(f.read)}
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_valid_simple_pay
|
12
|
+
data_filepath = File.join(File.dirname(__FILE__),"..", "data","valid_simple_pay_request_1.json")
|
13
|
+
data = read_json_file(data_filepath)
|
14
|
+
|
15
|
+
#receiverList not validating correctly, is it due to the schema or jsonschema parsing?
|
16
|
+
assert_nothing_raised do
|
17
|
+
JSON::Schema.validate(data, @schema)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_invalid_simple_pay
|
22
|
+
data_filepath = File.join(File.dirname(__FILE__),"..", "data","invalid_simple_pay_request_1.json")
|
23
|
+
data = read_json_file(data_filepath)
|
24
|
+
|
25
|
+
assert_raise JSON::Schema::ValueError do
|
26
|
+
JSON::Schema.validate(data, @schema)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_valid_chain_pay
|
31
|
+
#TODO
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_invalid_chain_pay
|
35
|
+
#TODO
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_valid_parallel_pay
|
39
|
+
#TODO
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_invalid_parallel_pay
|
43
|
+
#TODO
|
44
|
+
end
|
45
|
+
|
46
|
+
def read_json_file(filepath)
|
47
|
+
File.open(filepath, "rb"){|f| JSON.parse(f.read)}
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PayRequestTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@pay_request = PaypalAdaptive::Request.new("test")
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_valid_simple_pay
|
9
|
+
puts "-------"
|
10
|
+
puts "simple"
|
11
|
+
|
12
|
+
data_filepath = File.join(File.dirname(__FILE__),"..", "data","valid_simple_pay_request_1.json")
|
13
|
+
|
14
|
+
data = read_json_file(data_filepath)
|
15
|
+
pp_response = @pay_request.pay(data)
|
16
|
+
|
17
|
+
puts "redirect url to\n #{pp_response.approve_paypal_payment_url}"
|
18
|
+
assert pp_response.success?
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_invalid_simple_pay
|
22
|
+
data_filepath = File.join(File.dirname(__FILE__),"..", "data","invalid_simple_pay_request_1.json")
|
23
|
+
|
24
|
+
data = read_json_file(data_filepath)
|
25
|
+
pp_response = @pay_request.pay(data)
|
26
|
+
puts pp_response.errors
|
27
|
+
assert pp_response.success? == false
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_valid_chain_pay
|
31
|
+
puts "-------"
|
32
|
+
puts "chain"
|
33
|
+
data_filepath = File.join(File.dirname(__FILE__),"..", "data","valid_chain_pay_request.json")
|
34
|
+
|
35
|
+
data = read_json_file(data_filepath)
|
36
|
+
pp_response = @pay_request.pay(data)
|
37
|
+
puts "redirect url to\n #{pp_response.approve_paypal_payment_url}"
|
38
|
+
|
39
|
+
unless pp_response.success?
|
40
|
+
puts pp_response.errors
|
41
|
+
end
|
42
|
+
|
43
|
+
assert pp_response.success?
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_invalid_chain_pay
|
47
|
+
data_filepath = File.join(File.dirname(__FILE__),"..", "data","invalid_chain_pay_request.json")
|
48
|
+
|
49
|
+
data = read_json_file(data_filepath)
|
50
|
+
pp_response = @pay_request.pay(data)
|
51
|
+
puts pp_response.errors
|
52
|
+
assert pp_response.success? == false
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_valid_parallel_pay
|
56
|
+
puts "-------"
|
57
|
+
puts "parallel"
|
58
|
+
|
59
|
+
data_filepath = File.join(File.dirname(__FILE__),"..", "data","valid_parallel_pay_request.json")
|
60
|
+
|
61
|
+
data = read_json_file(data_filepath)
|
62
|
+
pp_response = @pay_request.pay(data)
|
63
|
+
puts "redirect url to\n #{pp_response.approve_paypal_payment_url}"
|
64
|
+
assert pp_response.success?
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_invalid_parallel_pay
|
68
|
+
data_filepath = File.join(File.dirname(__FILE__),"..", "data","invalid_parallel_pay_request.json")
|
69
|
+
|
70
|
+
data = read_json_file(data_filepath)
|
71
|
+
pp_response = @pay_request.pay(data)
|
72
|
+
puts pp_response.errors
|
73
|
+
assert pp_response.success? == false
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_set_payment_options
|
77
|
+
#TODO
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_preapproval
|
81
|
+
#TODO
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_preapproval_details
|
85
|
+
#TODO
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_cancel_preapproval
|
89
|
+
#TODO
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_convert_currency
|
93
|
+
#TODO
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_refund
|
97
|
+
#TODO
|
98
|
+
end
|
99
|
+
|
100
|
+
def read_json_file(filepath)
|
101
|
+
File.open(filepath, "rb"){|f| JSON.parse(f.read)}
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|