Skip to content

Commit

Permalink
Merge pull request marcelog#2 from staylorwr/lk/create_profile_from_t…
Browse files Browse the repository at this point in the history
…ransaction

Create customer profile from transaction
  • Loading branch information
staylorwr authored Aug 21, 2018
2 parents a0da7d7 + aae7da4 commit 9dfa4dd
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 13 deletions.
42 changes: 42 additions & 0 deletions lib/elixir_authorizenet/base_customer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule AuthorizeNet.BaseCustomer do
@moduledoc """
Handles customer profiles (http://developer.authorize.net/api/reference/index.html#manage-customer-profiles).
Copyright 2015 Marcelo Gornstein <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
use AuthorizeNet.Helper.XML
defstruct id: nil,
email: nil,
description: nil

@type t :: %AuthorizeNet.BaseCustomer{}

@spec new( String.t, String.t, String.t
) :: AuthorizeNet.BaseCustomer.t | no_return
def new( id, description, email) do
%AuthorizeNet.BaseCustomer{
id: id,
description: description,
email: email
}
end

def to_xml(customer) do
[
merchantCustomerId: customer.id,
description: customer.description,
email: customer.email,
]
end
end
45 changes: 45 additions & 0 deletions lib/elixir_authorizenet/customer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule AuthorizeNet.Customer do
alias AuthorizeNet, as: Main
alias AuthorizeNet.PaymentProfile, as: PaymentProfile
alias AuthorizeNet.Address, as: Address
alias AuthorizeNet.BaseCustomer, as: BaseCustomer
defstruct description: nil,
email: nil,
id: nil,
Expand Down Expand Up @@ -140,6 +141,50 @@ defmodule AuthorizeNet.Customer do
%AuthorizeNet.Customer{profile | profile_id: profile_id}
end

@spec create_from_transaction(
String.t, String.t, String.t, String.t, String.t
) :: AuthorizeNet.Customer.t | no_return
def create_from_transaction(id, nil, transaction_id, description, email) do
profile = BaseCustomer.new(id, description, email)
profile_xml = BaseCustomer.to_xml(profile)
doc = Main.req :createCustomerProfileFromTransactionRequest, [
transId: transaction_id,
customer: profile_xml
]
customer_profile = new id, nil, description, email
customer_profile_id = xml_one_value_int doc, "//customerProfileId"
payment_profile_id = xml_one_value_int doc, "//numericString"

%AuthorizeNet.Customer{customer_profile | profile_id: customer_profile_id, payment_profiles: [payment_profile_id]}
end

def create_from_transaction(_id, profile_id, transaction_id, _description, _email) do
doc = Main.req :createCustomerProfileFromTransactionRequest, [
transId: transaction_id,
customerProfileId: profile_id,
]
customer_profile = get profile_id
payment_profile_id = xml_one_value_int doc, "//numericString"

%AuthorizeNet.Customer{customer_profile | payment_profiles: [payment_profile_id]}
end

def validate_payment_profile(profile_id, payment_profile_id, card_code, mode) do
doc = Main.req :validateCustomerPaymentProfileRequest, [
customerProfileId: profile_id,
customerPaymentProfileId: payment_profile_id,
cardCode: card_code,
validationMode: mode
]
case xml_one_value(doc, "//resultCode") do
"Ok" ->
{:ok, payment_profile_id}

"Error" ->
{:error, payment_profile_id}
end
end

@doc """
Deletes a customer profile by customer profile ID. See:
http://developer.authorize.net/api/reference/index.html#manage-customer-profiles-delete-customer-profile
Expand Down
30 changes: 17 additions & 13 deletions test/resources/AnetApiSchema.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -2411,21 +2411,25 @@

<!--
===================================================================
bla bla
createCustomerProfileFromTransactionRequest
This method is used to create a new customer profile along with any
customer payment profiles and customer shipping addresses for the customer profile.
The merchant must be signed up for the CIM service to use it.
===================================================================
-->
<xs:element name="createCustomerProfileFromTransactionRequest">
<xs:complexType>
<xs:complexContent>
<xs:extension base="anet:ANetApiRequest">
<xs:sequence>
<xs:element name="transId" type="anet:numericString" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>

<xs:element name="createCustomerProfileFromTransactionRequest">
<xs:complexType>
<xs:complexContent>
<xs:extension base="anet:ANetApiRequest">
<xs:sequence>
<xs:element name="transId" type="anet:numericString" minOccurs="1" maxOccurs="1"/>
<xs:element name="customer" type="anet:customerProfileBaseType" minOccurs="0" maxOccurs="1"/>
<xs:element name="customerProfileId" type="anet:numericString" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<!--
===================================================================
getCustomerProfileRequest
Expand Down

0 comments on commit 9dfa4dd

Please sign in to comment.