Skip to content

Instantly share code, notes, and snippets.

@brandoncc
Created January 19, 2019 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandoncc/177b2006eb29e0009c750088a5900160 to your computer and use it in GitHub Desktop.
Save brandoncc/177b2006eb29e0009c750088a5900160 to your computer and use it in GitHub Desktop.
require 'authorizenet'
class PaymentAuthorization
attr_reader :response
def initialize(response)
@response = response
end
def successful?
response.messages.resultCode == AuthorizeNet::API::MessageTypeEnum::Ok &&
!response.transactionResponse.nil? &&
!response.transactionResponse.messages.nil?
end
def messages
(
(response.transactionResponse&.errors&.errors&.map(&:errorText) || []) +
(response.messages.messages.map(&:text) || [])
).join(" ")
end
def account_number
response.transactionResponse.accountNumber
end
def account_type
response.transactionResponse.accountType
end
def transaction_id
response.transactionResponse.transId
end
class << self
def authorize(order, descriptor, nonce)
transaction = AuthorizeNet::API::Transaction.new(
credentials[:login_id],
credentials[:transaction_key],
gateway: Rails.env.production? ? :production : :sandbox
)
request = AuthorizeNet::API::CreateTransactionRequest.new
request.transactionRequest = AuthorizeNet::API::TransactionRequestType.new()
request.transactionRequest.amount = order.grand_total
request.transactionRequest.payment = AuthorizeNet::API::PaymentType.new
request.transactionRequest.payment.opaqueData = AuthorizeNet::API::OpaqueDataType.new(descriptor, nonce)
request.transactionRequest.customer = AuthorizeNet::API::CustomerDataType.new(customer_type(order), nil, order.email)
request.transactionRequest.transactionType = AuthorizeNet::API::TransactionTypeEnum::AuthOnlyTransaction
request.transactionRequest.order = AuthorizeNet::API::OrderType.new(order.visible_id, "")
request.transactionRequest.billTo = AuthorizeNet::API::CustomerAddressType.new.tap do |ca_type|
ca_type.firstName = order.first_name
ca_type.lastName = order.last_name
ca_type.address = "#{order.billing_address} #{order.billing_address2}".strip
ca_type.city = order.billing_city
ca_type.state = order.billing_state
ca_type.zip = order.billing_zip_code
ca_type.phoneNumber = order.billing_phone.to_s
end
request.transactionRequest.shipTo = AuthorizeNet::API::NameAndAddressType.new.tap do |na_type|
na_type.firstName = order.first_name
na_type.lastName = order.last_name
na_type.company = order.shipping_company
na_type.address = "#{order.shipping_address} #{order.shipping_address2}".strip
na_type.city = order.shipping_city
na_type.state = order.shipping_state
na_type.zip = order.shipping_zip_code
end
new(transaction.create_transaction(request))
end
private
def credentials
@credentials ||=
if Rails.env.production?
Rails.application.credentials.authorize_net[:production]
else
Rails.application.credentials.authorize_net[:sandbox]
end
end
def customer_type(order)
if order.ship_to_business?
AuthorizeNet::API::CustomerTypeEnum::Business
else
AuthorizeNet::API::CustomerTypeEnum::Individual
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment