Slide 30
Slide 30 text
30
# app/services/purchaser.rb
class Purchaser < ActiveService ::Base
needs :payment_transaction
needs :order_mailer
def purchase!(customer:, card:, order:)
transaction = payment_transaction.new(card,order.amount)
if transaction.success?
order_mailer.receipt_email(customer,order).deliver_later
else
order_mailer.card_failed(customer,order,transaction).deliver_later
end
end
end
# config/initializers/braintree.rb
service :payment_transaction do
Braintree ::Transaction.new(api_key: ENV["BRAINTREE_API_KEY"])
end