lib/moneytree/payment_provider/stripe.rb in moneytree-rails-0.1.3 vs lib/moneytree/payment_provider/stripe.rb in moneytree-rails-0.1.4
- old
+ new
@@ -12,17 +12,63 @@
super
end
def get_access_token(params)
# FIXME: add error handling
- ::Stripe::OAuth.token({
- grant_type: 'authorization_code',
- code: params[:code]
- }).to_hash
+ ::Stripe::OAuth.token(
+ {
+ grant_type: 'authorization_code',
+ code: params[:code]
+ }
+ ).to_hash
end
def scope
PERMISSION.to_s
+ end
+
+ def charge(amount, details, app_fee_amount: 0, description: "Charge for #{account.name}", metadata:)
+ # `source` is obtained with Stripe.js; see https://stripe.com/docs/payments/accept-a-payment-charges#web-create-token
+ response = ::Stripe::Charge.create(
+ {
+ amount: (amount * 100).to_i,
+ currency: account.currency_code,
+ source: details[:card_token],
+ description: description,
+ metadata: metadata,
+ application_fee_amount: (app_fee_amount * 100).to_i
+ },
+ stripe_account: payment_gateway.psp_credentials[:stripe_user_id]
+ )
+ # succeeded, pending, or failed
+ TransactionResponse.new(
+ { succeeded: :success, pending: :pending, failed: :failed }[response[:status].to_sym],
+ response[:failure_message],
+ { charge_id: response[:id] }
+ )
+ rescue ::Stripe::StripeError => e
+ TransactionResponse.new(:failed, e.message)
+ end
+
+ def refund(amount, details, metadata:)
+ response = ::Stripe::Refund.create(
+ {
+ charge: details[:charge_id],
+ amount: (-amount * 100).to_i,
+ metadata: metadata,
+ refund_application_fee: Moneytree.refund_application_fee
+ },
+ stripe_account: payment_gateway.psp_credentials[:stripe_user_id]
+ )
+
+ # succeeded, pending, or failed
+ TransactionResponse.new(
+ { succeeded: :success, pending: :pending, failed: :failed }[response[:status].to_sym],
+ response[:failure_message],
+ { refund_id: response[:id] }
+ )
+ rescue ::Stripe::StripeError => e
+ TransactionResponse.new(:failed, e.message)
end
private
def credentitals