57 lines
1.2 KiB
Ruby
Raw Normal View History

2017-04-06 14:16:05 +10:00
module DiscourseDonations
class Stripe
2017-05-02 09:47:23 +10:00
attr_reader :charge, :currency, :description
2017-04-06 14:16:05 +10:00
def initialize(secret_key, opts)
::Stripe.api_key = secret_key
@description = opts[:description]
@currency = opts[:currency]
end
def checkoutCharge(email, token, amount)
customer = ::Stripe::Customer.create(
:email => email,
:source => token
)
charge = ::Stripe::Charge.create(
:customer => customer.id,
2017-10-17 12:35:14 +01:00
:amount => amount,
:description => @description,
:currency => @currency
2017-10-17 12:35:14 +01:00
)
charge
2017-10-17 12:35:14 +01:00
end
2017-04-06 14:16:05 +10:00
def charge(email, opts)
customer = ::Stripe::Customer.create(
email: email,
2017-04-07 03:22:53 +10:00
source: opts[:stripeToken]
2017-04-06 14:16:05 +10:00
)
2017-04-19 10:43:40 +10:00
@charge = ::Stripe::Charge.create(
2017-04-06 14:16:05 +10:00
customer: customer.id,
amount: opts[:amount],
2017-05-02 09:47:23 +10:00
description: description,
currency: currency
2017-04-06 14:16:05 +10:00
)
2017-04-19 10:43:40 +10:00
@charge
end
def subscribe(email, opts)
customer = ::Stripe::Customer.create(
email: email,
source: opts[:stripeToken]
)
@subscription = ::Stripe::Subscription.create(
customer: customer.id,
plan: opts[:plan]
)
@subscription
end
2017-04-19 10:43:40 +10:00
def successful?
@charge[:paid]
2017-04-06 14:16:05 +10:00
end
end
end