1+ 'use strict' ;
2+
3+ var ApiContracts = require ( 'authorizenet' ) . APIContracts ;
4+ var ApiControllers = require ( 'authorizenet' ) . APIControllers ;
5+ var SDKConstants = require ( 'authorizenet' ) . Constants ;
6+ var utils = require ( '../utils.js' ) ;
7+ var constants = require ( '../constants.js' ) ;
8+
9+ function createAnAcceptPaymentTransaction ( callback ) {
10+ var merchantAuthenticationType = new ApiContracts . MerchantAuthenticationType ( ) ;
11+ merchantAuthenticationType . setName ( constants . apiLoginKey ) ;
12+ merchantAuthenticationType . setTransactionKey ( constants . transactionKey ) ;
13+
14+ var opaqueData = new ApiContracts . OpaqueDataType ( ) ;
15+ opaqueData . setDataDescriptor ( 'COMMON.ACCEPT.INAPP.PAYMENT' ) ;
16+ opaqueData . setDataValue ( '119eyJjb2RlIjoiNTBfMl8wNjAwMDUyN0JEODE4RjQxOUEyRjhGQkIxMkY0MzdGQjAxQUIwRTY2NjhFNEFCN0VENzE4NTUwMjlGRUU0M0JFMENERUIwQzM2M0ExOUEwMDAzNzlGRDNFMjBCODJEMDFCQjkyNEJDIiwidG9rZW4iOiI5NDkwMjMyMTAyOTQwOTk5NDA0NjAzIiwidiI6IjEuMSJ9' ) ;
17+
18+
19+ var paymentType = new ApiContracts . PaymentType ( ) ;
20+ paymentType . setOpaqueData ( opaqueData ) ;
21+
22+ var orderDetails = new ApiContracts . OrderType ( ) ;
23+ orderDetails . setInvoiceNumber ( 'INV-12345' ) ;
24+ orderDetails . setDescription ( 'Product Description' ) ;
25+
26+ var tax = new ApiContracts . ExtendedAmountType ( ) ;
27+ tax . setAmount ( '4.26' ) ;
28+ tax . setName ( 'level2 tax name' ) ;
29+ tax . setDescription ( 'level2 tax' ) ;
30+
31+ var duty = new ApiContracts . ExtendedAmountType ( ) ;
32+ duty . setAmount ( '8.55' ) ;
33+ duty . setName ( 'duty name' ) ;
34+ duty . setDescription ( 'duty description' ) ;
35+
36+ var shipping = new ApiContracts . ExtendedAmountType ( ) ;
37+ shipping . setAmount ( '8.55' ) ;
38+ shipping . setName ( 'shipping name' ) ;
39+ shipping . setDescription ( 'shipping description' ) ;
40+
41+ var billTo = new ApiContracts . CustomerAddressType ( ) ;
42+ billTo . setFirstName ( 'Ellen' ) ;
43+ billTo . setLastName ( 'Johnson' ) ;
44+ billTo . setCompany ( 'Souveniropolis' ) ;
45+ billTo . setAddress ( '14 Main Street' ) ;
46+ billTo . setCity ( 'Pecan Springs' ) ;
47+ billTo . setState ( 'TX' ) ;
48+ billTo . setZip ( '44628' ) ;
49+ billTo . setCountry ( 'USA' ) ;
50+
51+ var shipTo = new ApiContracts . CustomerAddressType ( ) ;
52+ shipTo . setFirstName ( 'China' ) ;
53+ shipTo . setLastName ( 'Bayles' ) ;
54+ shipTo . setCompany ( 'Thyme for Tea' ) ;
55+ shipTo . setAddress ( '12 Main Street' ) ;
56+ shipTo . setCity ( 'Pecan Springs' ) ;
57+ shipTo . setState ( 'TX' ) ;
58+ shipTo . setZip ( '44628' ) ;
59+ shipTo . setCountry ( 'USA' ) ;
60+
61+ var lineItem_id1 = new ApiContracts . LineItemType ( ) ;
62+ lineItem_id1 . setItemId ( '1' ) ;
63+ lineItem_id1 . setName ( 'vase' ) ;
64+ lineItem_id1 . setDescription ( 'cannes logo' ) ;
65+ lineItem_id1 . setQuantity ( '18' ) ;
66+ lineItem_id1 . setUnitPrice ( 45.00 ) ;
67+
68+ var lineItem_id2 = new ApiContracts . LineItemType ( ) ;
69+ lineItem_id2 . setItemId ( '2' ) ;
70+ lineItem_id2 . setName ( 'vase2' ) ;
71+ lineItem_id2 . setDescription ( 'cannes logo2' ) ;
72+ lineItem_id2 . setQuantity ( '28' ) ;
73+ lineItem_id2 . setUnitPrice ( '25.00' ) ;
74+
75+ var lineItemList = [ ] ;
76+ lineItemList . push ( lineItem_id1 ) ;
77+ lineItemList . push ( lineItem_id2 ) ;
78+
79+ var lineItems = new ApiContracts . ArrayOfLineItem ( ) ;
80+ lineItems . setLineItem ( lineItemList ) ;
81+
82+ var userField_a = new ApiContracts . UserField ( ) ;
83+ userField_a . setName ( 'A' ) ;
84+ userField_a . setValue ( 'Aval' ) ;
85+
86+ var userField_b = new ApiContracts . UserField ( ) ;
87+ userField_b . setName ( 'B' ) ;
88+ userField_b . setValue ( 'Bval' ) ;
89+
90+ var userFieldList = [ ] ;
91+ userFieldList . push ( userField_a ) ;
92+ userFieldList . push ( userField_b ) ;
93+
94+ var userFields = new ApiContracts . TransactionRequestType . UserFields ( ) ;
95+ userFields . setUserField ( userFieldList ) ;
96+
97+ var transactionSetting1 = new ApiContracts . SettingType ( ) ;
98+ transactionSetting1 . setSettingName ( 'testRequest' ) ;
99+ transactionSetting1 . setSettingValue ( 's1val' ) ;
100+
101+ var transactionSetting2 = new ApiContracts . SettingType ( ) ;
102+ transactionSetting2 . setSettingName ( 'testRequest' ) ;
103+ transactionSetting2 . setSettingValue ( 's2val' ) ;
104+
105+ var transactionSettingList = [ ] ;
106+ transactionSettingList . push ( transactionSetting1 ) ;
107+ transactionSettingList . push ( transactionSetting2 ) ;
108+
109+ var transactionSettings = new ApiContracts . ArrayOfSetting ( ) ;
110+ transactionSettings . setSetting ( transactionSettingList ) ;
111+
112+ var transactionRequestType = new ApiContracts . TransactionRequestType ( ) ;
113+ transactionRequestType . setTransactionType ( ApiContracts . TransactionTypeEnum . AUTHCAPTURETRANSACTION ) ;
114+ transactionRequestType . setPayment ( paymentType ) ;
115+ transactionRequestType . setAmount ( utils . getRandomAmount ( ) ) ;
116+ transactionRequestType . setLineItems ( lineItems ) ;
117+ transactionRequestType . setUserFields ( userFields ) ;
118+ transactionRequestType . setOrder ( orderDetails ) ;
119+ transactionRequestType . setTax ( tax ) ;
120+ transactionRequestType . setDuty ( duty ) ;
121+ transactionRequestType . setShipping ( shipping ) ;
122+ transactionRequestType . setBillTo ( billTo ) ;
123+ transactionRequestType . setShipTo ( shipTo ) ;
124+ transactionRequestType . setTransactionSettings ( transactionSettings ) ;
125+
126+ var createRequest = new ApiContracts . CreateTransactionRequest ( ) ;
127+ createRequest . setMerchantAuthentication ( merchantAuthenticationType ) ;
128+ createRequest . setTransactionRequest ( transactionRequestType ) ;
129+
130+ //pretty print request
131+ console . log ( JSON . stringify ( createRequest . getJSON ( ) , null , 2 ) ) ;
132+
133+ var ctrl = new ApiControllers . CreateTransactionController ( createRequest . getJSON ( ) ) ;
134+ //Defaults to sandbox
135+ //ctrl.setEnvironment(SDKConstants.endpoint.production);
136+
137+ ctrl . execute ( function ( ) {
138+
139+ var apiResponse = ctrl . getResponse ( ) ;
140+
141+ var response = new ApiContracts . CreateTransactionResponse ( apiResponse ) ;
142+
143+ //pretty print response
144+ console . log ( JSON . stringify ( response , null , 2 ) ) ;
145+
146+ if ( response != null ) {
147+ if ( response . getMessages ( ) . getResultCode ( ) == ApiContracts . MessageTypeEnum . OK ) {
148+ if ( response . getTransactionResponse ( ) . getMessages ( ) != null ) {
149+ console . log ( 'Successfully created transaction with Transaction ID: ' + response . getTransactionResponse ( ) . getTransId ( ) ) ;
150+ console . log ( 'Response Code: ' + response . getTransactionResponse ( ) . getResponseCode ( ) ) ;
151+ console . log ( 'Message Code: ' + response . getTransactionResponse ( ) . getMessages ( ) . getMessage ( ) [ 0 ] . getCode ( ) ) ;
152+ console . log ( 'Description: ' + response . getTransactionResponse ( ) . getMessages ( ) . getMessage ( ) [ 0 ] . getDescription ( ) ) ;
153+ }
154+ else {
155+ console . log ( 'Failed Transaction.' ) ;
156+ if ( response . getTransactionResponse ( ) . getErrors ( ) != null ) {
157+ console . log ( 'Error Code: ' + response . getTransactionResponse ( ) . getErrors ( ) . getError ( ) [ 0 ] . getErrorCode ( ) ) ;
158+ console . log ( 'Error message: ' + response . getTransactionResponse ( ) . getErrors ( ) . getError ( ) [ 0 ] . getErrorText ( ) ) ;
159+ }
160+ }
161+ }
162+ else {
163+ console . log ( 'Failed Transaction. ' ) ;
164+ if ( response . getTransactionResponse ( ) != null && response . getTransactionResponse ( ) . getErrors ( ) != null ) {
165+
166+ console . log ( 'Error Code: ' + response . getTransactionResponse ( ) . getErrors ( ) . getError ( ) [ 0 ] . getErrorCode ( ) ) ;
167+ console . log ( 'Error message: ' + response . getTransactionResponse ( ) . getErrors ( ) . getError ( ) [ 0 ] . getErrorText ( ) ) ;
168+ }
169+ else {
170+ console . log ( 'Error Code: ' + response . getMessages ( ) . getMessage ( ) [ 0 ] . getCode ( ) ) ;
171+ console . log ( 'Error message: ' + response . getMessages ( ) . getMessage ( ) [ 0 ] . getText ( ) ) ;
172+ }
173+ }
174+ }
175+ else {
176+ console . log ( 'Null Response.' ) ;
177+ }
178+
179+ callback ( response ) ;
180+ } ) ;
181+ }
182+
183+ if ( require . main === module ) {
184+ createAnAcceptPaymentTransaction ( function ( ) {
185+ console . log ( 'createAnAcceptPaymentTransaction call complete.' ) ;
186+ } ) ;
187+ }
188+
189+ module . exports . createAnAcceptPaymentTransaction = createAnAcceptPaymentTransaction ;
0 commit comments