Class: Moneytree::PaymentProvider::Stripe

Inherits:
Base
  • Object
show all
Defined in:
lib/moneytree/payment_provider/stripe.rb

Constant Summary collapse

PERMISSION =

The permissions we request from Square OAuth, we store them in the database

:read_write

Instance Attribute Summary

Attributes inherited from Base

#account, #payment_gateway

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Stripe

Returns a new instance of Stripe.

Raises:



7
8
9
10
11
12
13
# File 'lib/moneytree/payment_provider/stripe.rb', line 7

def initialize(args)
  raise Error, 'Please set your Stripe credentials' if credentitals.nil?
  raise Error, 'Please include the stripe gem to your Gemfile' unless Object.const_defined?('::Stripe')

  ::Stripe.api_key = credentitals[:api_key]
  super
end

Instance Method Details

#card_for(transaction) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/moneytree/payment_provider/stripe.rb', line 77

def card_for(transaction)
  Card.new(
    last4: transaction.details[:card][:last4],
    exp_month: transaction.details[:card][:exp_month],
    exp_year: transaction.details[:card][:exp_year],
    fingerprint: transaction.details[:card][:fingerprint],
    brand: transaction.details[:card][:brand],
    address_zip: transaction.details[:card][:address_zip],
    country: transaction.details[:card][:country]
  )
end

#charge(amount, details, metadata:, app_fee_amount: 0, description: "Charge for #{account.name}") ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/moneytree/payment_provider/stripe.rb', line 29

def charge(amount, details, metadata:, app_fee_amount: 0, description: "Charge for #{.name}")
  # `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: .currency_code,
      source: details[:card_token],
      description: description,
      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],
      card: response[:payment_method_details][:card],
      has_application_fee: !app_fee_amount.zero?
    }
  )
rescue ::Stripe::StripeError => e
  TransactionResponse.new(:failed, e.message)
end

#get_access_token(params) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/moneytree/payment_provider/stripe.rb', line 15

def get_access_token(params)
  # FIXME: add error handling
  ::Stripe::OAuth.token(
    {
      grant_type: 'authorization_code',
      code: params[:code]
    }
  ).to_hash
end

#refund(amount, details, metadata:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/moneytree/payment_provider/stripe.rb', line 56

def refund(amount, details, metadata:)
  response = ::Stripe::Refund.create(
    {
      charge: details[:charge_id],
      amount: (-amount * 100).to_i,
      metadata: ,
      refund_application_fee: details[:has_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

#scopeObject



25
26
27
# File 'lib/moneytree/payment_provider/stripe.rb', line 25

def scope
  PERMISSION.to_s
end