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, is_test_api) ⇒ Api

Returns a new instance of Api.



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

def initialize(, api_transaction_key, is_test_api)
   = 
  @api_transaction_key = api_transaction_key
  @is_test_api = is_test_api
  @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)


60
61
62
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
# File 'lib/authorize_net/api.rb', line 60

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)

  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)


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

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)
  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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/authorize_net/api.rb', line 104

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)
  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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/authorize_net/api.rb', line 130

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)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/authorize_net/api.rb', line 154

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?



178
179
180
181
182
183
184
185
# File 'lib/authorize_net/api.rb', line 178

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



210
211
212
213
214
215
216
217
218
# File 'lib/authorize_net/api.rb', line 210

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



226
227
228
229
230
231
232
233
234
# File 'lib/authorize_net/api.rb', line 226

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



23
24
25
# File 'lib/authorize_net/api.rb', line 23

def setLogFullRequest(log_full_request)
  @log_full_request = log_full_request
end

#setLogger(logger) ⇒ Object



19
20
21
# File 'lib/authorize_net/api.rb', line 19

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?



194
195
196
197
198
199
200
201
202
# File 'lib/authorize_net/api.rb', line 194

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