Class: Payola::ChargeCard
- Inherits:
-
Object
- Object
- Payola::ChargeCard
- Defined in:
- app/services/payola/charge_card.rb
Class Method Summary collapse
- .call(sale) ⇒ Object
- .create_charge(sale, customer, secret_key) ⇒ Object
- .create_customer(sale, secret_key) ⇒ Object
- .update_sale(sale, customer, charge, secret_key) ⇒ Object
Class Method Details
.call(sale) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/services/payola/charge_card.rb', line 3 def self.call(sale) sale.save! secret_key = Payola.secret_key_for_sale(sale) begin sale.verify_charge! customer = create_customer(sale, secret_key) charge = create_charge(sale, customer, secret_key) update_sale(sale, customer, charge, secret_key) sale.finish! rescue Stripe::StripeError, RuntimeError => e sale.update_attributes(error: e.) sale.fail! end sale end |
.create_charge(sale, customer, secret_key) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'app/services/payola/charge_card.rb', line 35 def self.create_charge(sale, customer, secret_key) charge_attributes = { amount: sale.amount, currency: sale.currency, customer: customer.id, description: sale.guid, }.merge(Payola.additional_charge_attributes.call(sale, customer)) Stripe::Charge.create(charge_attributes, secret_key) end |
.create_customer(sale, secret_key) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'app/services/payola/charge_card.rb', line 24 def self.create_customer(sale, secret_key) if sale.stripe_customer_id.present? Stripe::Customer.retrieve(sale.stripe_customer_id, secret_key) else Stripe::Customer.create({ source: sale.stripe_token, email: sale.email }, secret_key) end end |
.update_sale(sale, customer, charge, secret_key) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/services/payola/charge_card.rb', line 46 def self.update_sale(sale, customer, charge, secret_key) if charge.respond_to?(:fee) fee = charge.fee else balance = Stripe::BalanceTransaction.retrieve(charge.balance_transaction, secret_key) fee = balance.fee end sale.update_attributes( stripe_id: charge.id, stripe_customer_id: customer.id, card_last4: charge.source.last4, card_expiration: Date.new(charge.source.exp_year, charge.source.exp_month, 1), card_type: charge.source.brand, fee_amount: fee ) end |