|
| 1 | +<?php |
| 2 | + require 'vendor/autoload.php'; |
| 3 | + |
| 4 | + use net\authorize\api\contract\v1 as AnetAPI; |
| 5 | + use net\authorize\api\controller as AnetController; |
| 6 | + |
| 7 | + define("AUTHORIZENET_LOG_FILE", "phplog"); |
| 8 | + |
| 9 | +function chargeCreditCard($amount) |
| 10 | +{ |
| 11 | + /* Create a merchantAuthenticationType object with authentication details |
| 12 | + retrieved from the constants file */ |
| 13 | + $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); |
| 14 | + $merchantAuthentication->setName(\SampleCode\Constants::MERCHANT_LOGIN_ID); |
| 15 | + $merchantAuthentication->setTransactionKey(\SampleCode\Constants::MERCHANT_TRANSACTION_KEY); |
| 16 | + |
| 17 | + // Set the transaction's refId |
| 18 | + $refId = 'ref' . time(); |
| 19 | + |
| 20 | + // Create the payment data for a credit card |
| 21 | + $creditCard = new AnetAPI\CreditCardType(); |
| 22 | + $creditCard->setCardNumber("4111111111111111"); |
| 23 | + $creditCard->setExpirationDate("2038-12"); |
| 24 | + $creditCard->setCardCode("123"); |
| 25 | + |
| 26 | + // Add the payment data to a paymentType object |
| 27 | + $paymentOne = new AnetAPI\PaymentType(); |
| 28 | + $paymentOne->setCreditCard($creditCard); |
| 29 | + |
| 30 | + // Create order information |
| 31 | + $order = new AnetAPI\OrderType(); |
| 32 | + $order->setInvoiceNumber("10101"); |
| 33 | + $order->setDescription("Golf Shirts"); |
| 34 | + |
| 35 | + // Set the customer's Bill To address |
| 36 | + $customerAddress = new AnetAPI\CustomerAddressType(); |
| 37 | + $customerAddress->setFirstName("Ellen"); |
| 38 | + $customerAddress->setLastName("Johnson"); |
| 39 | + $customerAddress->setCompany("Souveniropolis"); |
| 40 | + $customerAddress->setAddress("14 Main Street"); |
| 41 | + $customerAddress->setCity("Pecan Springs"); |
| 42 | + $customerAddress->setState("TX"); |
| 43 | + $customerAddress->setZip("44628"); |
| 44 | + $customerAddress->setCountry("USA"); |
| 45 | + |
| 46 | + // Set the customer's identifying information |
| 47 | + $customerData = new AnetAPI\CustomerDataType(); |
| 48 | + $customerData->setType("individual"); |
| 49 | + $customerData->setId("99999456654"); |
| 50 | + $customerData->setEmail("EllenJohnson@example.com"); |
| 51 | + |
| 52 | + // Add values for transaction settings |
| 53 | + $duplicateWindowSetting = new AnetAPI\SettingType(); |
| 54 | + $duplicateWindowSetting->setSettingName("duplicateWindow"); |
| 55 | + $duplicateWindowSetting->setSettingValue("60"); |
| 56 | + |
| 57 | + // Add some merchant defined fields. These fields won't be stored with the transaction, |
| 58 | + // but will be echoed back in the response. |
| 59 | + $merchantDefinedField1 = new AnetAPI\UserFieldType(); |
| 60 | + $merchantDefinedField1->setName("customerLoyaltyNum"); |
| 61 | + $merchantDefinedField1->setValue("1128836273"); |
| 62 | + |
| 63 | + $merchantDefinedField2 = new AnetAPI\UserFieldType(); |
| 64 | + $merchantDefinedField2->setName("favoriteColor"); |
| 65 | + $merchantDefinedField2->setValue("blue"); |
| 66 | + |
| 67 | + // Create a TransactionRequestType object and add the previous objects to it |
| 68 | + $transactionRequestType = new AnetAPI\TransactionRequestType(); |
| 69 | + $transactionRequestType->setTransactionType("authCaptureTransaction"); |
| 70 | + $transactionRequestType->setAmount($amount); |
| 71 | + $transactionRequestType->setOrder($order); |
| 72 | + $transactionRequestType->setPayment($paymentOne); |
| 73 | + $transactionRequestType->setBillTo($customerAddress); |
| 74 | + $transactionRequestType->setCustomer($customerData); |
| 75 | + $transactionRequestType->addToTransactionSettings($duplicateWindowSetting); |
| 76 | + $transactionRequestType->addToUserFields($merchantDefinedField1); |
| 77 | + $transactionRequestType->addToUserFields($merchantDefinedField2); |
| 78 | + |
| 79 | + // Assemble the complete transaction request |
| 80 | + $request = new AnetAPI\CreateTransactionRequest(); |
| 81 | + $request->setMerchantAuthentication($merchantAuthentication); |
| 82 | + $request->setRefId($refId); |
| 83 | + $request->setTransactionRequest($transactionRequestType); |
| 84 | + |
| 85 | + // Create the controller and get the response |
| 86 | + $controller = new AnetController\CreateTransactionController($request); |
| 87 | + $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX); |
| 88 | + |
| 89 | + |
| 90 | + if ($response != null) { |
| 91 | + // Check to see if the API request was successfully received and acted upon |
| 92 | + if ($response->getMessages()->getResultCode() == \SampleCode\Constants::RESPONSE_OK) { |
| 93 | + // Since the API request was successful, look for a transaction response |
| 94 | + // and parse it to display the results of authorizing the card |
| 95 | + $tresponse = $response->getTransactionResponse(); |
| 96 | + |
| 97 | + if ($tresponse != null && $tresponse->getMessages() != null) { |
| 98 | + echo " Successfully created transaction with Transaction ID: " . $tresponse->getTransId() . "\n"; |
| 99 | + echo " Transaction Response Code: " . $tresponse->getResponseCode() . "\n"; |
| 100 | + echo " Message Code: " . $tresponse->getMessages()[0]->getCode() . "\n"; |
| 101 | + echo " Auth Code: " . $tresponse->getAuthCode() . "\n"; |
| 102 | + echo " Description: " . $tresponse->getMessages()[0]->getDescription() . "\n"; |
| 103 | + } else { |
| 104 | + echo "Transaction Failed \n"; |
| 105 | + if ($tresponse->getErrors() != null) { |
| 106 | + echo " Error Code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n"; |
| 107 | + echo " Error Message : " . $tresponse->getErrors()[0]->getErrorText() . "\n"; |
| 108 | + } |
| 109 | + } |
| 110 | + // Or, print errors if the API request wasn't successful |
| 111 | + } else { |
| 112 | + echo "Transaction Failed \n"; |
| 113 | + $tresponse = $response->getTransactionResponse(); |
| 114 | + |
| 115 | + if ($tresponse != null && $tresponse->getErrors() != null) { |
| 116 | + echo " Error Code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n"; |
| 117 | + echo " Error Message : " . $tresponse->getErrors()[0]->getErrorText() . "\n"; |
| 118 | + } else { |
| 119 | + echo " Error Code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n"; |
| 120 | + echo " Error Message : " . $response->getMessages()->getMessage()[0]->getText() . "\n"; |
| 121 | + } |
| 122 | + } |
| 123 | + } else { |
| 124 | + echo "No response returned \n"; |
| 125 | + } |
| 126 | + |
| 127 | + return $response; |
| 128 | +} |
| 129 | + |
| 130 | +if (!defined('DONT_RUN_SAMPLES')) { |
| 131 | + chargeCreditCard(\SampleCode\Constants::SAMPLE_AMOUNT); |
| 132 | +} |
0 commit comments