Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Improvements for paypal-recurring gem #27

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
= PayPal Recurring Billing

PayPal Express Checkout API Client for recurring billing.
PayPal Express Checkout API Client for recurring billing. A modified version based on fnando/paypal-recurring

== Installation

gem install paypal-recurring
In your Gemfile, add gem 'paypal-recurring', :git => git://github.com/fredxinfan/paypal-recurring.git

== Usage

Expand All @@ -13,12 +13,23 @@ First, you need to set up your credentials:
require "paypal/recurring"

PayPal::Recurring.configure do |config|
config.sandbox = true
# config.sandbox = true
config.username = "seller_1308793919_biz_api1.simplesideias.com.br"
config.password = "1308793931"
config.application_id = "APP-YOURAPPIDHERE"
config.signature = "AFcWxV21C7fd0v3bYYYRCpSSRl31AzaB6TzXx5amObyEghjU13.0av2Y"
end

If you are using paypal certificate:

PayPal::Recurring.configure do |config|
# config.sandbox = true
config.username = "seller_1308793919_biz_api1.simplesideias.com.br"
config.password = "1308793931"
config.application_id = "APP-YOURAPPIDHERE"
config.ssl_cert = "/path/to/cert_key_pem.txt"
end

Then, you can request a new payment authorization:

ppr = PayPal::Recurring.new({
Expand All @@ -30,6 +41,20 @@ Then, you can request a new payment authorization:
:currency => "USD"
})

If you request a payment on behalf of a third party:

ppr = PayPal::Recurring.new({
:return_url => "http://example.com/paypal/thank_you",
:cancel_url => "http://example.com/paypal/canceled",
:ipn_url => "http://example.com/paypal/ipn",
:description => "Awesome - Monthly Subscription",
:amount => "9.00",
:subject => "[email protected]",
:currency => "USD"
})

You also need to add "X-PAYPAL-AUTHORIZATION" header in your third party API call. Generate "X-PAYPAL-AUTHORIZATION" header by using this gem https://github.com/fx83060856/paypal-permissions.git

response = ppr.checkout
puts response.checkout_url if response.valid?

Expand Down
20 changes: 17 additions & 3 deletions lib/paypal/recurring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ module Recurring

ENDPOINTS = {
:sandbox => {
:api => "https://api-3t.sandbox.paypal.com/nvp",
:api_cert => "https://api.sandbox.paypal.com/nvp",
:api_sig => "https://api-3t.sandbox.paypal.com/nvp",
:site => "https://www.sandbox.paypal.com/cgi-bin/webscr"
},
:production => {
:api => "https://api-3t.paypal.com/nvp",
:api_cert => "https://api.paypal.com/nvp",
:api_sig => "https://api-3t.paypal.com/nvp",
:site => "https://www.paypal.com/cgi-bin/webscr"
}
}
Expand All @@ -44,6 +46,18 @@ class << self
# Set PayPal's API signature.
#
attr_accessor :signature

# Set PayPal's API certificate.
#
attr_accessor :ssl_cert

# Set third party authorization signature.
#
attr_accessor :authorization

# Set PayPal's application id.
#
attr_accessor :application_id

# Set seller id. Will be used to verify IPN.
#
Expand Down Expand Up @@ -92,7 +106,7 @@ def self.endpoints
# Return API endpoint based on current environment.
#
def self.api_endpoint
endpoints[:api]
signature ? endpoints[:api_sig] : endpoints[:api_cert]
end

# Return PayPal's API version.
Expand Down
13 changes: 10 additions & 3 deletions lib/paypal/recurring/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Base
attr_accessor :trial_length
attr_accessor :trial_period
attr_accessor :trial_amount
attr_accessor :subject
attr_accessor :no_shipping

def initialize(options = {})
options.each {|name, value| send("#{name}=", value)}
Expand Down Expand Up @@ -68,10 +70,11 @@ def checkout
:item_category,
:item_name,
:item_amount,
:item_quantity
:item_quantity,
:subject,
:no_shipping
).merge(
:payment_action => "Authorization",
:no_shipping => 1,
:L_BILLINGTYPE0 => "RecurringPayments"
)

Expand Down Expand Up @@ -273,7 +276,11 @@ def refund
def collect(*args) # :nodoc:
args.inject({}) do |buffer, attr_name|
value = send(attr_name)
buffer[attr_name] = value if value
if value
buffer[attr_name] = value
elsif attr_name.to_s == "no_shipping"
buffer[attr_name] = 1
end
buffer
end
end
Expand Down
15 changes: 15 additions & 0 deletions lib/paypal/recurring/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Request
:trial_length => "TRIALTOTALBILLINGCYCLES",
:trial_period => "TRIALBILLINGPERIOD",
:username => "USER",
:subject => "SUBJECT",
:version => "VERSION"
}

Expand Down Expand Up @@ -119,6 +120,13 @@ def run(method, params = {})
def request
@request ||= Net::HTTP::Post.new(uri.request_uri).tap do |http|
http["User-Agent"] = "PayPal::Recurring/#{PayPal::Recurring::Version::STRING}"
http["X-PAYPAL-SECURITY-USERID"] = PayPal::Recurring.username
http["X-PAYPAL-SECURITY-PASSWORD"] = PayPal::Recurring.password
http["X-PAYPAL-APPLICATION-ID"] = PayPal::Recurring.application_id
http["X-PAYPAL-SECURITY-SIGNATURE"] = PayPal::Recurring.signature
http["X-PAYPAL-AUTHORIZATION"] = PayPal::Recurring.authorization
http['X-PAYPAL-REQUEST-DATA-FORMAT'] = 'NV'
http['X-PAYPAL-RESPONSE-DATA-FORMAT'] = 'NV'
end
end

Expand Down Expand Up @@ -147,6 +155,11 @@ def client
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.ca_file = CA_FILE
if PayPal::Recurring.ssl_cert
cert = File.read(PayPal::Recurring.ssl_cert)
http.cert = OpenSSL::X509::Certificate.new(cert)
http.key = OpenSSL::PKey::RSA.new(cert)
end
end
end
end
Expand All @@ -155,7 +168,9 @@ def default_params
{
:username => PayPal::Recurring.username,
:password => PayPal::Recurring.password,
:application_id => PayPal::Recurring.application_id,
:signature => PayPal::Recurring.signature,
:ssl_cert => PayPal::Recurring.ssl_cert,
:version => PayPal::Recurring.api_version
}
end
Expand Down