@@ -5,84 +5,94 @@ var ApiControllers = require('authorizenet').APIControllers;
5
5
var utils = require ( '../utils.js' ) ;
6
6
var constants = require ( '../constants.js' ) ;
7
7
8
- var merchantAuthenticationType = new ApiContracts . MerchantAuthenticationType ( ) ;
9
- merchantAuthenticationType . setName ( constants . apiLoginKey ) ;
10
- merchantAuthenticationType . setTransactionKey ( constants . transactionKey ) ;
11
-
12
- var profileToCharge = new ApiContracts . CustomerProfilePaymentType ( ) ;
13
- profileToCharge . setCustomerProfileId ( '11111' ) ;
14
-
15
- var paymentProfile = new ApiContracts . PaymentProfile ( ) ;
16
- paymentProfile . setPaymentProfileId ( '22222' ) ;
17
- profileToCharge . setPaymentProfile ( paymentProfile ) ;
18
-
19
- var orderDetails = new ApiContracts . OrderType ( ) ;
20
- orderDetails . setInvoiceNumber ( 'INV-12345' ) ;
21
- orderDetails . setDescription ( 'Product Description' ) ;
22
-
23
- var lineItem_id1 = new ApiContracts . LineItemType ( ) ;
24
- lineItem_id1 . setItemId ( '1' ) ;
25
- lineItem_id1 . setName ( 'vase' ) ;
26
- lineItem_id1 . setDescription ( 'cannes logo' ) ;
27
- lineItem_id1 . setQuantity ( '18' ) ;
28
- lineItem_id1 . setUnitPrice ( 45.00 ) ;
29
-
30
- var lineItem_id2 = new ApiContracts . LineItemType ( ) ;
31
- lineItem_id2 . setItemId ( '2' ) ;
32
- lineItem_id2 . setName ( 'vase2' ) ;
33
- lineItem_id2 . setDescription ( 'cannes logo2' ) ;
34
- lineItem_id2 . setQuantity ( '28' ) ;
35
- lineItem_id2 . setUnitPrice ( '25.00' ) ;
36
-
37
- var lineItemList = [ ] ;
38
- lineItemList . push ( lineItem_id1 ) ;
39
- lineItemList . push ( lineItem_id2 ) ;
40
-
41
- var lineItems = new ApiContracts . ArrayOfLineItem ( ) ;
42
- lineItems . setLineItem ( lineItemList ) ;
43
-
44
- var shipTo = new ApiContracts . CustomerAddressType ( ) ;
45
- shipTo . setFirstName ( 'China' ) ;
46
- shipTo . setLastName ( 'Bayles' ) ;
47
- shipTo . setCompany ( 'Thyme for Tea' ) ;
48
- shipTo . setAddress ( '12 Main Street' ) ;
49
- shipTo . setCity ( 'Pecan Springs' ) ;
50
- shipTo . setState ( 'TX' ) ;
51
- shipTo . setZip ( '44628' ) ;
52
- shipTo . setCountry ( 'USA' ) ;
53
-
54
- var transactionRequestType = new ApiContracts . TransactionRequestType ( ) ;
55
- transactionRequestType . setTransactionType ( ApiContracts . TransactionTypeEnum . AUTHCAPTURETRANSACTION ) ;
56
- transactionRequestType . setProfile ( profileToCharge ) ;
57
- transactionRequestType . setAmount ( utils . getRandomAmount ( ) ) ;
58
- transactionRequestType . setLineItems ( lineItems ) ;
59
- transactionRequestType . setOrder ( orderDetails ) ;
60
- transactionRequestType . setShipTo ( shipTo ) ;
61
-
62
- var createRequest = new ApiContracts . CreateTransactionRequest ( ) ;
63
- createRequest . setMerchantAuthentication ( merchantAuthenticationType ) ;
64
- createRequest . setTransactionRequest ( transactionRequestType ) ;
65
-
66
- //pretty print request
67
- console . log ( JSON . stringify ( createRequest . getJSON ( ) , null , 2 ) ) ;
68
-
69
- var ctrl = new ApiControllers . CreateTransactionController ( createRequest . getJSON ( ) ) ;
70
-
71
- ctrl . execute ( function ( ) {
72
-
73
- var apiResponse = ctrl . getResponse ( ) ;
74
-
75
- var response = new ApiContracts . CreateTransactionResponse ( apiResponse ) ;
76
-
77
- //pretty print response
78
- console . log ( JSON . stringify ( response , null , 2 ) ) ;
79
-
80
- if ( response . getMessages ( ) . getResultCode ( ) == ApiContracts . MessageTypeEnum . OK &&
81
- response . getTransactionResponse ( ) . getResponseCode ( ) == '1' ) {
82
- console . log ( 'Transaction ID: ' + response . getTransactionResponse ( ) . getTransId ( ) ) ;
83
- }
84
- else {
85
- console . log ( 'Result Code: ' + response . getMessages ( ) . getResultCode ( ) ) ;
86
- }
87
-
88
- } ) ;
8
+ function chargeCustomerProfile ( customerProfileId , customerPaymentProfileId , callback ) {
9
+ var merchantAuthenticationType = new ApiContracts . MerchantAuthenticationType ( ) ;
10
+ merchantAuthenticationType . setName ( constants . apiLoginKey ) ;
11
+ merchantAuthenticationType . setTransactionKey ( constants . transactionKey ) ;
12
+
13
+ var profileToCharge = new ApiContracts . CustomerProfilePaymentType ( ) ;
14
+ profileToCharge . setCustomerProfileId ( customerProfileId ) ;
15
+
16
+ var paymentProfile = new ApiContracts . PaymentProfile ( ) ;
17
+ paymentProfile . setPaymentProfileId ( customerPaymentProfileId ) ;
18
+ profileToCharge . setPaymentProfile ( paymentProfile ) ;
19
+
20
+ var orderDetails = new ApiContracts . OrderType ( ) ;
21
+ orderDetails . setInvoiceNumber ( 'INV-12345' ) ;
22
+ orderDetails . setDescription ( 'Product Description' ) ;
23
+
24
+ var lineItem_id1 = new ApiContracts . LineItemType ( ) ;
25
+ lineItem_id1 . setItemId ( '1' ) ;
26
+ lineItem_id1 . setName ( 'vase' ) ;
27
+ lineItem_id1 . setDescription ( 'cannes logo' ) ;
28
+ lineItem_id1 . setQuantity ( '18' ) ;
29
+ lineItem_id1 . setUnitPrice ( 45.00 ) ;
30
+
31
+ var lineItem_id2 = new ApiContracts . LineItemType ( ) ;
32
+ lineItem_id2 . setItemId ( '2' ) ;
33
+ lineItem_id2 . setName ( 'vase2' ) ;
34
+ lineItem_id2 . setDescription ( 'cannes logo2' ) ;
35
+ lineItem_id2 . setQuantity ( '28' ) ;
36
+ lineItem_id2 . setUnitPrice ( '25.00' ) ;
37
+
38
+ var lineItemList = [ ] ;
39
+ lineItemList . push ( lineItem_id1 ) ;
40
+ lineItemList . push ( lineItem_id2 ) ;
41
+
42
+ var lineItems = new ApiContracts . ArrayOfLineItem ( ) ;
43
+ lineItems . setLineItem ( lineItemList ) ;
44
+
45
+ var shipTo = new ApiContracts . CustomerAddressType ( ) ;
46
+ shipTo . setFirstName ( 'China' ) ;
47
+ shipTo . setLastName ( 'Bayles' ) ;
48
+ shipTo . setCompany ( 'Thyme for Tea' ) ;
49
+ shipTo . setAddress ( '12 Main Street' ) ;
50
+ shipTo . setCity ( 'Pecan Springs' ) ;
51
+ shipTo . setState ( 'TX' ) ;
52
+ shipTo . setZip ( '44628' ) ;
53
+ shipTo . setCountry ( 'USA' ) ;
54
+
55
+ var transactionRequestType = new ApiContracts . TransactionRequestType ( ) ;
56
+ transactionRequestType . setTransactionType ( ApiContracts . TransactionTypeEnum . AUTHCAPTURETRANSACTION ) ;
57
+ transactionRequestType . setProfile ( profileToCharge ) ;
58
+ transactionRequestType . setAmount ( utils . getRandomAmount ( ) ) ;
59
+ transactionRequestType . setLineItems ( lineItems ) ;
60
+ transactionRequestType . setOrder ( orderDetails ) ;
61
+ transactionRequestType . setShipTo ( shipTo ) ;
62
+
63
+ var createRequest = new ApiContracts . CreateTransactionRequest ( ) ;
64
+ createRequest . setMerchantAuthentication ( merchantAuthenticationType ) ;
65
+ createRequest . setTransactionRequest ( transactionRequestType ) ;
66
+
67
+ //pretty print request
68
+ console . log ( JSON . stringify ( createRequest . getJSON ( ) , null , 2 ) ) ;
69
+
70
+ var ctrl = new ApiControllers . CreateTransactionController ( createRequest . getJSON ( ) ) ;
71
+
72
+ ctrl . execute ( function ( ) {
73
+
74
+ var apiResponse = ctrl . getResponse ( ) ;
75
+
76
+ var response = new ApiContracts . CreateTransactionResponse ( apiResponse ) ;
77
+
78
+ //pretty print response
79
+ console . log ( JSON . stringify ( response , null , 2 ) ) ;
80
+
81
+ if ( response . getMessages ( ) . getResultCode ( ) == ApiContracts . MessageTypeEnum . OK &&
82
+ response . getTransactionResponse ( ) . getResponseCode ( ) == '1' ) {
83
+ console . log ( 'Transaction ID: ' + response . getTransactionResponse ( ) . getTransId ( ) ) ;
84
+ }
85
+ else {
86
+ console . log ( 'Result Code: ' + response . getMessages ( ) . getResultCode ( ) ) ;
87
+ }
88
+ callback ( response ) ;
89
+ } ) ;
90
+ }
91
+
92
+ if ( require . main === module ) {
93
+ chargeCustomerProfile ( '111111' , '222222' , function ( ) {
94
+ console . log ( "chargeCustomerProfile call complete." ) ;
95
+ } ) ;
96
+ }
97
+
98
+ module . exports . chargeCustomerProfile = chargeCustomerProfile ;
0 commit comments