the message

This commit is contained in:
Rimian Perkins 2017-04-19 10:43:40 +10:00
parent 81a42af4d9
commit 0d078c6131
3 changed files with 47 additions and 5 deletions

View File

@ -7,9 +7,10 @@ module DiscourseDonations
skip_before_filter :verify_authenticity_token, only: [:create]
def create
if email.nil? || email.empty?
if !email.nil? || email.empty?
response = {}
else
byebug
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
response = payment.charge(email, params)
end

View File

@ -12,12 +12,17 @@ module DiscourseDonations
email: email,
source: opts[:stripeToken]
)
::Stripe::Charge.create(
@charge = ::Stripe::Charge.create(
customer: customer.id,
amount: opts[:amount],
description: @description,
currency: @currency
)
@charge
end
def successful?
@charge[:paid]
end
end
end

View File

@ -6,6 +6,7 @@ module DiscourseDonations
before { SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo') }
let(:stripe_options) { { description: 'hi there', currency: 'AUD' } }
let(:params) { { email: email, stripeToken: 'stripe-token', amount: '1234', other: 'redundant param' } }
let(:email) { 'ray-zintoast@example.com' }
let(:customer) { stub(id: 1) }
let!(:subject) { described_class.new('secret-key-yo', stripe_options) }
@ -15,18 +16,53 @@ module DiscourseDonations
end
it 'creates a customer and charges them an amount' do
options = { email: email, stripeToken: 'stripe-token', amount: '1234', other: 'redundant param' }
::Stripe::Customer.expects(:create).with(
email: email,
source: 'stripe-token'
).returns(customer)
::Stripe::Charge.expects(:create).with(
customer: customer.id,
amount: options[:amount],
amount: params[:amount],
description: stripe_options[:description],
currency: stripe_options[:currency]
)
subject.charge(email, options)
subject.charge(email, params)
end
it 'has a message' do
::Stripe::Charge.expects(:create).with(
customer: customer.id,
amount: params[:amount],
description: stripe_options[:description],
currency: stripe_options[:currency]
).returns(
{
paid: true,
outcome: { seller_message: 'yay!' }
}
)
subject.charge(email, params)
expect(subject.message).to eq 'yay!'
end
describe '.successful?' do
let(:charge_options) { { customer: customer.id, amount: params[:amount], description: stripe_options[:description], currency: stripe_options[:currency] } }
before do
::Stripe::Customer.expects(:create).with(email: email, source: 'stripe-token').returns(customer)
end
it 'is successful' do
::Stripe::Charge.expects(:create).with(charge_options).returns({paid: true})
subject.charge(email, params)
expect(subject).to be_successful
end
it 'is not successful' do
::Stripe::Charge.expects(:create).with(charge_options).returns({paid: false})
subject.charge(email, params)
expect(subject).not_to be_successful
end
end
end
end