active_payment 0.0.5

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.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/app/controllers/active_payment/paypal_adaptive_payment_callback_controller.rb +26 -0
  3. data/app/controllers/active_payment/paypal_express_checkout_callback_controller.rb +33 -0
  4. data/app/models/active_payment/transaction.rb +18 -0
  5. data/config/routes.rb +9 -0
  6. data/db/migrate/20150813161144_create_payments_transactions.rb +22 -0
  7. data/lib/active_payment/configuration.rb +14 -0
  8. data/lib/active_payment/engine.rb +23 -0
  9. data/lib/active_payment/gateway.rb +125 -0
  10. data/lib/active_payment/gateways/paypal_adaptive_payment.rb +85 -0
  11. data/lib/active_payment/gateways/paypal_express_checkout.rb +86 -0
  12. data/lib/active_payment/models/concerns/payable.rb +32 -0
  13. data/lib/active_payment/models/concerns/payee.rb +27 -0
  14. data/lib/active_payment/models/concerns/payer.rb +13 -0
  15. data/lib/active_payment/models/sale.rb +60 -0
  16. data/lib/active_payment/models/sales.rb +78 -0
  17. data/lib/active_payment/version.rb +3 -0
  18. data/lib/active_payment.rb +36 -0
  19. data/spec/active_payment/configuration_spec.rb +59 -0
  20. data/spec/active_payment/controllers/paypal_adaptive_payment_callback_controller_spec.rb +77 -0
  21. data/spec/active_payment/controllers/paypal_express_checkout_callback_controller_spec.rb +102 -0
  22. data/spec/active_payment/gateway_spec.rb +140 -0
  23. data/spec/active_payment/gateways/paypal_adaptive_payment_spec.rb +105 -0
  24. data/spec/active_payment/gateways/paypal_express_checkout_spec.rb +144 -0
  25. data/spec/active_payment/models/sale_spec.rb +109 -0
  26. data/spec/active_payment/models/sales_spec.rb +93 -0
  27. data/spec/active_payment/models/transaction_spec.rb +31 -0
  28. data/spec/dummy/README.rdoc +28 -0
  29. data/spec/dummy/Rakefile +6 -0
  30. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  31. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  32. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  33. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  34. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  35. data/spec/dummy/bin/bundle +3 -0
  36. data/spec/dummy/bin/rails +4 -0
  37. data/spec/dummy/bin/rake +4 -0
  38. data/spec/dummy/config/application.rb +29 -0
  39. data/spec/dummy/config/boot.rb +5 -0
  40. data/spec/dummy/config/database.yml +25 -0
  41. data/spec/dummy/config/environment.rb +5 -0
  42. data/spec/dummy/config/environments/development.rb +32 -0
  43. data/spec/dummy/config/environments/production.rb +80 -0
  44. data/spec/dummy/config/environments/test.rb +39 -0
  45. data/spec/dummy/config/initializers/active_payment.rb +7 -0
  46. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  47. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  48. data/spec/dummy/config/initializers/inflections.rb +16 -0
  49. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  50. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  51. data/spec/dummy/config/initializers/session_store.rb +3 -0
  52. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  53. data/spec/dummy/config/locales/en.yml +23 -0
  54. data/spec/dummy/config/routes.rb +3 -0
  55. data/spec/dummy/config.ru +4 -0
  56. data/spec/dummy/db/development.sqlite3 +0 -0
  57. data/spec/dummy/db/migrate/20150813161144_create_payments_transactions.rb +22 -0
  58. data/spec/dummy/db/schema.rb +35 -0
  59. data/spec/dummy/db/test.sqlite3 +0 -0
  60. data/spec/dummy/log/development.log +33 -0
  61. data/spec/dummy/log/test.log +73016 -0
  62. data/spec/dummy/public/404.html +58 -0
  63. data/spec/dummy/public/422.html +58 -0
  64. data/spec/dummy/public/500.html +57 -0
  65. data/spec/dummy/public/favicon.ico +0 -0
  66. data/spec/factories/transaction.rb +15 -0
  67. data/spec/helper.rb +86 -0
  68. metadata +298 -0
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'rails'
3
+ require 'activemerchant'
4
+ require 'active_paypal_adaptive_payment'
5
+
6
+ require 'active_payment/engine' if defined?(Rails)
7
+ require 'active_payment/gateway'
8
+ require 'active_payment/configuration'
9
+ require 'active_payment/gateways/paypal_adaptive_payment'
10
+ require 'active_payment/gateways/paypal_express_checkout'
11
+ require 'active_payment/models/concerns/payable'
12
+ require 'active_payment/models/concerns/payee'
13
+ require 'active_payment/models/concerns/payer'
14
+ require 'active_payment/models/sale'
15
+ require 'active_payment/models/sales'
16
+
17
+ module ActivePayment
18
+ attr_accessor :configuration
19
+
20
+ class InvalidGatewayResponseError < StandardError; end
21
+ class InvalidAmountError < StandardError; end
22
+ class InvalidItemsError < StandardError; end
23
+ class InvalidGatewayUserError < StandardError; end
24
+ class NoTransactionError < StandardError; end
25
+ class SecurityError < StandardError; end
26
+
27
+ class Engine < Rails::Engine; end
28
+
29
+ def self.configuration
30
+ @configuration ||= Configuration.new
31
+ end
32
+
33
+ def self.configure
34
+ yield(configuration)
35
+ end
36
+ end
@@ -0,0 +1,59 @@
1
+ require 'helper'
2
+
3
+ describe ActivePayment::Configuration do
4
+ before(:each) do
5
+ @configuration = ActivePayment::Configuration.new
6
+ end
7
+
8
+ it 'set the min amount' do
9
+ @configuration.min_amount = 10
10
+ expect(@configuration.min_amount).to eq(10)
11
+ end
12
+
13
+ it 'set the min amount when its not set' do
14
+ expect(@configuration.min_amount).to eq(0)
15
+ end
16
+
17
+ it 'set the ip_security to false by default' do
18
+ expect(@configuration.ip_security).to be false
19
+ end
20
+
21
+ it 'set the ip_security' do
22
+ @configuration.ip_security = true
23
+ expect(@configuration.ip_security).to be true
24
+ end
25
+
26
+ it 'set paypal login' do
27
+ @configuration.paypal_login = 'login@paypal.com'
28
+ expect(@configuration.paypal_login).to eq('login@paypal.com')
29
+ end
30
+
31
+ it 'set paypal password' do
32
+ @configuration.paypal_password = 'password'
33
+ expect(@configuration.paypal_password).to eq('password')
34
+ end
35
+
36
+ it 'set paypal signature' do
37
+ @configuration.paypal_signature = 'signature'
38
+ expect(@configuration.paypal_signature).to eq('signature')
39
+ end
40
+
41
+ it 'set paypal appid' do
42
+ @configuration.paypal_appid = 'appid'
43
+ expect(@configuration.paypal_appid).to eq('appid')
44
+ end
45
+
46
+ it 'set the default host' do
47
+ @configuration.default_url_host = 'http://example.com'
48
+ expect(@configuration.default_url_host).to eq('http://example.com')
49
+ end
50
+
51
+ it 'set test as false by default' do
52
+ expect(@configuration.test).to be false
53
+ end
54
+
55
+ it 'set the test' do
56
+ @configuration.test = true
57
+ expect(@configuration.test).to be true
58
+ end
59
+ end
@@ -0,0 +1,77 @@
1
+ require 'helper'
2
+
3
+ RSpec.describe ActivePayment::PaypalAdaptivePaymentCallbackController, type: :controller do
4
+ routes { ActivePayment::Engine.routes }
5
+
6
+ describe 'ipn' do
7
+ it 'should throw invalid route if trying to access with GET' do
8
+ # get :ipn
9
+ # p response.inspect
10
+ # response.status.to eq 404
11
+ end
12
+
13
+ it 'should raise NoTransactionError if no such transaction' do
14
+ payload = "transaction%5B1%5D.status_for_sender_txn=Pending&transaction%5B1%5D.pending_reason=UNILATERAL&payment_request_date=Wed+Aug+19+11%3A39%3A12+PDT+2015&return_url=http%3A//088da0ca.ngrok.io/payments/paypal/success&fees_payer=EACHRECEIVER&ipn_notification_url=http%3A//088da0ca.ngrok.io/payments/paypal/ipn&transaction%5B1%5D.paymentType=SERVICE&transaction%5B1%5D.id_for_sender_txn=60M289731B8092737&sender_email=support-buyer%40streamup.com&verify_sign=An5ns1Kso7MWUdW4ErQKJJJ4qi4-AB0icniyvWSGgTYS2MPeuSWdNrYN&transaction%5B1%5D.amount=USD+200.00&test_ipn=1&transaction%5B0%5D.id_for_sender_txn=0DV12165W2314574U&transaction%5B0%5D.receiver=marcelle%40turcotte.info&cancel_url=http%3A//088da0ca.ngrok.io/payments/paypal/cancel&transaction%5B1%5D.is_primary_receiver=false&transaction%5B0%5D.is_primary_receiver=false&pay_key=AP-45250901MC1380023&action_type=PAY&transaction%5B0%5D.paymentType=SERVICE&transaction%5B0%5D.status_for_sender_txn=Pending&transaction%5B0%5D.pending_reason=UNILATERAL&transaction%5B1%5D.receiver=jeremy%40remixjobs.com&transaction_type=Adaptive+Payment+PAY&transaction%5B0%5D.amount=USD+101.00&status=COMPLETED&log_default_shipping_address_in_transaction=false&charset=windows-1252&notify_version=UNVERSIONED&reverse_all_parallel_payments_on_error=false"
15
+ expect {
16
+ @request.env['RAW_POST_DATA'] = payload
17
+ post :ipn
18
+ }.to raise_error(ActivePayment::NoTransactionError)
19
+ end
20
+
21
+ it 'should set transaction success if paypal return success' do
22
+ key = "AP-45250901MC1380023"
23
+ payload = "transaction%5B1%5D.status_for_sender_txn=Pending&transaction%5B1%5D.pending_reason=UNILATERAL&payment_request_date=Wed+Aug+19+11%3A39%3A12+PDT+2015&return_url=http%3A//088da0ca.ngrok.io/payments/paypal/success&fees_payer=EACHRECEIVER&ipn_notification_url=http%3A//088da0ca.ngrok.io/payments/paypal/ipn&transaction%5B1%5D.paymentType=SERVICE&transaction%5B1%5D.id_for_sender_txn=60M289731B8092737&sender_email=support-buyer%40streamup.com&verify_sign=An5nl1Kso7MWUdW4ErQKJJJ4qi4-AB0icniyvWSGgTYS2MPeuSWdNrYN&transaction%5B1%5D.amount=USD+200.00&test_ipn=1&transaction%5B0%5D.id_for_sender_txn=0DV12165W2314574U&transaction%5B0%5D.receiver=marcelle%40turcotte.info&cancel_url=http%3A//088da0ca.ngrok.io/payments/paypal/cancel&transaction%5B1%5D.is_primary_receiver=false&transaction%5B0%5D.is_primary_receiver=false&pay_key=AP-45250901MC1380023&action_type=PAY&transaction%5B0%5D.paymentType=SERVICE&transaction%5B0%5D.status_for_sender_txn=Pending&transaction%5B0%5D.pending_reason=UNILATERAL&transaction%5B1%5D.receiver=jeremy%40remixjobs.com&transaction_type=Adaptive+Payment+PAY&transaction%5B0%5D.amount=USD+101.00&status=COMPLETED&log_default_shipping_address_in_transaction=false&charset=windows-1252&notify_version=UNVERSIONED&reverse_all_parallel_payments_on_error=false"
24
+ params = {
25
+ external_id: key,
26
+ gateway: 'paypal_adaptive_payment',
27
+ currency: 'usd',
28
+ ip_address: '127.0.0.1',
29
+ amount: 15000,
30
+ payee_id: 1,
31
+ payer_id: 1
32
+ }
33
+ ActivePayment::Transaction.create(params)
34
+
35
+ transaction = ActivePayment::Transaction.find_by(external_id: key)
36
+ expect(transaction.state).to eq 'pending'
37
+ ActivePayment.configuration.ip_security = false
38
+
39
+ expect {
40
+ @request.env['RAW_POST_DATA'] = payload
41
+ post :ipn
42
+ }.to raise_error(ActivePayment::InvalidGatewayResponseError)
43
+
44
+ transaction = ActivePayment::Transaction.find_by(external_id: key)
45
+ expect(transaction.state).to eq 'error'
46
+ end
47
+
48
+ it 'should set transaction success if paypal return success' do
49
+ key = "AP-45250901MC1380023"
50
+ payload = "transaction%5B1%5D.status_for_sender_txn=Pending&transaction%5B1%5D.pending_reason=UNILATERAL&payment_request_date=Wed+Aug+19+11%3A39%3A12+PDT+2015&return_url=http%3A//088da0ca.ngrok.io/payments/paypal/success&fees_payer=EACHRECEIVER&ipn_notification_url=http%3A//088da0ca.ngrok.io/payments/paypal/ipn&transaction%5B1%5D.paymentType=SERVICE&transaction%5B1%5D.id_for_sender_txn=60M289731B8092737&sender_email=support-buyer%40streamup.com&verify_sign=An5nl1Kso7MWUdW4ErQKJJJ4qi4-AB0icniyvWSGgTYS2MPeuSWdNrYN&transaction%5B1%5D.amount=USD+200.00&test_ipn=1&transaction%5B0%5D.id_for_sender_txn=0DV12165W2314574U&transaction%5B0%5D.receiver=marcelle%40turcotte.info&cancel_url=http%3A//088da0ca.ngrok.io/payments/paypal/cancel&transaction%5B1%5D.is_primary_receiver=false&transaction%5B0%5D.is_primary_receiver=false&pay_key=AP-45250901MC1380023&action_type=PAY&transaction%5B0%5D.paymentType=SERVICE&transaction%5B0%5D.status_for_sender_txn=Pending&transaction%5B0%5D.pending_reason=UNILATERAL&transaction%5B1%5D.receiver=jeremy%40remixjobs.com&transaction_type=Adaptive+Payment+PAY&transaction%5B0%5D.amount=USD+101.00&status=COMPLETED&log_default_shipping_address_in_transaction=false&charset=windows-1252&notify_version=UNVERSIONED&reverse_all_parallel_payments_on_error=false"
51
+
52
+ params = {
53
+ external_id: key,
54
+ gateway: 'paypal_adaptive_payment',
55
+ currency: 'usd',
56
+ ip_address: '127.0.0.1',
57
+ amount: 15000,
58
+ payee_id: 1,
59
+ payer_id: 1
60
+ }
61
+ ActivePayment::Transaction.create(params)
62
+ allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return(params[:ip_address])
63
+
64
+ transaction = ActivePayment::Transaction.find_by(external_id: key)
65
+ expect(transaction.state).to eq 'pending'
66
+
67
+ allow_any_instance_of(ActiveMerchant::Billing::Integrations::PaypalAdaptivePayment::Notification).to receive(:acknowledge).and_return(true)
68
+ allow_any_instance_of(ActiveMerchant::Billing::Integrations::PaypalAdaptivePayment::Notification).to receive(:complete?).and_return(true)
69
+
70
+ @request.env['RAW_POST_DATA'] = payload
71
+ post :ipn
72
+
73
+ transaction = ActivePayment::Transaction.find_by(external_id: key)
74
+ expect(transaction.state).to eq 'completed'
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,102 @@
1
+ require 'helper'
2
+
3
+ RSpec.describe ActivePayment::PaypalExpressCheckoutCallbackController, type: :controller do
4
+ routes { ActivePayment::Engine.routes }
5
+
6
+ before(:each) do
7
+ class MockResponse
8
+ def success?
9
+ true
10
+ end
11
+
12
+ def payer_id
13
+ 1
14
+ end
15
+ end
16
+
17
+ @paypal_response = MockResponse.new
18
+ end
19
+
20
+ describe 'success' do
21
+ let!(:transaction) { create(:transaction) }
22
+
23
+ it 'should riase NoTransactionError if no token is passed' do
24
+ expect {
25
+ get :success
26
+ }.to raise_error(ActivePayment::NoTransactionError)
27
+ end
28
+
29
+ it 'should raise exception if wrong token' do
30
+ expect {
31
+ get :success, token: 'invalid_token'
32
+ }.to raise_error(ActivePayment::NoTransactionError)
33
+ end
34
+
35
+ it 'should raise SecurityError if not same ip address and ip_security is true' do
36
+ ActivePayment.configuration.ip_security = true
37
+ expect {
38
+ get :success, token: transaction.external_id
39
+ }.to raise_error(ActivePayment::SecurityError)
40
+ end
41
+
42
+ it 'should raise InvalidGatewayResponseError if gateway response is bad' do
43
+ allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return(transaction.ip_address)
44
+
45
+ expect {
46
+ get :success, token: transaction.external_id
47
+ }.to raise_error(ActivePayment::InvalidGatewayResponseError)
48
+ end
49
+
50
+ it 'should redirect to / and display success flash message if success' do
51
+ allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return(transaction.ip_address)
52
+ expect(transaction.state).to eq 'pending'
53
+
54
+ allow_any_instance_of(ActiveMerchant::Billing::PaypalExpressGateway).to receive(:details_for).and_return(@paypal_response)
55
+ allow_any_instance_of(ActiveMerchant::Billing::PaypalExpressGateway).to receive(:purchase).and_return(@paypal_response)
56
+ allow(DateTime).to receive(:now).and_return('2015-09-03 17:21:36.356460000 +0000')
57
+
58
+ get :success, token: transaction.external_id
59
+ expect(flash[:success]).to be_present
60
+ expect(response).to redirect_to('/')
61
+
62
+ trans = ActivePayment::Transaction.find(transaction.id)
63
+ expect(trans.state).to eq 'completed'
64
+ expect(trans.paid_at).to eq '2015-09-03 17:21:36.356460000 +0000'
65
+ end
66
+ end
67
+
68
+ describe 'cancel' do
69
+ let!(:transaction) { create(:transaction) }
70
+
71
+ it 'should raise NoTransactionError if no token' do
72
+ expect {
73
+ get :cancel
74
+ }.to raise_error(ActivePayment::NoTransactionError)
75
+ end
76
+
77
+ it 'should raise exception if wrong token' do
78
+ expect {
79
+ get :cancel, token: "invalid_token"
80
+ }.to raise_error(ActivePayment::NoTransactionError)
81
+ end
82
+
83
+ it 'should raise SecurityError if not same ip address' do
84
+ ActivePayment.configuration.ip_security = true
85
+ expect {
86
+ get :cancel, token: transaction.external_id
87
+ }.to raise_error(ActivePayment::SecurityError)
88
+ end
89
+
90
+ it 'should redirect to / and display error flash message if success' do
91
+ allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return(transaction.ip_address)
92
+ expect(transaction.state).to eq 'pending'
93
+
94
+ get :cancel, token: transaction.external_id
95
+ expect(flash[:error]).to be_present
96
+ expect(response).to redirect_to('/')
97
+
98
+ trans = ActivePayment::Transaction.find(transaction.id)
99
+ expect(trans.state).to eq 'canceled'
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,140 @@
1
+ require 'helper'
2
+
3
+ describe ActivePayment::Gateway do
4
+ it 'set activemerchant test if configuration test is set' do
5
+ ActivePayment.configuration.test = true
6
+ expect(ActiveMerchant::Billing::Base).to receive(:mode=).with(:test)
7
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
8
+ end
9
+
10
+ it 'set the paypal express checkout gateway' do
11
+ gateway = ActivePayment::Gateway.new('paypal_express_checkout')
12
+ expect(gateway.gateway.class).to eq(ActivePayment::Gateways::PaypalExpressCheckout)
13
+ end
14
+
15
+ it 'set the paypal adaptive payment gateway' do
16
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
17
+ expect(gateway.gateway.class).to eq(ActivePayment::Gateways::PaypalAdaptivePayment)
18
+ end
19
+
20
+ it 'call setup_purchase on the gateway' do
21
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
22
+ return_url = 'http://return.url'
23
+ purchase_token = 'token'
24
+ ip_address = '127.0.0.1'
25
+
26
+ expect(gateway.gateway).to receive(:setup_purchase).and_return(return_url)
27
+ expect(gateway.gateway).to receive(:purchase_token).and_return(purchase_token)
28
+ allow(gateway.gateway).to receive(:sales).and_return([])
29
+ gateway.setup_purchase(ActivePayment::Models::Sales.new, ip_address)
30
+ end
31
+
32
+ it 'set empty transactions' do
33
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
34
+ return_url = 'http://return.url'
35
+ purchase_token = 'token'
36
+ ip_address = '127.0.0.1'
37
+
38
+ expect(gateway.gateway).to receive(:setup_purchase).and_return(return_url)
39
+ expect(gateway.gateway).to receive(:purchase_token).and_return(purchase_token)
40
+ allow(gateway.gateway).to receive(:sales).and_return([])
41
+ gateway.setup_purchase(ActivePayment::Models::Sales.new, ip_address)
42
+ expect(gateway.transactions).to eq([])
43
+ end
44
+
45
+ it 'set correct transactions' do
46
+ payer = Payer.new
47
+ payee = Payee.new
48
+ payable = Payable.new
49
+ sale = ActivePayment::Models::Sale.new(payable: payable, payer: payer, payee: payee)
50
+ sales = ActivePayment::Models::Sales.new([sale])
51
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
52
+ return_url = 'http://return.url'
53
+ purchase_token = 'token'
54
+ ip_address = '127.0.0.1'
55
+
56
+ expect(gateway.gateway).to receive(:setup_purchase).and_return(return_url)
57
+ expect(gateway.gateway).to receive(:purchase_token).and_return(purchase_token)
58
+ allow(gateway.gateway).to receive(:sales).and_return([sale])
59
+ gateway.setup_purchase(sales, ip_address)
60
+ expect(gateway.transactions.length).to eq(1)
61
+ expect(gateway.transactions.first.amount).to eq(100)
62
+ expect(gateway.transactions.first.payee_id).to eq(1)
63
+ expect(gateway.transactions.first.payer_id).to eq(2)
64
+ expect(gateway.transactions.first.payable_id).to eq(3)
65
+ expect(gateway.transactions.first.reference_number).to eq('3')
66
+ expect(gateway.transactions.first.payable_type).to eq('Payable')
67
+ expect(gateway.transactions.first.gateway).to eq('ActivePayment::Gateways::PaypalAdaptivePayment')
68
+ expect(gateway.transactions.first.ip_address).to eq('127.0.0.1')
69
+ expect(gateway.transactions.first.state).to eq('pending')
70
+ expect(gateway.transactions.first.metadata[:description]).to eq('description')
71
+ end
72
+
73
+ it 'must create transactions after calling setup_purchase' do
74
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
75
+ return_url = 'http://return.url'
76
+ purchase_token = 'token'
77
+ ip_address = '127.0.0.1'
78
+
79
+ allow(gateway.gateway).to receive(:setup_purchase).and_return(return_url)
80
+ allow(gateway.gateway).to receive(:purchase_token).and_return(purchase_token)
81
+ allow(gateway.gateway).to receive(:sales).and_return([])
82
+ expect(gateway).to receive(:create_transactions).with(ip_address)
83
+ gateway.setup_purchase(ActivePayment::Models::Sales.new, ip_address)
84
+ end
85
+
86
+ it 'raise NoTransactionError when calling verify_purchase with invalid external_id' do
87
+ external_id = 1
88
+ ip_address = '127.0.0.1'
89
+ raw_data = {}
90
+
91
+ expect {
92
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
93
+ gateway.verify_purchase(external_id, ip_address, raw_data)
94
+ }.to raise_error(ActivePayment::NoTransactionError)
95
+ end
96
+
97
+ it 'set the transactions to error if gateway response is wrong' do
98
+ external_id = 1
99
+ ip_address = '127.0.0.1'
100
+ raw_data = {}
101
+ transaction = create(:transaction)
102
+
103
+ expect {
104
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
105
+ allow(ActivePayment::Transaction).to receive(:where).with(external_id: external_id).and_return([transaction])
106
+ allow(gateway.gateway).to receive(:verify_purchase).and_return(false)
107
+ expect(gateway).to receive(:transactions_error)
108
+
109
+ gateway.verify_purchase(external_id, ip_address, raw_data)
110
+ }.to raise_error(ActivePayment::InvalidGatewayResponseError)
111
+ end
112
+
113
+ it 'set the transactions to success if gateway response is ok' do
114
+ external_id = 1
115
+ ip_address = '127.0.0.1'
116
+ raw_data = {}
117
+ transaction = create(:transaction)
118
+
119
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
120
+ allow(ActivePayment::Transaction).to receive(:where).with(external_id: external_id).and_return([transaction])
121
+ allow(gateway.gateway).to receive(:verify_purchase).and_return(true)
122
+ expect(gateway).to receive(:transactions_success)
123
+
124
+ gateway.verify_purchase(external_id, ip_address, raw_data)
125
+ end
126
+
127
+ it 'call livemode? on the gateway' do
128
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
129
+ expect(gateway.gateway).to receive(:livemode?)
130
+ gateway.livemode?
131
+ end
132
+ #
133
+ it 'call external_id_from_request on the gateway' do
134
+ request = {}
135
+ gateway = ActivePayment::Gateway.new('paypal_adaptive_payment')
136
+ expect(gateway.gateway).to receive(:external_id_from_request).with(request)
137
+ gateway.external_id_from_request(request)
138
+ end
139
+ end
140
+
@@ -0,0 +1,105 @@
1
+ require 'helper'
2
+
3
+ describe ActivePayment::Gateways::PaypalAdaptivePayment do
4
+ before(:each) do
5
+ @payer = Payer.new
6
+ @payee = Payee.new
7
+ @payable = Payable.new
8
+ @sale = ActivePayment::Models::Sale.new(payable: @payable, payer: @payer, payee: @payee)
9
+ @payee2 = Payee.new
10
+ @payee2.paypal_identifier = 'test2@paypal.com'
11
+ @payee3 = Payee.new
12
+ @payee3.paypal_identifier = 'test3@paypal.com'
13
+ @sale2 = ActivePayment::Models::Sale.new(payable: @payable, payer: @payer, payee: @payee2)
14
+ @sale3 = ActivePayment::Models::Sale.new(payable: @payable, payer: @payer, payee: @payee3)
15
+
16
+ @sales = ActivePayment::Models::Sales.new([@sale, @sale2, @sale3])
17
+ @gateway = ActivePayment::Gateways::PaypalAdaptivePayment.new
18
+ class SuccessMockResponse
19
+ def success?
20
+ true
21
+ end
22
+
23
+ def [](payKey)
24
+ "payKey"
25
+ end
26
+ end
27
+ class FailedMockResponse
28
+ def success?
29
+ false
30
+ end
31
+ end
32
+ class SuccessVerifyPurchaseMockResponse
33
+ def acknowledge
34
+ true
35
+ end
36
+ def complete?
37
+ true
38
+ end
39
+ def [](request)
40
+ "pay_key"
41
+ end
42
+ end
43
+ @success_gateway_response = SuccessMockResponse.new
44
+ @failed_gateway_response = FailedMockResponse.new
45
+ @success_verify_response = SuccessVerifyPurchaseMockResponse.new
46
+ end
47
+
48
+ describe 'initialize' do
49
+ it 'initialize the correct gateway' do
50
+ expect(ActiveMerchant::Billing::PaypalAdaptivePayment).to receive(:new).once
51
+ ActivePayment::Gateways::PaypalAdaptivePayment.new
52
+ end
53
+ end
54
+
55
+ describe 'setup_purchase' do
56
+ it 'call setup_purchase on the gateway' do
57
+ expect_any_instance_of(ActiveMerchant::Billing::PaypalAdaptivePayment).to receive(:setup_purchase).once.and_return(@success_gateway_response)
58
+ @gateway.setup_purchase(@sales)
59
+ end
60
+
61
+ it 'raise InvalidGatewayResponse if response is not success' do
62
+ expect {
63
+ expect_any_instance_of(ActiveMerchant::Billing::PaypalAdaptivePayment).to receive(:setup_purchase).once.and_return(@failed_gateway_response)
64
+ @gateway.setup_purchase(@sales)
65
+ }.to raise_error(ActivePayment::InvalidGatewayResponseError)
66
+ end
67
+ end
68
+
69
+ describe 'verify_purchase' do
70
+ it 'calls notification on PaypalAdaptivePayment' do
71
+ params = {}
72
+ expect(ActiveMerchant::Billing::Integrations::PaypalAdaptivePayment).to receive(:notification).with(params).once.and_return(@success_verify_response)
73
+ response = @gateway.verify_purchase(params)
74
+ expect(response).to be(true)
75
+ end
76
+ end
77
+
78
+ describe 'external_id_from_request' do
79
+ it 'returns notification pay_key params' do
80
+ class RequestMock
81
+ def raw_post
82
+ {}
83
+ end
84
+ end
85
+ request = RequestMock.new
86
+
87
+ class ResponseMock
88
+ attr_accessor :params
89
+ def initialize
90
+ @params = {'pay_key' => 'key'}
91
+ end
92
+ end
93
+
94
+ expect(ActiveMerchant::Billing::Integrations::PaypalAdaptivePayment).to receive(:notification).with(request.raw_post).once.and_return(ResponseMock.new)
95
+ expect(@gateway.external_id_from_request(request)).to eq("key")
96
+ end
97
+ end
98
+
99
+ describe 'livemode?' do
100
+ it 'calls livemode? on the gateway' do
101
+ expect(ActiveMerchant::Billing::Base).to receive(:mode).once
102
+ @gateway.livemode?
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,144 @@
1
+ require 'helper'
2
+
3
+ describe ActivePayment::Gateways::PaypalExpressCheckout do
4
+ before(:each) do
5
+ class Payee
6
+ include ActivePayment::Models::Payee
7
+ attr_accessor :paypal_identifier
8
+
9
+ def paypal_identifier
10
+ "test@paypal.com"
11
+ end
12
+ end
13
+ class Payer
14
+ include ActivePayment::Models::Payer
15
+ end
16
+ class Payable
17
+ include ActivePayment::Models::Payable
18
+
19
+ def amount
20
+ 100
21
+ end
22
+
23
+ def description
24
+ "description"
25
+ end
26
+
27
+ def tax
28
+ 10
29
+ end
30
+
31
+ def shipping
32
+ 20
33
+ end
34
+ end
35
+
36
+ @payer = Payer.new
37
+ @payee = Payee.new
38
+ @payable = Payable.new
39
+ @sale = ActivePayment::Models::Sale.new(payable: @payable, payer: @payer, payee: @payee)
40
+ @payee2 = Payee.new
41
+ @payee2.paypal_identifier = "test2@paypal.com"
42
+ @payee3 = Payee.new
43
+ @payee3.paypal_identifier = "test3@paypal.com"
44
+ @sale2 = ActivePayment::Models::Sale.new(payable: @payable, payer: @payer, payee: @payee2)
45
+ @sale3 = ActivePayment::Models::Sale.new(payable: @payable, payer: @payer, payee: @payee3)
46
+
47
+ @sales = ActivePayment::Models::Sales.new([@sale, @sale2, @sale3])
48
+ @gateway = ActivePayment::Gateways::PaypalExpressCheckout.new
49
+ class SuccessMockResponse
50
+ def success?
51
+ true
52
+ end
53
+
54
+ def payer_id
55
+ 1
56
+ end
57
+
58
+ def token
59
+ "token"
60
+ end
61
+ end
62
+ class FailedMockResponse
63
+ def success?
64
+ false
65
+ end
66
+
67
+ def payer_id
68
+ 1
69
+ end
70
+
71
+ def token
72
+ "token"
73
+ end
74
+ end
75
+ @success_gateway_response = SuccessMockResponse.new
76
+ @failed_gateway_response = FailedMockResponse.new
77
+ end
78
+
79
+ describe 'initialize' do
80
+ it 'initialize the correct gateway' do
81
+ expect(ActiveMerchant::Billing::PaypalExpressGateway).to receive(:new).once
82
+ ActivePayment::Gateways::PaypalExpressCheckout.new
83
+ end
84
+ end
85
+
86
+ describe 'setup_purchase' do
87
+ it 'call setup_purchase on the gateway' do
88
+ expect_any_instance_of(ActiveMerchant::Billing::PaypalExpressGateway).to receive(:setup_purchase).once.and_return(@success_gateway_response)
89
+ @gateway.setup_purchase(@sales)
90
+ end
91
+
92
+ it 'raise InvalidGatewayResponse if response is not success' do
93
+ expect {
94
+ expect_any_instance_of(ActiveMerchant::Billing::PaypalExpressGateway).to receive(:setup_purchase).once.and_return(@failed_gateway_response)
95
+ @gateway.setup_purchase(@sales)
96
+ }.to raise_error(ActivePayment::InvalidGatewayResponseError)
97
+ end
98
+ end
99
+
100
+ describe 'verify_purchase' do
101
+ it 'calls notification on PaypalAdaptivePayment' do
102
+ params = {:token => 'token', :amount => 100}
103
+ expect_any_instance_of(ActiveMerchant::Billing::PaypalExpressGateway).to receive(:details_for).with('token').once.and_return(@success_gateway_response)
104
+ expect_any_instance_of(ActiveMerchant::Billing::PaypalExpressGateway).to receive(:purchase).once.and_return(@success_gateway_response)
105
+ @gateway.verify_purchase(params)
106
+ end
107
+
108
+ it 'returns false is response if a failure' do
109
+ params = {:token => 'token', :amount => 100}
110
+ expect_any_instance_of(ActiveMerchant::Billing::PaypalExpressGateway).to receive(:details_for).with('token').once.and_return(@failed_gateway_response)
111
+ response = @gateway.verify_purchase(params)
112
+ expect(response).to be(false)
113
+ end
114
+
115
+ it 'returns true if response is success' do
116
+ params = {:token => 'token', :amount => 100}
117
+ expect_any_instance_of(ActiveMerchant::Billing::PaypalExpressGateway).to receive(:details_for).with('token').once.and_return(@success_gateway_response)
118
+ expect_any_instance_of(ActiveMerchant::Billing::PaypalExpressGateway).to receive(:purchase).once.and_return(@success_gateway_response)
119
+ response = @gateway.verify_purchase(params)
120
+ expect(response).to be(true)
121
+ end
122
+ end
123
+
124
+ describe 'external_id_from_request' do
125
+ it 'return the param token' do
126
+ class Request
127
+ attr_accessor :params
128
+ def initialize
129
+ @params = {:token => 'key'}
130
+ end
131
+ end
132
+
133
+ response = @gateway.external_id_from_request(Request.new)
134
+ expect(response).to eq('key')
135
+ end
136
+ end
137
+
138
+ describe 'livemode?' do
139
+ it 'calls livemode? on the gateway' do
140
+ expect(ActiveMerchant::Billing::Base).to receive(:mode).once
141
+ @gateway.livemode?
142
+ end
143
+ end
144
+ end