add discourse stripe service

This commit is contained in:
Rimian Perkins 2017-04-06 14:16:05 +10:00
parent 66094fdfdd
commit 316dc61af5
2 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,23 @@
module DiscourseDonations
class Stripe
def initialize(secret_key, opts)
::Stripe.api_key = secret_key
@description = opts[:description]
@currency = opts[:currency]
end
def charge(email, opts)
customer = ::Stripe::Customer.create(
email: email,
source: opts[:source]
)
::Stripe::Charge.create(
customer: customer.id,
amount: opts[:amount],
description: @description,
currency: @currency
)
end
end
end

View File

@ -0,0 +1,32 @@
require 'rails_helper'
require_relative '../../support/dd_helper'
module DiscourseDonations
RSpec.describe DiscourseDonations::Stripe do
before { SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo') }
let(:stripe_options) { { description: 'hi there', currency: 'AUD' } }
let(:email) { 'ray-zintoast@example.com' }
let(:customer) { stub(id: 1) }
let!(:subject) { described_class.new('secret-key-yo', stripe_options) }
it 'sets the api key' do
expect(::Stripe.api_key).to eq 'secret-key-yo'
end
it 'creates a customer and charges them an amount' do
options = { email: email, source: '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],
description: stripe_options[:description],
currency: stripe_options[:currency]
)
subject.charge(email, options)
end
end
end