Skip to content

Commit b1dd8b2

Browse files
committed
Update README.md
Added a quick usage example (charge credit card).
1 parent 31a7ce3 commit b1dd8b2

1 file changed

Lines changed: 62 additions & 1 deletion

File tree

README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ If you need a sandbox account you can sign up for one really easily [`here`](htt
4444

4545
Once you have your keys simply plug them into the appropriate variables, as per the below code dealing with the authentication part of the flow.
4646

47+
...
48+
````php
49+
use net\authorize\api\contract\v1 as AnetAPI;
50+
````
51+
...
4752
````php
4853
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
4954
$merchantAuthentication->setName("YOURLOGIN");
@@ -63,7 +68,63 @@ Apart from this README, you can find details and examples of using the SDK in th
6368
- [Developer Center Reference](http://developer.authorize.net/api/reference/index.html)
6469
- [Github Sample Code Repositories](http://developer.authorize.net/api/samplecode/), [php](https://github.com/AuthorizeNet/sample-code-php)
6570

66-
While using these samples, load the appropriate dependencies of the sdk (e.g. 'composer update') and modify paths as needed (e.g. in 'require' statement) for the code to work in your system.
71+
72+
### Quick Usage Example (with Charge Credit Card - Authorize and Capture)
73+
Note: The following is a php console application. Ensure that you can invoke the php command from command line.
74+
- Save the below code to a php file named, say, `charge-credit-card.php`
75+
- Open command prompt and navigate to your sdk folder ( if want to run from a different folder, modify the `require` statement to have the full path to the sdk e.g. `require 'c:/anet-sdk-php/vendor/autoload.php'` in place of `require 'vendor/autoload.php'` )
76+
- Update dependecies - e.g., With composer, type `composer update`
77+
- Type `php [<path to folder containing the php file>\]charge-credit-card.php`
78+
79+
```php
80+
require 'vendor/autoload.php';
81+
use net\authorize\api\contract\v1 as AnetAPI;
82+
use net\authorize\api\controller as AnetController;
83+
define("AUTHORIZENET_LOG_FILE", "phplog");
84+
85+
// Common setup for API credentials
86+
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
87+
$merchantAuthentication->setName("556KThWQ6vf2");
88+
$merchantAuthentication->setTransactionKey("9ac2932kQ7kN2Wzq");
89+
90+
// Create the payment data for a credit card
91+
$creditCard = new AnetAPI\CreditCardType();
92+
$creditCard->setCardNumber("4111111111111111");
93+
$creditCard->setExpirationDate("2038-12");
94+
$paymentOne = new AnetAPI\PaymentType();
95+
$paymentOne->setCreditCard($creditCard);
96+
97+
// Create a transaction
98+
$transactionRequestType = new AnetAPI\TransactionRequestType();
99+
$transactionRequestType->setTransactionType( "authCaptureTransaction");
100+
$transactionRequestType->setAmount(151.51);
101+
$transactionRequestType->setPayment($paymentOne);
102+
103+
$request = new AnetAPI\CreateTransactionRequest();
104+
$request->setMerchantAuthentication($merchantAuthentication);
105+
$request->setTransactionRequest( $transactionRequestType);
106+
$controller = new AnetController\CreateTransactionController($request);
107+
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
108+
109+
if ($response != null)
110+
{
111+
$tresponse = $response->getTransactionResponse();
112+
113+
if (($tresponse != null) && ($tresponse->getResponseCode()=="1") )
114+
{
115+
echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n";
116+
echo "Charge Credit Card TRANS ID : " . $tresponse->getTransId() . "\n";
117+
}
118+
else
119+
{
120+
echo "Charge Credit Card ERROR : Invalid response\n";
121+
}
122+
}
123+
else
124+
{
125+
echo "Charge Credit card Null response returned";
126+
}
127+
```
67128

68129
## Testing
69130

0 commit comments

Comments
 (0)