nimbleshop_stripe 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. data/app/controllers/nimbleshop_stripe/payments_controller.rb +9 -6
  2. data/app/controllers/nimbleshop_stripe/stripes_controller.rb +8 -22
  3. data/app/models/nimbleshop_stripe/stripe.rb +1 -1
  4. data/app/views/nimbleshop_stripe/payments/_new.html.erb +6 -2
  5. data/app/views/nimbleshop_stripe/stripes/_edit.html.erb +59 -24
  6. data/lib/nimbleshop_stripe/processor.rb +32 -64
  7. data/lib/tasks/nimbleshop_stripe_tasks.rake +2 -2
  8. data/test/unit/payment_method_test.rb +7 -7
  9. data/test/unit/processor_test.rb +35 -199
  10. data/test/vcr_cassettes/stripe/purchase-failure.yml +49 -0
  11. data/test/vcr_cassettes/stripe/purchase-success-for-refund.yml +113 -0
  12. data/test/vcr_cassettes/stripe/purchase-success.yml +112 -0
  13. data/test/vcr_cassettes/stripe/refund-success.yml +62 -0
  14. metadata +12 -28
  15. data/app/views/nimbleshop_stripe/stripes/_form.html.erb +0 -68
  16. data/lib/nimbleshop_stripe/util.rb +0 -32
  17. data/test/vcr_cassettes/authorize_net/authorize-failure.yml +0 -38
  18. data/test/vcr_cassettes/authorize_net/authorize-success.yml +0 -38
  19. data/test/vcr_cassettes/authorize_net/capture-failure.yml +0 -38
  20. data/test/vcr_cassettes/authorize_net/capture-success.yml +0 -38
  21. data/test/vcr_cassettes/authorize_net/purchase-failure.yml +0 -38
  22. data/test/vcr_cassettes/authorize_net/purchase-success.yml +0 -38
  23. data/test/vcr_cassettes/authorize_net/refund-failure.yml +0 -38
  24. data/test/vcr_cassettes/authorize_net/refund-success.yml +0 -38
  25. data/test/vcr_cassettes/authorize_net/void-authorize.yml +0 -38
  26. data/test/vcr_cassettes/authorize_net/void-failure.yml +0 -38
  27. data/test/vcr_cassettes/authorize_net/void-success.yml +0 -38
@@ -3,13 +3,12 @@ module NimbleshopStripe
3
3
  class PaymentsController < ::ActionController::Base
4
4
 
5
5
  def create
6
- order = Order.find_by_id!(session[:order_id])
6
+ order = Order.find_by_id! session[:order_id]
7
7
  token = params[:stripeToken]
8
8
 
9
9
  address_attrs = order.final_billing_address.to_credit_card_attributes
10
- creditcard_attrs = params[:creditcard].merge(address_attrs)
11
- creditcard = Creditcard.new(creditcard_attrs)
12
- creditcard.perform_validations = false
10
+ creditcard_attrs = creditcard_params.merge address_attrs
11
+ creditcard = Creditcard.new creditcard_attrs
13
12
 
14
13
  payment_method = NimbleshopStripe::Stripe.first
15
14
  processor = NimbleshopStripe::Processor.new({order: order, payment_method: payment_method})
@@ -17,7 +16,7 @@ module NimbleshopStripe
17
16
  default_action = Shop.current.default_creditcard_action
18
17
 
19
18
  if processor.send(default_action, token: token)
20
- url = nimbleshop_simply.order_path(order)
19
+ url = nimbleshop_simply.order_path order
21
20
  @output = "window.location='#{url}'"
22
21
  else
23
22
  error = processor.errors.first
@@ -33,6 +32,10 @@ module NimbleshopStripe
33
32
 
34
33
  end
35
34
 
36
- end
35
+ private
37
36
 
37
+ def creditcard_params
38
+ params.require(:creditcard).permit(:number, :"expires_on(3i)", :"expires_on(2i)", :"expires_on(1i)", :cvv)
39
+ end
40
+ end
38
41
  end
@@ -8,30 +8,16 @@ module NimbleshopStripe
8
8
  before_filter :load_payment_method
9
9
 
10
10
  def update
11
- respond_to do |format|
12
- if @payment_method.update_attributes(post_params[:stripe])
13
- format.js {
14
- flash[:notice] = 'Stripe record was successfully updated'
15
- render js: "window.location = '/admin/payment_methods'"
16
- }
17
- else
18
- msg = @payment_method.errors.full_messages.first
19
- error = %Q[alert("#{msg}")]
20
- format.js { render js: error }
21
- end
22
- end
23
- end
11
+ alert_msg = if @payment_method.update_attributes(post_params[:stripe])
12
+ msg = "Record has been updated"
13
+ %Q[alert("#{msg}")]
14
+ else
15
+ msg = @payment_method.errors.full_messages.first
16
+ %Q[alert("#{msg}")]
17
+ end
24
18
 
25
- def destroy
26
19
  respond_to do |format|
27
- if @payment_method.destroy
28
- format.js {
29
- flash[:notice] = 'Stripe record was successfully deleted'
30
- render js: "window.location = '/admin/payment_methods'"
31
- }
32
- else
33
- format.js { render js: 'Stripe record could not be deleted. Please try again later.' }
34
- end
20
+ format.js { render js: alert_msg }
35
21
  end
36
22
  end
37
23
 
@@ -12,7 +12,7 @@ module NimbleshopStripe
12
12
  end
13
13
 
14
14
  def kapture!(order)
15
- processor = NimbleshopAuthorizedotnet::Processor.new(order)
15
+ processor = NimbleshopAuthorizedotnet::Processor.new order
16
16
  processor.kapture
17
17
  order.kapture!
18
18
  end
@@ -13,7 +13,7 @@
13
13
  </p>
14
14
 
15
15
  <%= form_for @creditcard, url: nimbleshop_stripe.payment_path(format: :js), remote: true, html: { id: 'stripe-payment-form', class: 'form-horizontal' } do |f| %>
16
- <div class='nimbleshop_stripe_umbrella'>
16
+ <div class='nimbleshop_stripe_umbrella' style="margin-left:-125px;">
17
17
  <% if @creditcard.errors.any? %>
18
18
  <div class="alert alert-error">
19
19
  <ul>
@@ -97,7 +97,11 @@ $(document).ready(function(){
97
97
  $.ajax({
98
98
  type: 'POST',
99
99
  url: $this.attr('action'),
100
- data: $this.serializeArray()
100
+ data: $this.serializeArray(),
101
+ error: function(){
102
+ $('[data-behavior~=stripe-submit-button]').attr('value', 'Submit').removeAttr('disabled');
103
+ alert("Something has gone wrong")
104
+ }
101
105
  });
102
106
 
103
107
 
@@ -1,31 +1,66 @@
1
- <div class='payment-method-engine-well'>
2
-
3
- <div>
4
- <h2>Stripe</h2>
5
- <div class="edit_link">
6
- <%= link_to 'Edit', '#', class: 'nimbleshop-payment-method-edit', id: 'nimbleshop-stripe-payment-method-edit' %>
7
- <%= link_to nimbleshop_stripe.stripe_path, class: 'nimbleshop-payment-method-delete',
8
- confirm: 'Do you really want to delete Stripe payment method', method: :delete, remote: true do %>
9
- <i class='icon-remove icon-white'></i>
10
- <% end %>
11
- </div>
1
+ <% payment_method = NimbleshopStripe::Stripe.first %>
12
2
 
3
+ <%= content_tag :div, style: 'display:none', data: { behavior: "payment-method-form-#{payment_method.permalink}" } do %>
4
+ <div style="margin-left:50px;margin-bottom:20px;">
13
5
  <%= nimbleshop_stripe_picture_on_admin_payment_methods %>
14
-
15
- <div class='clear'></div>
16
6
  </div>
17
7
 
18
- <%= render partial: '/nimbleshop_stripe/stripes/form' %>
19
- </div>
8
+ <%= form_for payment_method, url: '/nimbleshop_stripe/stripe',
9
+ remote: true,
10
+ html: { method: 'put',
11
+ id: 'nimbleshop-stripe-form',
12
+ class: 'nimbleshop-payment-method-form form-horizontal'} do |f| %>
13
+
14
+ <fieldset>
15
+ <div class='control-group'>
16
+ <%= f.label :publishable_key, nil, class: 'control-label' %>
17
+ <div class='controls'>
18
+ <%= f.text_field :publishable_key, class: 'span6' %>
19
+ </div>
20
+ </div>
21
+
22
+ <div class='control-group'>
23
+ <%= f.label :secret_key, nil, class: 'control-label' %>
24
+ <div class='controls'>
25
+ <%= f.text_field :secret_key, class: 'span6' %>
26
+ </div>
27
+ </div>
20
28
 
21
- <script>
22
- $(function(){
29
+ <div class='control-group'>
30
+ <%= f.label :business_name, nil, class: 'control-label' %>
31
+ <%= link_to '?', '#', 'data-content' => 'Please enter the name of the company as it would appear in the credit card statement. If are not sure what name would appear in the credit card statements then consult your merchant account provider.',
32
+ 'data-original-title' => 'Business name' %>
33
+ <div class='controls'>
34
+ <%= f.text_field :business_name, class: 'span6' %>
35
+ </div>
36
+ </div>
23
37
 
24
- $('#nimbleshop-stripe-payment-method-edit').toggle(function(){
25
- $('#nimbleshop-stripe-form-well').show();
26
- }, function(){
27
- $('#nimbleshop-stripe-form-well').hide();
28
- });
38
+ <div class='control-group'>
39
+ <div class='controls'>
40
+ <label class='checkbox'>
41
+ <%= f.check_box :mode, {}, 'test', 'production' %> Enable test mode
42
+ <%= link_to '?', '#', class: 'help', 'data-content' => 'In the test mode real credit card numbers are not accepted.',
43
+ 'data-original-title' => 'Test mode' %>
44
+ </label>
45
+ </div>
46
+ </div>
29
47
 
30
- })
31
- </script>
48
+ <div class='control-group'>
49
+ <div class='controls'>
50
+ <label class='checkbox'>
51
+ <%= f.check_box :ssl, {}, 'enabled', 'disabled' %> Enable SSL requirement
52
+ <%= link_to '?', '#', class: 'help', 'data-content' => 'Enabling SSL requirement will force the payment page to be on SSL.',
53
+ 'data-original-title' => 'SSL requirement' %>
54
+ </label>
55
+ </div>
56
+ </div>
57
+
58
+ </fieldset>
59
+
60
+ <div class='form-actions'>
61
+ <%= f.submit('Submit', class: 'btn btn-primary') %>
62
+ &nbsp;
63
+ <%= link_to 'Disable', main_app.disable_admin_payment_method_path(payment_method), class: 'disable-payment-method-link', method: :put %>
64
+ </div>
65
+ <% end %>
66
+ <% end %>
@@ -5,9 +5,9 @@ module NimbleshopStripe
5
5
 
6
6
  def initialize(options)
7
7
  @errors = []
8
- @order = options.fetch(:order)
9
- @payment_method = options.fetch(:payment_method)
10
- @gateway = ::NimbleshopStripe::Gateway.instance(payment_method)
8
+ @order = options.fetch :order
9
+ @payment_method = options.fetch :payment_method
10
+ @gateway = ::NimbleshopStripe::Gateway.instance payment_method
11
11
  ::Stripe.api_key = payment_method.secret_key
12
12
  end
13
13
 
@@ -18,8 +18,7 @@ module NimbleshopStripe
18
18
  end
19
19
 
20
20
  def do_authorize(options = {})
21
- #Stripe does not support authorize operation
22
- do_purchase(options)
21
+ do_purchase options
23
22
  end
24
23
 
25
24
  # Creates purchase for the order amount.
@@ -33,53 +32,42 @@ module NimbleshopStripe
33
32
  #
34
33
  def do_purchase(options = {})
35
34
  options.symbolize_keys!
36
- options.assert_valid_keys(:token)
35
+ options.assert_valid_keys :token
37
36
 
38
- token = options[:token]
37
+ token = options.fetch :token
39
38
 
40
39
  response = gateway.purchase(order.total_amount_in_cents, token)
41
- token_response = ::Stripe::Token.retrieve(token)
42
-
43
- record_transaction(response, 'purchased',
44
- fingerprint: token_response.card.fingerprint,
45
- livemode: token_response.livemode,
46
- card_number: "XXXX-XXXX-XXXX-#{token_response.card.last4}",
47
- cardtype: token_response.card.type.downcase,
48
- transaction_gid: token_response.id)
49
40
 
50
41
  if response.success?
42
+ # record transaction only if the operation was a success. Because if the operation was a failure then
43
+ # getting Token.retrieve will throw exception
44
+ ::Nimbleshop::PaymentTransactionRecorder.new(payment_transaction_recorder_hash(response, token)).record
45
+
51
46
  order.update_attributes(payment_method: payment_method)
52
47
  order.purchase!
53
48
  else
54
49
  Rails.logger.info response.params['error']['message']
55
50
  @errors << 'Credit card was declined. Please try again!'
56
- return false
57
51
  end
58
- end
59
52
 
60
- # Voids the previously authorized transaction.
61
- #
62
- # === Options
63
- #
64
- # * <tt>:transaction_gid</tt> -- transaction_gid is the transaction id returned by the gateway. This is a required field.
65
- #
66
- # This method returns false if void fails. Error messages are in <tt>errors</tt> array.
67
- # If void succeeds then <tt>order.void</tt> is invoked.
68
- #
69
- def do_void(options = {})
70
- options.symbolize_keys!
71
- options.assert_valid_keys(:transaction_gid)
72
- transaction_gid = options[:transaction_gid]
53
+ response.success?
54
+ end
73
55
 
74
- response = gateway.void(transaction_gid, {})
75
- record_transaction(response, 'voided')
56
+ def payment_transaction_recorder_hash(response, token)
57
+ token_response = ::Stripe::Token.retrieve token
58
+
59
+ { response: response,
60
+ order: order,
61
+ operation: :purchased,
62
+ transaction_gid: response.authorization,
63
+ metadata: { fingerprint: token_response.card.fingerprint,
64
+ livemode: token_response.livemode,
65
+ card_number: "XXXX-XXXX-XXXX-#{token_response.card.last4}",
66
+ cardtype: token_response.card.type.downcase }}
67
+ end
76
68
 
77
- if response.success?
78
- order.void
79
- else
80
- @errors << "Void operation failed"
81
- false
82
- end
69
+ def do_void(options = {})
70
+ raise 'Stripe does not support void operation'
83
71
  end
84
72
 
85
73
  # Refunds the given transaction.
@@ -93,41 +81,21 @@ module NimbleshopStripe
93
81
  #
94
82
  def do_refund(options = {})
95
83
  options.symbolize_keys!
96
- options.assert_valid_keys(:transaction_gid, :card_number)
84
+ options.assert_valid_keys :transaction_gid, :card_number
97
85
 
98
- transaction_gid = options[:transaction_gid]
99
- card_number = options[:card_number]
86
+ transaction_gid = options.fetch :transaction_gid
87
+ card_number = options.fetch :card_number
100
88
 
101
89
  response = gateway.refund(order.total_amount_in_cents, transaction_gid, card_number: card_number)
102
- record_transaction(response, 'refunded')
103
90
 
104
91
  if response.success?
92
+ ::Nimbleshop::PaymentTransactionRecorder.new(transaction_gid: response.authorization, response: response, operation: :refunded, order: order).record
105
93
  order.refund
106
94
  else
107
- @errors << "Refund failed"
108
- false
95
+ @errors << "Refund operation failed"
109
96
  end
110
97
 
111
- end
112
-
113
- def record_transaction(response, operation, additional_options = {}) # :nodoc:
114
- transaction_gid = additional_options.fetch(:transaction_gid)
115
-
116
- options = { operation: 'capture',
117
- params: response.params,
118
- success: true,
119
- metadata: additional_options,
120
- transaction_gid: transaction_gid }
121
-
122
- if response.success?
123
- options.update(amount: order.total_amount_in_cents)
124
- end
125
-
126
- order.payment_transactions.create(options)
127
- end
128
-
129
- def valid_card?(creditcard) # :nodoc:
130
- true
98
+ response.success?
131
99
  end
132
100
 
133
101
  end
@@ -14,7 +14,7 @@ namespace :nimbleshop_stripe do
14
14
  task :load_record => :environment do
15
15
 
16
16
  if NimbleshopStripe::Stripe.find_by_permalink('stripe')
17
- puts "Stripe record already exists"
17
+ Rails.logger.debug "Stripe record already exists"
18
18
  else
19
19
  NimbleshopStripe::Stripe.create!(
20
20
  {
@@ -25,7 +25,7 @@ namespace :nimbleshop_stripe do
25
25
  permalink: 'stripe',
26
26
  description: %Q[<p> Stripe is a payment gateway service provider allowing merchants to accept credit card payments. </p>]
27
27
  })
28
- puts "Stripe record was successfuly created"
28
+ Rails.logger.debug "Stripe record was successfuly created"
29
29
  end
30
30
  end
31
31
  end
@@ -1,21 +1,21 @@
1
1
  require "test_helper"
2
2
 
3
- class PaymentMethodAuthorizeNetTest < ActiveSupport::TestCase
3
+ class PaymentMethodStripeTest < ActiveSupport::TestCase
4
4
 
5
5
  test "validations" do
6
- pm = NimbleshopAuthorizedotnet::Authorizedotnet.new(name: 'Authorize.net', description: 'this is description')
6
+ pm = NimbleshopStripe::Stripe.new(name: 'Stripe', description: 'this is description')
7
7
  refute pm.valid?
8
- expected = ["Business name can't be blank", "Login can't be blank", "Transaction key can't be blank"]
8
+ expected = ["Business name can't be blank", "Publishable key can't be blank", "Secret key can't be blank"].sort
9
9
  assert_equal expected, pm.errors.full_messages.sort
10
10
  end
11
11
 
12
12
  test "should save the record" do
13
- pm = NimbleshopAuthorizedotnet::Authorizedotnet.new(name: 'Authorize.net', description: 'this is description')
14
- pm.login_id = 'FWERSDEED093d'
15
- pm.transaction_key = 'SDFSDFSFSF423433SDFSFSSFSFSF334'
13
+ pm = NimbleshopStripe::Stripe.new(name: 'Stripe', description: 'this is description')
14
+ pm.publishable_key = 'FWERSDEED093d'
15
+ pm.secret_key = 'SDFSDFSFSF423433SDFSFSSFSFSF334'
16
16
  pm.business_name = 'BigBinary LLC'
17
17
  assert pm.save
18
- assert_match /authorize-net/, pm.permalink
18
+ assert_match /stripe/, pm.permalink
19
19
  end
20
20
 
21
21
  end
@@ -1,241 +1,77 @@
1
1
  require 'test_helper'
2
2
 
3
3
  module Processor
4
- class NimbleshopAuthorizeNetAuthorizeTest < ActiveRecord::TestCase
4
+ class NimbleshopStripePurchaseTest < ActiveRecord::TestCase
5
+
6
+ TOKEN = 'tok_0eLPgnVqeoGqWd'
7
+
5
8
  setup do
6
- payment_method = NimbleshopAuthorizedotnet::Authorizedotnet.first
9
+ payment_method = NimbleshopStripe::Stripe.first
10
+ assert_equal 'test', payment_method.mode.to_s
11
+ assert_equal 'Stripe', payment_method.name
7
12
  @order = create(:order)
8
13
  @order.stubs(:total_amount).returns(100.48)
9
- @processor = NimbleshopAuthorizedotnet::Processor.new(order: @order, payment_method: payment_method)
14
+ @processor = NimbleshopStripe::Processor.new(order: @order, payment_method: payment_method)
10
15
  end
11
16
 
12
- test 'when authorization succeeds' do
13
- creditcard = build(:creditcard)
17
+ test 'when purchase succeeds' do
18
+ transactions_count = @order.payment_transactions.count
14
19
 
15
- playcasette('authorize.net/authorize-success') do
16
- assert_equal true, @processor.authorize(creditcard: creditcard)
20
+ playcasette('stripe/purchase-success') do
21
+ assert_equal true, @processor.purchase(token: TOKEN)
17
22
  end
18
23
 
19
24
  @order.reload
20
25
 
21
26
  transaction = @order.payment_transactions.last
22
- assert_equal 'authorized', transaction.operation
23
- assert_equal true, transaction.success
24
- assert_equal NimbleshopAuthorizedotnet::Authorizedotnet.first, @order.payment_method
25
- assert @order.authorized?
26
- end
27
-
28
- test 'authorization fails when credit card number is not entered' do
29
- creditcard = build(:creditcard, number: nil)
30
- assert_equal false, @processor.authorize(creditcard: creditcard)
31
- assert_equal 'Please enter credit card number', @processor.errors.first
32
-
33
- @order.reload
34
-
35
- assert_nil @order.payment_method
36
- assert @order.abandoned?
27
+ assert_equal 'purchased', transaction.operation
28
+ assert_equal NimbleshopStripe::Stripe.first, @order.payment_method
29
+ assert @order.purchased?
30
+ assert_equal transactions_count + 1, @order.payment_transactions.count
31
+ assert_equal 'ch_0eLWIXVivQp0KE', transaction.transaction_gid
37
32
  end
38
33
 
39
- test 'authorization fails with invalid credit card number' do
40
- creditcard = build(:creditcard, number: 2)
34
+ test 'purchase fails when token number is not entered' do
35
+ transactions_count = @order.payment_transactions.count
41
36
 
42
- playcasette('authorize.net/authorize-failure') do
43
- assert_equal false, @processor.authorize(creditcard: creditcard)
44
- assert_equal 'Credit card was declined. Please try again!', @processor.errors.first
37
+ playcasette('stripe/purchase-failure') do
38
+ assert_equal false, @processor.purchase(token: 'xxxxxx')
39
+ assert_equal "Credit card was declined. Please try again!", @processor.errors.first
45
40
  end
46
41
 
47
42
  @order.reload
48
43
 
49
44
  assert_nil @order.payment_method
50
45
  assert @order.abandoned?
46
+ assert_equal transactions_count, @order.payment_transactions.count
51
47
  end
52
48
  end
53
49
 
54
- class AuthorizeNetCaptureTest < ActiveRecord::TestCase
55
- setup do
56
- payment_method = NimbleshopAuthorizedotnet::Authorizedotnet.first
57
- @order = create(:order, payment_method: payment_method)
58
- @order.stubs(:total_amount).returns(100.48)
59
- @processor = NimbleshopAuthorizedotnet::Processor.new(order: @order, payment_method: payment_method)
60
- creditcard = build(:creditcard)
61
-
62
- playcasette('authorize.net/authorize-success') do
63
- @processor.authorize(creditcard: creditcard)
64
- end
65
-
66
- @tsx_id = @order.payment_transactions.last.transaction_gid
67
- end
68
-
69
- test 'when capture succeeds' do
70
- creditcard = build(:creditcard)
71
-
72
- playcasette('authorize.net/capture-success') do
73
- assert_equal true, @processor.kapture(transaction_gid: @tsx_id)
74
- end
50
+ class NimbleshopStripeRefundTest < ActiveRecord::TestCase
75
51
 
76
- @order.reload
77
- transaction = @order.payment_transactions.last
78
- assert_equal 'captured', transaction.operation
79
- assert_equal true, transaction.success
80
- assert @order.purchased?
81
- end
82
-
83
- test 'when capture fails' do
84
- creditcard = build(:creditcard, number: 2)
85
-
86
- playcasette('authorize.net/capture-failure') do
87
- assert_equal false, @processor.kapture(transaction_gid: @tsx_id)
88
- assert_equal 'Capture request failed', @processor.errors.first
89
- end
52
+ TOKEN = 'tok_0g3n2A0IthHLzf'
90
53
 
91
- @order.reload
92
-
93
- transaction = @order.payment_transactions.last
94
-
95
- assert_equal false, transaction.success
96
- assert_equal 'captured', transaction.operation
97
- assert @order.authorized?
98
- end
99
- end
100
-
101
- class AuthorizeNetRefundTest < ActiveRecord::TestCase
102
54
  setup do
103
- payment_method = NimbleshopAuthorizedotnet::Authorizedotnet.first
55
+ payment_method = NimbleshopStripe::Stripe.first
56
+ assert_equal 'test', payment_method.mode.to_s
57
+ assert_equal 'Stripe', payment_method.name
104
58
  @order = create(:order)
105
59
  @order.stubs(:total_amount).returns(100.48)
106
- @processor = NimbleshopAuthorizedotnet::Processor.new(order: @order, payment_method: payment_method)
107
- creditcard = build(:creditcard)
108
-
109
- playcasette('authorize.net/purchase-success') do
110
- assert_equal true, @processor.purchase(creditcard: creditcard)
60
+ @processor = NimbleshopStripe::Processor.new(order: @order, payment_method: payment_method)
61
+ playcasette('stripe/purchase-success-for-refund') do
62
+ assert_equal true, @processor.purchase(token: TOKEN)
111
63
  end
112
-
113
- assert @order.reload.purchased?
114
-
115
64
  @transaction = @order.payment_transactions.last
116
65
  end
117
66
 
118
67
  test 'when refund succeeds' do
119
-
120
- playcasette('authorize.net/refund-success') do
121
- assert_equal true, @processor.refund(transaction_gid: @transaction.transaction_gid,
68
+ assert_equal 'ch_0g3pxaKlHho2cr', @transaction.transaction_gid
69
+ playcasette('stripe/refund-success') do
70
+ assert_equal true, @processor.refund(transaction_gid: 'ch_0g3pxaKlHho2cr',
122
71
  card_number: @transaction.metadata[:card_number])
123
72
  end
124
-
125
73
  @order.reload
126
- transaction = @order.payment_transactions.last
127
-
128
- assert_equal 'refunded', transaction.operation
129
- assert_equal true, transaction.success
130
- assert_equal NimbleshopAuthorizedotnet::Authorizedotnet.first, @order.payment_method
131
- assert @order.refunded?
132
- end
133
-
134
- test 'when refund fails' do
135
-
136
- playcasette('authorize.net/refund-failure') do
137
- assert_equal false, @processor.refund(transaction_gid: @transaction.transaction_gid, card_number: '1234')
138
- end
139
-
140
- @order.reload
141
-
142
- transaction = @order.payment_transactions.last
143
-
144
- assert_equal 'refunded', transaction.operation
145
- assert_equal false, transaction.success
146
- end
147
- end
148
-
149
- class AuthorizeNetVoidTest < ActiveRecord::TestCase
150
- setup do
151
- payment_method = NimbleshopAuthorizedotnet::Authorizedotnet.first
152
- @order = create(:order)
153
- @order.stubs(:total_amount).returns(100.48)
154
- @processor = NimbleshopAuthorizedotnet::Processor.new(order: @order, payment_method: payment_method)
155
- creditcard = build(:creditcard)
156
-
157
- playcasette('authorize.net/authorize-success') do
158
- assert_equal true, @processor.authorize(creditcard: creditcard)
159
- end
160
-
161
- @tsx_id = @order.payment_transactions.last.transaction_gid
162
- end
163
-
164
- test 'when capture succeeds' do
165
- playcasette('authorize.net/void-success') do
166
- assert_equal true, @processor.void(transaction_gid: @tsx_id)
167
- end
168
-
169
- @order.reload
170
- transaction = @order.payment_transactions.last
171
-
172
- assert_equal 'voided', transaction.operation
173
- assert_equal true, transaction.success
174
- assert_equal NimbleshopAuthorizedotnet::Authorizedotnet.first, @order.payment_method
175
- assert @order.voided?
176
- end
177
-
178
- test 'when capture fails' do
179
- playcasette('authorize.net/void-failure') do
180
- assert_equal false, @processor.void(transaction_gid: @tsx_id)
181
- end
182
-
183
- @order.reload
184
-
185
- transaction = @order.payment_transactions.last
186
-
187
- assert_equal 'voided', transaction.operation
188
- assert_equal false, transaction.success
189
- end
190
- end
191
-
192
- class AuthorizeNetPurchaseTest < ActiveRecord::TestCase
193
- setup do
194
- payment_method = NimbleshopAuthorizedotnet::Authorizedotnet.first
195
- @order = create(:order)
196
- @order.stubs(:total_amount).returns(100.48)
197
- @processor = NimbleshopAuthorizedotnet::Processor.new(order: @order, payment_method: payment_method)
198
- end
199
-
200
- test 'when purchase succeeds' do
201
- creditcard = build(:creditcard)
202
-
203
- playcasette('authorize.net/purchase-success') do
204
- assert_equal true, @processor.purchase(creditcard: creditcard)
205
- end
206
-
207
- @order.reload
208
-
209
- transaction = @order.payment_transactions.last
210
- assert_equal 'purchased', transaction.operation
211
- assert_equal true, transaction.success
212
- assert @order.purchased?
213
- end
214
-
215
- test 'purchase fails when credit card number is not entered ' do
216
- creditcard = build(:creditcard, number: nil)
217
-
218
- playcasette('authorize.net/purchase-failure') do
219
- assert_equal false, @processor.purchase(creditcard: creditcard)
220
- assert_equal 'Please enter credit card number', @processor.errors.first
221
- end
222
-
223
- assert @order.abandoned?
224
- end
225
-
226
- test 'purchase fails when invalid credit card number is entered' do
227
- creditcard = build(:creditcard, number: 2)
228
-
229
- playcasette('authorize.net/purchase-failure') do
230
- assert_equal false, @processor.purchase(creditcard: creditcard)
231
- assert_equal 'Credit card was declined. Please try again!', @processor.errors.first
232
- end
233
-
234
- transaction = @order.payment_transactions.last
235
-
236
- assert_equal false, transaction.success
237
- assert_equal 'purchased', transaction.operation
238
- assert @order.abandoned?
74
+ assert @order.refunded?
239
75
  end
240
76
  end
241
77
  end