module Workarea decorate Payment::Purchase::CreditCard, with: :braintree do decorated { delegate :address, to: :tender } def complete! transaction.response = handle_active_merchant_errors do if tender.token.present? gateway.purchase( transaction.amount.cents, tender.token, { payment_method_token: true } ) else gateway.purchase( transaction.amount.cents, tender.to_active_merchant, { store: true, billing_address: billing_address } ) end end if transaction.response.success? && tender.token.blank? tender.token = transaction.response.params["braintree_transaction"]["credit_card_details"]["token"] tender.save! end end def cancel! return unless transaction.success? transaction.cancellation = handle_active_merchant_errors do gateway.void(transaction.response.authorization) end end private def billing_address { name: nil, company: nil, address1: address.street, address2: address.street_2, city: address.city, state: address.region, country: address.country.try(:alpha2), zip: address.postal_code, phone: nil } end end end