|
| 1 | +Old Usage Examples (Of Deprecated SDK Functionality) |
| 2 | +======================================= |
| 3 | + |
| 4 | +**PLEASE NOTE: These examples are for deprecated functionality. Refer to the [README](/README.md) in root folder of the SDK for the new examples.** |
| 5 | + |
| 6 | +## Requirements |
| 7 | + |
| 8 | +- PHP 5.3+ *(>=5.3.10 recommended)* |
| 9 | +- cURL PHP Extension |
| 10 | +- JSON PHP Extension |
| 11 | +- SimpleXML PHP Extension |
| 12 | +- An Authorize.Net Merchant Account or Sandbox Account. You can get a |
| 13 | + free sandbox account at http://developer.authorize.net/sandbox/ |
| 14 | + |
| 15 | +## Autoloading |
| 16 | + |
| 17 | +[`Composer`](http://getcomposer.org) currently has a [MITM](https://github.com/composer/composer/issues/1074) |
| 18 | +security vulnerability. However, if you wish to use it, require its autoloader in |
| 19 | +your script or bootstrap file: |
| 20 | +```php |
| 21 | +require 'vendor/autoload.php'; |
| 22 | +``` |
| 23 | +*Note: you'll need a composer.json file with the following require section and to run |
| 24 | +`composer update`.* |
| 25 | +```json |
| 26 | +"require": { |
| 27 | + "authorizenet/authorizenet": "~1.8" |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Alternatively, we provide a custom `SPL` autoloader: |
| 32 | +```php |
| 33 | +require 'path/to/anet_php_sdk/autoload.php'; |
| 34 | +``` |
| 35 | + |
| 36 | +## Authentication |
| 37 | +To authenticate with the Authorize.Net API you will need to retrieve your API Login ID and Transaction Key from the [`Merchant Interface`](https://account.authorize.net/). You can find these details in the Settings section. |
| 38 | +If you need a sandbox account you can sign up for one really easily [`here`](https://developer.authorize.net/sandbox/). |
| 39 | + |
| 40 | +Once you have your keys simply plug them into the appropriate variables as per the samples below. |
| 41 | + |
| 42 | +````php |
| 43 | +define("AUTHORIZENET_API_LOGIN_ID", "YOURLOGIN"); |
| 44 | +define("AUTHORIZENET_TRANSACTION_KEY", "YOURKEY"); |
| 45 | +```` |
| 46 | + |
| 47 | +## Usage Examples |
| 48 | + |
| 49 | +See below for basic usage examples. View the `tests/` folder for more examples of |
| 50 | +each API. Additional documentation is in the `docs/` folder. |
| 51 | + |
| 52 | +### AuthorizeNetAIM.php Quick Usage Example |
| 53 | + |
| 54 | +```php |
| 55 | +define("AUTHORIZENET_API_LOGIN_ID", "YOURLOGIN"); |
| 56 | +define("AUTHORIZENET_TRANSACTION_KEY", "YOURKEY"); |
| 57 | +define("AUTHORIZENET_SANDBOX", true); |
| 58 | +$sale = new AuthorizeNetAIM; |
| 59 | +$sale->amount = "5.99"; |
| 60 | +$sale->card_num = '6011000000000012'; |
| 61 | +$sale->exp_date = '04/15'; |
| 62 | +$response = $sale->authorizeAndCapture(); |
| 63 | +if ($response->approved) { |
| 64 | + $transaction_id = $response->transaction_id; |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### AuthorizeNetAIM.php Advanced Usage Example |
| 69 | + |
| 70 | +```php |
| 71 | +define("AUTHORIZENET_API_LOGIN_ID", "YOURLOGIN"); |
| 72 | +define("AUTHORIZENET_TRANSACTION_KEY", "YOURKEY"); |
| 73 | +define("AUTHORIZENET_SANDBOX", true); |
| 74 | +$auth = new AuthorizeNetAIM; |
| 75 | +$auth->amount = "45.00"; |
| 76 | + |
| 77 | +// Use eCheck: |
| 78 | +$auth->setECheck( |
| 79 | + '121042882', |
| 80 | + '123456789123', |
| 81 | + 'CHECKING', |
| 82 | + 'Bank of Earth', |
| 83 | + 'Jane Doe', |
| 84 | + 'WEB' |
| 85 | +); |
| 86 | + |
| 87 | +// Set multiple line items: |
| 88 | +$auth->addLineItem('item1', 'Golf tees', 'Blue tees', '2', '5.00', 'N'); |
| 89 | +$auth->addLineItem('item2', 'Golf shirt', 'XL', '1', '40.00', 'N'); |
| 90 | + |
| 91 | +// Set Invoice Number: |
| 92 | +$auth->invoice_num = time(); |
| 93 | + |
| 94 | +// Set a Merchant Defined Field: |
| 95 | +$auth->setCustomField("entrance_source", "Search Engine"); |
| 96 | + |
| 97 | +// Authorize Only: |
| 98 | +$response = $auth->authorizeOnly(); |
| 99 | + |
| 100 | +if ($response->approved) { |
| 101 | + $auth_code = $response->transaction_id; |
| 102 | + |
| 103 | + // Now capture: |
| 104 | + $capture = new AuthorizeNetAIM; |
| 105 | + $capture_response = $capture->priorAuthCapture($auth_code); |
| 106 | + |
| 107 | + // Now void: |
| 108 | + $void = new AuthorizeNetAIM; |
| 109 | + $void_response = $void->void($capture_response->transaction_id); |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### AuthorizeNetARB.php Usage Example |
| 114 | + |
| 115 | +```php |
| 116 | +define("AUTHORIZENET_API_LOGIN_ID", "YOURLOGIN"); |
| 117 | +define("AUTHORIZENET_TRANSACTION_KEY", "YOURKEY"); |
| 118 | +$subscription = new AuthorizeNet_Subscription; |
| 119 | +$subscription->name = "PHP Monthly Magazine"; |
| 120 | +$subscription->intervalLength = "1"; |
| 121 | +$subscription->intervalUnit = "months"; |
| 122 | +$subscription->startDate = "2011-03-12"; |
| 123 | +$subscription->totalOccurrences = "12"; |
| 124 | +$subscription->amount = "12.99"; |
| 125 | +$subscription->creditCardCardNumber = "6011000000000012"; |
| 126 | +$subscription->creditCardExpirationDate= "2018-10"; |
| 127 | +$subscription->creditCardCardCode = "123"; |
| 128 | +$subscription->billToFirstName = "Rasmus"; |
| 129 | +$subscription->billToLastName = "Doe"; |
| 130 | + |
| 131 | +// Create the subscription. |
| 132 | +$request = new AuthorizeNetARB; |
| 133 | +$response = $request->createSubscription($subscription); |
| 134 | +$subscription_id = $response->getSubscriptionId(); |
| 135 | +``` |
| 136 | + |
| 137 | +### AuthorizeNetCIM.php Usage Example |
| 138 | + |
| 139 | +```php |
| 140 | +define("AUTHORIZENET_API_LOGIN_ID", "YOURLOGIN"); |
| 141 | +define("AUTHORIZENET_TRANSACTION_KEY", "YOURKEY"); |
| 142 | +$request = new AuthorizeNetCIM; |
| 143 | +// Create new customer profile |
| 144 | +$customerProfile = new AuthorizeNetCustomer; |
| 145 | +$customerProfile->description = "Description of customer"; |
| 146 | +$customerProfile->merchantCustomerId = time(); |
| 147 | +$customerProfile->email = "test@domain.com"; |
| 148 | +$response = $request->createCustomerProfile($customerProfile); |
| 149 | +if ($response->isOk()) { |
| 150 | + $customerProfileId = $response->getCustomerProfileId(); |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +### AuthorizeNetSIM.php Usage Example |
| 155 | + |
| 156 | +```php |
| 157 | +define("AUTHORIZENET_API_LOGIN_ID", "YOURLOGIN"); |
| 158 | +define("AUTHORIZENET_MD5_SETTING", ""); |
| 159 | +$message = new AuthorizeNetSIM; |
| 160 | +if ($message->isAuthorizeNet()) { |
| 161 | + $transactionId = $message->transaction_id; |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +### AuthorizeNetDPM.php Usage Example |
| 166 | + |
| 167 | +```php |
| 168 | +$url = "http://YOUR_DOMAIN.com/direct_post.php"; |
| 169 | +$api_login_id = 'YOUR_API_LOGIN_ID'; |
| 170 | +$transaction_key = 'YOUR_TRANSACTION_KEY'; |
| 171 | +$md5_setting = 'YOUR_MD5_SETTING'; // Your MD5 Setting |
| 172 | +$amount = "5.99"; |
| 173 | +AuthorizeNetDPM::directPostDemo($url, $api_login_id, $transaction_key, $amount, $md5_setting); |
| 174 | +``` |
| 175 | + |
| 176 | +### AuthorizeNetCP.php Usage Example |
| 177 | + |
| 178 | +```php |
| 179 | +define("AUTHORIZENET_API_LOGIN_ID", "YOURLOGIN"); |
| 180 | +define("AUTHORIZENET_TRANSACTION_KEY", "YOURKEY"); |
| 181 | +define("AUTHORIZENET_MD5_SETTING", ""); |
| 182 | +$sale = new AuthorizeNetCP; |
| 183 | +$sale->amount = '59.99'; |
| 184 | +$sale->device_type = '4'; |
| 185 | +$sale->setTrack1Data('%B4111111111111111^CARDUSER/JOHN^1803101000000000020000831000000?'); |
| 186 | +$response = $sale->authorizeAndCapture(); |
| 187 | +$trans_id = $response->transaction_id; |
| 188 | +``` |
| 189 | + |
| 190 | +### AuthorizeNetTD.php Usage Example |
| 191 | + |
| 192 | +```php |
| 193 | +define("AUTHORIZENET_API_LOGIN_ID", "YOURLOGIN"); |
| 194 | +define("AUTHORIZENET_TRANSACTION_KEY", "YOURKEY"); |
| 195 | +$request = new AuthorizeNetTD; |
| 196 | +$response = $request->getTransactionDetails("12345"); |
| 197 | +echo $response->xml->transaction->transactionStatus; |
| 198 | +``` |
0 commit comments