From 316dc61af55ae6460b16ae2fd4f26562af3c10f6 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Thu, 6 Apr 2017 14:16:05 +1000 Subject: [PATCH] add discourse stripe service --- app/services/discourse_donations/stripe.rb | 23 +++++++++++++ .../discourse_donations/stripe_spec.rb | 32 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 app/services/discourse_donations/stripe.rb create mode 100644 spec/services/discourse_donations/stripe_spec.rb diff --git a/app/services/discourse_donations/stripe.rb b/app/services/discourse_donations/stripe.rb new file mode 100644 index 0000000..4c02a6e --- /dev/null +++ b/app/services/discourse_donations/stripe.rb @@ -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 diff --git a/spec/services/discourse_donations/stripe_spec.rb b/spec/services/discourse_donations/stripe_spec.rb new file mode 100644 index 0000000..b13924e --- /dev/null +++ b/spec/services/discourse_donations/stripe_spec.rb @@ -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