@@ -22,11 +22,15 @@ function chargeCreditCard($amount)
2222 $ creditCard ->setCardNumber ("4111111111111111 " );
2323 $ creditCard ->setExpirationDate ("1226 " );
2424 $ creditCard ->setCardCode ("123 " );
25+
26+ // Add the payment data to a paymentType object
2527 $ paymentOne = new AnetAPI \PaymentType ();
26- $ paymentOne ->setCreditCard ( $ creditCard );
28+ $ paymentOne ->setOpaqueData ( $ opaqueData );
2729
30+ // Create order information
2831 $ order = new AnetAPI \OrderType ();
29- $ order ->setDescription ("New Item " );
32+ $ order ->invoiceNumber ("10101 " );
33+ $ order ->setDescription ("Golf Shirts " );
3034
3135 // Set the customer's Bill To address
3236 $ customerAddress = new AnetAPI \CustomerAddressType ();
@@ -50,73 +54,68 @@ function chargeCreditCard($amount)
5054 $ duplicateWindowSetting ->setSettingName ("duplicateWindow " );
5155 $ duplicateWindowSetting ->setSettingValue ("600 " );
5256
53- // Create a TransactionRequestType object
57+ // Create a transactionRequestType object and add the previous objects to it
5458 $ transactionRequestType = new AnetAPI \TransactionRequestType ();
55- $ transactionRequestType ->setTransactionType ( "authCaptureTransaction " );
59+ $ transactionRequestType ->setTransactionType ("authCaptureTransaction " );
5660 $ transactionRequestType ->setAmount ($ amount );
5761 $ transactionRequestType ->setOrder ($ order );
5862 $ transactionRequestType ->setPayment ($ paymentOne );
5963 $ transactionRequestType ->setBillTo ($ customerAddress );
6064 $ transactionRequestType ->setCustomer ($ customerData );
6165 $ transactionRequestType ->addToTransactionSettings ($ duplicateWindowSetting );
6266
67+ // Assemble the complete transaction request
6368 $ request = new AnetAPI \CreateTransactionRequest ();
6469 $ request ->setMerchantAuthentication ($ merchantAuthentication );
65- $ request ->setRefId ( $ refId );
66- $ request ->setTransactionRequest ( $ transactionRequestType );
70+ $ request ->setRefId ($ refId );
71+ $ request ->setTransactionRequest ($ transactionRequestType );
6772
73+ // Create the controller and get the response
6874 $ controller = new AnetController \CreateTransactionController ($ request );
69- $ response = $ controller ->executeWithApiResponse ( \net \authorize \api \constants \ANetEnvironment::SANDBOX );
75+ $ response = $ controller ->executeWithApiResponse (\net \authorize \api \constants \ANetEnvironment::SANDBOX );
7076
7177
72- if ($ response != null )
73- {
74- if ($ response ->getMessages ()->getResultCode () == \SampleCode \Constants::RESPONSE_OK )
75- {
76- $ tresponse = $ response ->getTransactionResponse ();
78+ if ($ response != null ) {
79+ // Check to see if the API request was successfully received and acted upon
80+ if ($ response ->getMessages ()->getResultCode () == \SampleCode \Constants::RESPONSE_OK ) {
81+ // Since the API request was successful, look for a transaction response
82+ // and parse it to display the results of authorizing the card
83+ $ tresponse = $ response ->getTransactionResponse ();
7784
78- if ($ tresponse != null && $ tresponse ->getMessages () != null )
79- {
80- echo " Transaction Response Code : " . $ tresponse ->getResponseCode () . "\n" ;
81- echo " Successfully created an authCapture transaction with Auth Code : " . $ tresponse ->getAuthCode () . "\n" ;
82- echo " Transaction ID : " . $ tresponse ->getTransId () . "\n" ;
83- echo " Code : " . $ tresponse ->getMessages ()[0 ]->getCode () . "\n" ;
84- echo " Description : " . $ tresponse ->getMessages ()[0 ]->getDescription () . "\n" ;
85- }
86- else
87- {
88- echo "Transaction Failed \n" ;
89- if ($ tresponse ->getErrors () != null )
90- {
91- echo " Error code : " . $ tresponse ->getErrors ()[0 ]->getErrorCode () . "\n" ;
92- echo " Error message : " . $ tresponse ->getErrors ()[0 ]->getErrorText () . "\n" ;
93- }
94- }
95- }
96- else
97- {
98- echo "Transaction Failed \n" ;
99- $ tresponse = $ response ->getTransactionResponse ();
85+ if ($ tresponse != null && $ tresponse ->getMessages () != null ) {
86+ echo " Successfully created transaction with Transaction ID: " . $ tresponse ->getTransId () . "\n" ;
87+ echo " Transaction Response Code: " . $ tresponse ->getResponseCode () . "\n" ;
88+ echo " Message Code: " . $ tresponse ->getMessages ()[0 ]->getCode () . "\n" ;
89+ echo " Auth Code: " . $ tresponse ->getAuthCode () . "\n" ;
90+ echo " Description: " . $ tresponse ->getMessages ()[0 ]->getDescription () . "\n" ;
91+ } else {
92+ echo "Transaction Failed \n" ;
93+ if ($ tresponse ->getErrors () != null ) {
94+ echo " Error Code : " . $ tresponse ->getErrors ()[0 ]->getErrorCode () . "\n" ;
95+ echo " Error Message : " . $ tresponse ->getErrors ()[0 ]->getErrorText () . "\n" ;
96+ }
97+ }
98+ // Or, print errors if the API request wasn't successful
99+ } else {
100+ echo "Transaction Failed \n" ;
101+ $ tresponse = $ response ->getTransactionResponse ();
100102
101- if ($ tresponse != null && $ tresponse ->getErrors () != null )
102- {
103- echo " Error code : " . $ tresponse ->getErrors ()[0 ]->getErrorCode () . "\n" ;
104- echo " Error message : " . $ tresponse ->getErrors ()[0 ]->getErrorText () . "\n" ;
105- }
106- else
107- {
108- echo " Error code : " . $ response ->getMessages ()->getMessage ()[0 ]->getCode () . "\n" ;
109- echo " Error message : " . $ response ->getMessages ()->getMessage ()[0 ]->getText () . "\n" ;
110- }
111- }
112- }
113- else
114- {
115- echo "No response returned \n" ;
103+ if ($ tresponse != null && $ tresponse ->getErrors () != null ) {
104+ echo " Error Code : " . $ tresponse ->getErrors ()[0 ]->getErrorCode () . "\n" ;
105+ echo " Error Message : " . $ tresponse ->getErrors ()[0 ]->getErrorText () . "\n" ;
106+ } else {
107+ echo " Error Code : " . $ response ->getMessages ()->getMessage ()[0 ]->getCode () . "\n" ;
108+ echo " Error Message : " . $ response ->getMessages ()->getMessage ()[0 ]->getText () . "\n" ;
109+ }
110+ }
111+ } else {
112+ echo "No response returned \n" ;
116113 }
117114
118115 return $ response ;
119- }
120- if (!defined ('DONT_RUN_SAMPLES ' ))
116+ }
117+
118+ if (!defined ('DONT_RUN_SAMPLES ' )) {
121119 chargeCreditCard (\SampleCode \Constants::SAMPLE_AMOUNT );
120+ }
122121?>
0 commit comments