Sample Shopping Cart Check-Out Code in PHP
(Note: Click here to download a fully working PHP web
page that demonstrates calling I-Bill IT
from PHP.)
Using I-Bill IT, on a PHP page is quite simple.
There are two different paths to creating a payment depending upon the needs
of your site. The easy way is to use the SubmitCreditCardPayment()
method of the BillIt object. The code looks like this*:
// Create the BillIt object and set login credentials
$billit = new COM("ITDevWorks.BillIt.BillIt");
$billit->MerchantId = "xxxxxxxxxxxxxxx";
$billit->TransactionKey = "xxxxxxxxxxxxxxx";
// Submit the actual payment
$resp = $billit->SubmitCreditCardPayment(249.99, "4111111111111111",
new VARIANT("12/31/2010", VT_DATE), "111");
*This sample, and any other PHP samples on this site are all written for PHP 5, which has
significantly better support for COM than previous versions. If you need
help getting I-Bill IT to work on an earlier
version of PHP, please contact us for support.
On the other hand, to take full advantage of the features that I-Bill
IT offers, create a Payment object, set properties on it to suit your site's needs,
then call the Payment.Submit() method.
define("AUTHNET_API_LOGIN", "<your-merchant-api-login-here>");
define("AUTHNET_TRANSACTION_KEY", "<your-transaction-key-here>");
function MakePayment()
{
//
// Create the BillIt object
//
$billit = new COM("ITDevWorks.BillIt.BillIt");
//
// Set the login credentials
//
$billit->MerchantId = AUTHNET_API_LOGIN;
$billit->TransactionKey = AUTHNET_TRANSACTION_KEY;
//
// Create the Payment object
//
$pmt = $billit->CreatePayment();
//
// This reduces the chances of getting "A duplicate
// transaction has already been submitted" responses
// from the gateway when doing testing.
//
$pmt->DuplicateCheckTimeout = 1;
//
// Add some products to the list of purchased items.
//
$pmt->OrderLineItems->Add("ITM0496", "Digital Camera",
"10 Megapixel digital camera", 1, 249.99, FALSE);
$pmt->OrderLineItems->Add("ITM0395", "Camera Carry Bag",
"Nylon camera carrying case", 1, 49.99, FALSE);
$pmt->OrderLineItems->Add("ITM9655", "AA Btry 4-pack",
"Package of 4 AA Alkaline batteries", 4, 3.99, FALSE);
//
// Add the shipping costs for each item
//
$pmt->Freight->Add(7.99, "Digital camera S&H", "");
$pmt->Freight->Add(3.99, "Camera carrying case S&H", "");
$pmt->Freight->Add(4.99, "Batteries S&H", "");
//
// NOTE: The following line of code must be called.
// The OrderLineItem, Tax, Freight, and Duty collections
// do not automatically set the amount that will be billed.
//
$pmt->Amount = $pmt->CalculateTotalAmount(TRUE, TRUE, TRUE, FALSE);
//
// Set the credit card properties
//
$pmt->CreditCardNumber = "4111111111111111";
$pmt->CreditCardExpiration = new VARIANT("12/31/2010", VT_DATE);
$pmt->CreditCardCode = "111";
//
// Set the customer properties (optional for most payments)
//
$pmt->CustomerBillTo->FirstName = "John";
$pmt->CustomerBillTo->LastName = "Doe";
$pmt->CustomerEmail = "johnd@somecompany.com";
$pmt->CustomerPhone = "(425)555-6789";
//
// Submit the payment to the Authorize.Net gateway
//
return $pmt->Submit();
}
There is no requirement to use .OrderLineItems or any of the .Tax, .Freight, or .Duty
collections, but doing so can capture much more detail about transactions in your
Authorize.net account. By caching the Payment object, the OrderLineItems collection
can also be your in-session shopping cart data store. Every object in I-Bill IT
supports a .UserData property, which allows you to attach any data of your own to
it. For the OrderLineItems collection, you could store the CartId from your
database, or even a whole object representing the persisted cart in your database.
However you choose to use it, I-Bill IT can increase your
reliability, simplify your development effort, and ultimately shorten your time
to market. Buy I-Bill IT today and find out for yourself.