Created
September 2, 2012 16:40
-
-
Save nov/3601298 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'paypal' | |
require 'active_support/all' | |
module Paypal | |
module NVP | |
class Request | |
def post_with_debugger(method, params = {}) | |
response = post_without_debugger(method, params) | |
puts <<-DEBUG | |
#{method} | |
========== | |
#{common_params.merge(params).to_query} | |
---------- | |
#{response} | |
========== | |
DEBUG | |
response | |
end | |
alias_method_chain :post, :debugger | |
end | |
end | |
end | |
module Payment | |
class Paypal | |
def self.config | |
if ::Paypal.sandbox? | |
{ | |
:username => SANDBOX_DEVELOPER_ACCOUNT_EMAIL, | |
:password => SANDBOX_DEVELOPER_ACCOUNT_PASSWORD, | |
:signature => SANDBOX_DEVELOPER_ACCOUNT_SIGNATURE | |
} | |
else | |
# NOT USED | |
end | |
end | |
def client | |
@client ||= ::Paypal::Express::Request.new(self.class.config) | |
end | |
end | |
end | |
RETURN_URL = 'http://matake.jp/paypal/success' | |
CANCEL_URL = 'http://matake.jp/paypal/cancel' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.join(File.dirname(__FILE__), 'paypal_debugger') | |
Paypal.sandbox! | |
mode = :setup | |
paypal = Payment::Paypal.new | |
payment_request = Paypal::Payment::Request.new( | |
:currency_code => :JPY, | |
:billing_type => :RecurringPayments, | |
:billing_agreement_description => 'iKnow! Subscription' | |
) | |
token = PUT_YOUT_TOKEN_HERE_AFTER_SUCCEEDED_SETUP_CALL | |
profile_id = PUT_YOUT_PROFILE_ID_HERE_AFTER_SUCCEEDED_SUBSCRIBE_CALL | |
action = :Cancel # for unsubscribe call | |
# paypal.client.details(token) | |
begin | |
response = case mode | |
when :setup | |
response = paypal.client.setup( | |
payment_request, | |
RETURN_URL, | |
CANCEL_URL, | |
:pay_on_paypal => true, | |
:no_shipping => true | |
) | |
puts response.redirect_uri | |
puts response.popup_uri | |
`open "#{response.redirect_uri}"` | |
when :subscribe | |
# When pending, check IPN later. | |
profile = Paypal::Payment::Recurring.new( | |
:start_date => Time.now.utc.end_of_month + 8.hours + 1.second, | |
:description => 'iKnow! Subscription', | |
:activation => { | |
:initial_amount => 1000, | |
:failed_action => :CancelOnFailure | |
}, | |
:billing => { | |
:period => :Month, | |
:frequency => 1, | |
:amount => 1000, | |
:currency_code => :JPY | |
} | |
) | |
response = paypal.client.subscribe!(token, profile) | |
p response.recurring.identifier | |
profile = paypal.client.subscription(response.recurring.identifier).recurring | |
p profile.start_date | |
p profile.summary.last_payment_date | |
when :subscription | |
profile = paypal.client.subscription(profile_id).recurring | |
# if profile.summary.cycles_completed > 0 | |
# p "reccured" | |
# p "End at #{(profile.summary.last_payment_date.to_time + 1.week).end_of_month}" | |
# else | |
# p "only initial" | |
# p "End at #{Time.now.end_of_month}" | |
# end | |
p profile.start_date | |
# p profile.summary.last_payment_date, profile.summary.last_payment_date.to_time | |
# p "End at #{(profile.summary.last_payment_date.to_time + 1.week).end_of_month}" | |
# p paypal.client.subscription(profile_id).recurring | |
when :renew | |
p paypal.client.renew!(profile_id, action) | |
end | |
rescue => e | |
p e.response | |
end | |
# for transaction incomplete error | |
# => Update credit card on paypal please! | |
# incontext recurring (without instanct) | |
# => from PayPal API version 69 (2011/03/07~) | |
# mobile | |
# => Paypal handles UA automatically, no need for mobile support in paypal-express gem! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment