rails-paypal-gem 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.md +86 -0
- data/lib/rails-paypal.rb +22 -0
- data/lib/rails-paypal/nvp-parser.rb +12 -0
- data/lib/rails-paypal/version.rb +3 -0
- data/lib/services/express_checkout.rb +50 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MzUzODUzODRiNmIzYmNhZWE2NmZhZWE4ODZmZTY3NjNlMzA0NTg2YQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ODZhZjY2YmMzZGRkNTVlZTIxYzg5ZDRiYjNkNjRkMTMxMTRlMDUwZA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YTQzODYzM2JkYzY0ZjgzZWI1M2UwMTYxZDZkOGY0YzI4Y2E5ZTllYmIyMzlj
|
10
|
+
NGFhYTczMWRkNWQ1ZWUxODIyZmVmMTc4MmJmNDQzYWZmOWMyM2VkNTIyNTc1
|
11
|
+
NGU3ZDk3YmYxMmRlMGY2YTAzZDFlOGYzYjUxNzJjYmIxMzM0MDc=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YTE3NzRhNzUwNzliYWJlYTY5OWRjMzczODMzZDFlNTg2NzI2YmQ2NGNkZTYw
|
14
|
+
YzNmYWFjZGVjZThlODNjNTlkMjJiZGM1ODQ1NmVjYTVhNWFmOTZkYzRiMDc3
|
15
|
+
Y2I4ZjQ5ZWUwMjkwMmIxMGM1M2RhNzFjNDU0MTJlOTYwNmYwMjE=
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
rails-paypal
|
2
|
+
============
|
3
|
+
|
4
|
+
A simple paypal client for *rails lovers* and *paypal haters*!
|
5
|
+
|
6
|
+
|
7
|
+
Install
|
8
|
+
-------
|
9
|
+
Since this is pre-alpha right now, I did not updage the builded gem, so just add it your Gemfile and point to github here.
|
10
|
+
|
11
|
+
gem 'rails-paypal', :git => 'git://github.com/aliciatang/rails-paypal.git'
|
12
|
+
bundle update
|
13
|
+
|
14
|
+
Call Paypal
|
15
|
+
-----------
|
16
|
+
Make a call the paypal endpoint with what every parameters you need.
|
17
|
+
|
18
|
+
RailsPaypal.call({ :METHOD => 'SetExpressCheckout', :PAYMENTREQUEST_0_AMT=>'300.0'})
|
19
|
+
|
20
|
+
The above call will return
|
21
|
+
|
22
|
+
{"TOKEN"=>"EC-41U99669P1884934P", "TIMESTAMP"=>"2012-09-03T04:59:14Z", "CORRELATIONID"=>"5d061f72f2cd4", "ACK"=>"Success", "VERSION"=>"92.0", "BUILD"=>"3622349"}
|
23
|
+
|
24
|
+
Express Checkout
|
25
|
+
----------------
|
26
|
+
I am working on express checkout now. Due the urgliness of paypay documentations, I will try to write a little bit more and summarize them.
|
27
|
+
|
28
|
+
Step One: get token
|
29
|
+
|
30
|
+
# create the express checkout service instance with a list of line-item details
|
31
|
+
ec = RailsPaypal::ExpressCheckout.new([{:name=>'item 1',:price=>'5.0',:quantity=> 4, :description => "This is optional"}])
|
32
|
+
# call paypal for a toke
|
33
|
+
ec.set
|
34
|
+
|
35
|
+
Step Two: rediret the end user to paypal
|
36
|
+
|
37
|
+
# you can call this directly without calling set first since it will check for the token
|
38
|
+
# But you do need to pass the list of line items to the construtor, since we need to call paypal for the token
|
39
|
+
ec.redirect_url
|
40
|
+
|
41
|
+
Step Tree: hope your user finish the payment on paypal site.
|
42
|
+
|
43
|
+
If they do, they will be redirected to the return_url you specified in step one with two parameters: token and PayerID
|
44
|
+
|
45
|
+
your_return_url/?token=EC-3EC18176EP034311U&PayerID=TPCS2TXJU979S
|
46
|
+
|
47
|
+
In the controller for the above url, now is the time for you to capture the $$. That's all it is about, isn't it?
|
48
|
+
|
49
|
+
ec.do
|
50
|
+
# or use the static method
|
51
|
+
RailsPaypal::ExpressCheckout.do("YOURTOKEN","TPCS2TXJU979S","20.00")
|
52
|
+
|
53
|
+
|
54
|
+
Step Four: There is no step 4! How nice is that!
|
55
|
+
|
56
|
+
----------
|
57
|
+
Hand on there, you cautch me! Yes, in step 3 I have an if condition but no else. Which is pretty bad!
|
58
|
+
If you only care about the case you got the money, there is nothing you need to do.
|
59
|
+
No action is not a bad action to take.
|
60
|
+
In case you do wanna be noticed when the user abandon the cart or at lease log it. There another url you passed to paypay in step 1 when setting up express checkout.
|
61
|
+
And we could do something about it.
|
62
|
+
|
63
|
+
A little bit more accurate.
|
64
|
+
Verify payment info by calling the get method to get what the actual amount was paid.
|
65
|
+
And of course you can get invoice id you specified in step one as well so you know which order you should mark as paid and deliver just in case you did not pass the order id in the return_url already.
|
66
|
+
|
67
|
+
ec.get
|
68
|
+
# or use the static method
|
69
|
+
RailsPaypal::ExpressCheckout.get("YOURTOKEN")
|
70
|
+
|
71
|
+
|
72
|
+
# TODO move thise to a config file
|
73
|
+
URLS = {
|
74
|
+
:sandbox => 'https://api-3t.sandbox.paypal.com/nvp',
|
75
|
+
:production =>'https://api-3t.paypal.com/nvp',
|
76
|
+
:checkout => 'https://www.sandbox.paypal.com/cgi-bin/webscr'
|
77
|
+
}
|
78
|
+
# wish httparty have something similar for default_params for body data
|
79
|
+
PARAMS = {:USER => 'alicia_1345427347_biz_api1.gmail.com' ,
|
80
|
+
:PWD => '1345427370',
|
81
|
+
:SIGNATURE => 'AndYn9Gde1bznDG3gf7QHBhnXR5-A4Xl8tLjtE.ZHgc49RBCg5Yo4RhN',
|
82
|
+
:VERSION => '92.0', # this is the latest version as of Sep 2nd, 2012
|
83
|
+
:RETURNURL => 'http://return.com',
|
84
|
+
:CANCELURL => 'http://cancel.com'
|
85
|
+
}
|
86
|
+
|
data/lib/rails-paypal.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'rails-paypal/nvp-parser'
|
3
|
+
class RailsPaypal
|
4
|
+
include HTTParty
|
5
|
+
if Rails.env == "development"
|
6
|
+
base_uri "https://api-3t.sandbox.paypal.com/nvp"
|
7
|
+
else
|
8
|
+
base_uri "https://api-3t.paypal.com/nvp"
|
9
|
+
end
|
10
|
+
# custom parser must be signed before set the format
|
11
|
+
parser NvpParsingIncluded
|
12
|
+
# the response format
|
13
|
+
format :nvp
|
14
|
+
# debug_output # turn on httparty debuy
|
15
|
+
# default_timeout 100
|
16
|
+
|
17
|
+
def self.call(data)
|
18
|
+
ret = post('/', :body => PARAMS.merge(data))
|
19
|
+
ret.parsed_response
|
20
|
+
end
|
21
|
+
end
|
22
|
+
require 'services/express_checkout'
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# parse nave value pair
|
2
|
+
require 'uri'
|
3
|
+
require 'httparty/parser'
|
4
|
+
class NvpParsingIncluded < HTTParty::Parser
|
5
|
+
SupportedFormats.merge!({"text/plain" => :nvp})
|
6
|
+
protected
|
7
|
+
# perform atom parsing on body
|
8
|
+
def nvp
|
9
|
+
ret = body.split('&').map { |e| e.split('=').map { |v| URI::decode v } }
|
10
|
+
Hash[*ret.flatten]
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
class RailsPaypal::ExpressCheckout < RailsPaypal
|
2
|
+
attr_accessor :params
|
3
|
+
attr_accessor :token
|
4
|
+
attr_accessor :line_items
|
5
|
+
|
6
|
+
def initialize(line_items)
|
7
|
+
self.line_items = line_items
|
8
|
+
self.params = {}
|
9
|
+
total = 0.0
|
10
|
+
line_items.each_with_index do |li, i|
|
11
|
+
self.params["L_PAYMENTREQUEST_0_NAME#{i}"] = li[:name] if li.has_key?(:name)
|
12
|
+
self.params["L_PAYMENTREQUEST_0_QTY#{i}"] = li[:quantity] if li.has_key?(:quantity)
|
13
|
+
self.params["L_PAYMENTREQUEST_0_AMT#{i}"] = li[:price]
|
14
|
+
self.params["L_PAYMENTREQUEST_0_DESC#{i}"] = li[:description] if li.has_key?(:description)
|
15
|
+
total += (li[:price].to_f * li[:quantity].to_i)
|
16
|
+
end
|
17
|
+
params["PAYMENTREQUEST_0_AMT"] = total.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def set(action = 'Sale')
|
21
|
+
self.params["PAYMENTREQUEST_0_CURRENCYCODE"] = "EUR"
|
22
|
+
self.params["PAYMENTREQUEST_0_PAYMENTACTION"] = action
|
23
|
+
self.params["METHOD"] = 'SetExpressCheckout'
|
24
|
+
response = self.class.call(self.params)
|
25
|
+
if response["ACK"] == 'Success'
|
26
|
+
self.token = response["TOKEN"]
|
27
|
+
else
|
28
|
+
raise response["L_ERRORCODE0"]+":"+response["L_LONGMESSAGE0"]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def get
|
33
|
+
set if self.token.nil?
|
34
|
+
self.class.get(self.token)
|
35
|
+
end
|
36
|
+
|
37
|
+
def redirect_url
|
38
|
+
set if self.token.nil?
|
39
|
+
if Rails.env == "development"
|
40
|
+
"https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=" + self.token
|
41
|
+
elsif Rails.env == "production"
|
42
|
+
"https://www.paypal.com/webscr?cmd=_express-checkout&token=" + self.token
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.get(token)
|
47
|
+
call({"TOKEN" => token, "METHOD" => "GetExpressCheckoutDetails"})
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-paypal-gem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Jezek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A simple paypal client for rails lovers and paypal haters!
|
28
|
+
email: mail@mediatainment-productions.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.md
|
33
|
+
files:
|
34
|
+
- lib/rails-paypal/nvp-parser.rb
|
35
|
+
- lib/rails-paypal/version.rb
|
36
|
+
- lib/rails-paypal.rb
|
37
|
+
- lib/services/express_checkout.rb
|
38
|
+
- README.md
|
39
|
+
homepage: https://github.com/mediatainment/rails-paypal
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.0.3
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Paypal for rails
|
63
|
+
test_files: []
|
64
|
+
has_rdoc:
|