Class: AuthorizeNet::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/authorize_net/api.rb

Overview

This class uses the AuthroizeRequest object to interact with the Authorize.Net API

Add any new Authroize.Net API endpoints here

Instance Method Summary collapse

Constructor Details

#initialize(api_login_id, api_transaction_key, **options) ⇒ Api

Returns a new instance of Api.



12
13
14
15
16
17
18
19
# File 'lib/authorize_net/api.rb', line 12

def initialize(, api_transaction_key, **options)
  @api_login_id = 
  @api_transaction_key = api_transaction_key
  @is_sandbox = options[:sandbox]
  @signature_key = options[:signature_key]
  @logger = nil
  @log_full_request = false
end

Instance Method Details

#chargeAndCreateProfile(amount, customer_profile) ⇒ customer_profile_id, payment_profile_id

Creates the CustomerProfile and charges the first listed PaymentProfile on AuthorizeNet

Parameters:

  • Number

    amount

  • AuthorizeNet::CustomerProfile

    customer_profile

Returns:

  • (customer_profile_id, payment_profile_id)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/authorize_net/api.rb', line 63

def chargeAndCreateProfile(amount, customer_profile)
  if customer_profile.payment_profiles.empty?
    raise "[AuthorizeNet] CustomerProfile in Api.chargeAndCreateProfile requires a PaymentProfile"
  end

  payment_profile = customer_profile.payment_profiles.first
  xml_obj = getXmlAuth
  xml_obj["transactionRequest"] = {
    "transactionType" => "authCaptureTransaction",
    "amount" => amount,
    "payment" => {
      "creditCard" => payment_profile.credit_card.to_h,
    },
    "profile" => {
      "createProfile" => true,
    },
    "customer" => {
      "id" => customer_profile.merchant_id,
      "email" => customer_profile.email,
      "description" => customer_profile.description,
    },
    "billTo" => payment_profile.billing_address.to_h,
  }

  response = sendRequest("createTransactionRequest", xml_obj)
  validate_hash(response, amount)
  if !response.nil?
    return {
      :transaction => AuthorizeNet::Transaction.parse(response),
      :customer_profile_id => AuthorizeNet::Util.getXmlValue(
        response, "customerProfileId"),
      :payment_profile_id => AuthorizeNet::Util.getXmlValue(
        response, "customerPaymentProfileIdList numericString"),
    }
  end
end

#chargeCard(amount, credit_card, billing_address = nil) ⇒ transaction_id

Charges the given credit card

Parameters:

  • Number

    amount

  • AuthorizeNet::CreditCard

    credit_card

  • AuthorizeNet::Address

    billing_address

Returns:

  • (transaction_id)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/authorize_net/api.rb', line 36

def chargeCard(amount, credit_card, billing_address=nil)
  xml_obj = getXmlAuth
  xml_obj["transactionRequest"] = {
    "transactionType" => "authCaptureTransaction",
    "amount" => amount,
    "payment" => {
      "creditCard" => credit_card.to_h,
    },
  }
  if !billing_address.nil?
    xml_obj["transactionRequest"]["billTo"] = billing_address.to_h
  end

  response = sendRequest("createTransactionRequest", xml_obj)
  validate_hash(response, amount)
  if !response.nil?
    return AuthorizeNet::Transaction.parse(response)
  end
end

#chargeProfile(amount, profile_id, payment_profile_id) ⇒ Object

Charges the given profile and payment profile on Authorize.net

Parameters:

  • Number

    amount

  • String/Number

    customer_profile_id

  • String/Number

    payment_profile_id

Returns:

  • transaction_id



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/authorize_net/api.rb', line 107

def chargeProfile(amount, profile_id, payment_profile_id)
  xml_obj = getXmlAuth
  xml_obj["transactionRequest"] = {
    "transactionType" => "authCaptureTransaction",
    "amount" => amount,
    "profile" => {
      "customerProfileId" => profile_id,
      "paymentProfile" => {
        "paymentProfileId" => payment_profile_id,
      },
    },
  }

  response = sendRequest("createTransactionRequest", xml_obj)
  validate_hash(response, amount)
  if !response.nil?
    return AuthorizeNet::Transaction.parse(response)
  end
end

#createCustomerProfile(customer_profile, validation_mode = nil) ⇒ Object

Creates the given customer profile on Authorize.net

Parameters:

  • AuthorizeNet::CustomerProfile

    customer_profile

  • Number

    amount

  • AuthorizeNet::ValidationMode

    validation_mode (optional)

Returns:

  • transaction_id



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/authorize_net/api.rb', line 134

def createCustomerProfile(customer_profile, validation_mode=nil)
  xml_obj = getXmlAuth
  xml_obj["profile"] = customer_profile.to_h

  addValidationMode!(xml_obj, validation_mode)
  response = sendRequest("createCustomerProfileRequest", xml_obj)

  if !response.nil?
    return {
      :customer_profile_id => AuthorizeNet::Util.getXmlValue(
        response, "customerProfileId"),
      :payment_profile_id => AuthorizeNet::Util.getXmlValue(
        response, "customerPaymentProfileIdList numericString"),
    }
  end
end

#createPaymentProfile(customer_profile_id, payment_profile, validation_mode = nil) ⇒ customer_profile_id, payment_profile_id

Create Customer Payment Profile

Parameters:

  • String/Number

    customer_profile_id

  • AuthorizeNet::PaymentProfile

    payment_profile

  • AuthorizeNet::ValidationMode

    validation_mode (optional)

Returns:

  • (customer_profile_id, payment_profile_id)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/authorize_net/api.rb', line 158

def createPaymentProfile(customer_profile_id, payment_profile, validation_mode=nil)
  xml_obj = getXmlAuth
  xml_obj["customerProfileId"] = customer_profile_id
  xml_obj["paymentProfile"] = payment_profile.to_h

  addValidationMode!(xml_obj, validation_mode)
  response = sendRequest("createCustomerPaymentProfileRequest", xml_obj)

  if !response.nil?
    return {
      :customer_profile_id => AuthorizeNet::Util.getXmlValue(
        response, "customerProfileId"),
      :payment_profile_id => AuthorizeNet::Util.getXmlValue(
        response, "customerPaymentProfileId"),
    }
  end
end

#deletePaymentProfile(customer_profile_id, payment_profile_id) ⇒ Object

Delete Customer Payment Profile

Parameters:

  • String/Number

    customer_profile_id

  • String/Number

    payment_profile_id

Returns:

  • boolean is delete successful?



182
183
184
185
186
187
188
189
# File 'lib/authorize_net/api.rb', line 182

def deletePaymentProfile(customer_profile_id, payment_profile_id)
  xml_obj = getXmlAuth
  xml_obj["customerProfileId"] = customer_profile_id
  xml_obj["customerPaymentProfileId"] = payment_profile_id

  response = sendRequest("deleteCustomerPaymentProfileRequest", xml_obj)
  return !response.nil?
end

#getCustomerProfile(customer_profile_id) ⇒ Object

Get customer profile information

Parameters:

  • String/Number

    customer_profile_id

  • String/Number

    customer_profile_id

Returns:

  • AuthorizeNet::CustomerProfile



214
215
216
217
218
219
220
221
222
# File 'lib/authorize_net/api.rb', line 214

def getCustomerProfile(customer_profile_id)
  xml_obj = getXmlAuth
  xml_obj["customerProfileId"] = customer_profile_id

  response = sendRequest("getCustomerProfileRequest", xml_obj)
  if response
    return AuthorizeNet::CustomerProfile.parse(response)
  end
end

#getTransactionInfo(transaction_id) ⇒ Object

Gets transaction information

Parameters:

  • String/Number

    customer_profile_id

  • String/Number

    transaction_id

Returns:

  • AuthorizeNet::Transaction



230
231
232
233
234
235
236
237
238
# File 'lib/authorize_net/api.rb', line 230

def getTransactionInfo(transaction_id)
  xml_obj = getXmlAuth
  xml_obj["transId"] = transaction_id

  response = sendRequest("getTransactionDetailsRequest", xml_obj)
  if response
    return AuthorizeNet::Transaction.parse(response)
  end
end

#setLogFullRequest(log_full_request) ⇒ Object



25
26
27
# File 'lib/authorize_net/api.rb', line 25

def setLogFullRequest(log_full_request)
  @log_full_request = log_full_request
end

#setLogger(logger) ⇒ Object



21
22
23
# File 'lib/authorize_net/api.rb', line 21

def setLogger(logger)
  @logger = logger
end

#validatePaymentProfile(customer_profile_id, payment_profile_id, validation_mode) ⇒ Object

Validate Customer Payment Profile

Parameters:

  • String/Number

    customer_profile_id

  • String/Number

    payment_profile_id

  • AuthorizeNet::ValidationMode:: (String)

    validation_mode

Returns:

  • boolean is update successful?



198
199
200
201
202
203
204
205
206
# File 'lib/authorize_net/api.rb', line 198

def validatePaymentProfile(customer_profile_id, payment_profile_id, validation_mode)
  xml_obj = getXmlAuth
  xml_obj["customerProfileId"] = customer_profile_id
  xml_obj["customerPaymentProfileId"] = payment_profile_id
  xml_obj["validationMode"] = validation_mode

  response = sendRequest("validateCustomerPaymentProfileRequest", xml_obj)
  return !response.nil?
end