As with DataCash, you’ll have to modify the PsCheckFundsand PsTakePaymentclasses to use the new Authorize.net functionality.
Remember that you can use the files from the Source Code Download section of the Apress web site (http://www.apress.com/) instead of typing the code yourself.
The final modifications involve changing the pipeline section classes that deal with credit card transactions (PsCheckFundsand PsTakePayment). We’ve already included the infrastructure for storing and retrieving authentication code and reference information via the OrderProcessor::SetOrderAuthCodeAndReferencemethod.
Exercise: Implementing the Order Pipeline Classes
1. First, modify business/ps_check_funds.phpto work with Authorize.net:
<?php
class PsCheckFunds implements IPipelineSection {
public function Process($processor) {
// Audit
$processor->CreateAudit('PsCheckFunds started.', 20100);
$order_total_cost = $processor->mOrderInfo['total_amount'];
$order_total_cost += $processor->mOrderInfo['shipping_cost'];
$order_total_cost +=
round((float)$order_total_cost *
(float)$this->mOrderInfo['tax_percentage'], 2) / 100.00;
$exp_date = str_replace('/', '',
$processor->mCustomerInfo['credit_card']->ExpiryDate);
$transaction = array (
'x_invoice_num' => $processor->mOrderInfo['order_id'], 'x_amount' => $order_total_cost, // Amount to charge
'x_card_num' => $processor->mCustomerInfo['credit_card']->CardNumber, 'x_exp_date' => $exp_date, // Expiry (MMYY)
'x_method' => 'CC', 'x_type' => 'AUTH_ONLY');
// Process Transaction
$request = new AuthorizeNetRequest(AUTHORIZE_NET_URL);
$request->SetRequest($transaction);
$response = $request->GetResponse();
$response = explode('|', $response);
if ($response[0] == 1) {
$processor->SetAuthCodeAndReference($response[4], $response[6]);
// Audit
$processor->CreateAudit('Funds available for purchase.', 20102);
// Update order status
$processor->UpdateOrderStatus(2);
// Continue processing
$processor->mContinueNow = true;
} else {
// Audit
$processor->CreateAudit('Funds not available for purchase.', 20103);
throw new Exception('Credit card check funds failed for order ' .
$processor->mOrderInfo['order_id'] . ".\n\n" . 'Data exchanged:' . "\n" .
var_export($transaction, true) . "\n" . var_export($response, true));
} // Audit
$processor->CreateAudit('PsCheckFunds finished.', 20101);
} }
?>
2. Modify business/ps_take_payment.phpas follows:
<?php
class PsTakePayment implements IPipelineSection {
public function Process($processor) {
// Audit
$processor->CreateAudit('PsTakePayment started.', 20400);
$transaction =
array ('x_ref_trans_id' => $processor->mOrderInfo['reference'], 'x_method' => 'CC',
'x_type' => 'PRIOR_AUTH_CAPTURE');
// Process Transaction
$request = new AuthorizeNetRequest(AUTHORIZE_NET_URL);
$request->SetRequest($transaction);
$response = $request->GetResponse();
$response = explode('|', $response);
if ($response[0] == 1) {
// Audit
$processor->CreateAudit(
'Funds deducted from customer credit card account.', 20402);
// Update order status
$processor->UpdateOrderStatus(5);
// Continue processing
$processor->mContinueNow = true;
// Audit
$processor->CreateAudit('PsTakePayment finished.', 20401);
} else {
// Audit
$processor->CreateAudit(
'Error taking funds from customer credit card.', 20403);
throw new Exception('Credit card take payment failed for order ' .
$processor->mOrderInfo['order_id'] . ".\n\n" . 'Data exchanged:' . "\n" .
var_export($transaction, true) . "\n" . var_export($response, true));
} } }
?>
3. Add a reference to the business/authorize_net_request.phpfile in include/app_top.phpas highlighted:
require_once BUSINESS_DIR . 'ps_ship_ok.php';
require_once BUSINESS_DIR . 'ps_final_notification.php';
require_once BUSINESS_DIR . 'authorize_net_request.php';
Testing Authorize.net Integration
All you have to do now is run some tests with your new web site. Retrieve the list of “magic”
Authorize.net credit card numbers from the Advanced Integration Method (AIM) Imple- mentation Guide, and experiment doing transactions with them.
Summary
In this chapter, you have completed your e-commerce application by integrating it with credit card authorization. Short of putting your own products in, hooking it up with your suppliers, getting a merchant bank account, and putting it on the web, you’re ready to go. Okay, so that’s still quite a lot of work, but none of it is particularly difficult. The hard work is behind you now.
Specifically, in this chapter, we have looked at the theory behind credit card transactions on the web and looked at one full implementation—DataCash. We created a library that can be used to access DataCash and integrated it with our application. We also looked at Authorize.net.
Product Reviews
At this point, you have a complete and functional e-commerce web site. However, this doesn’t stop you from adding even more features to it, making it more useful and pleasant for visitors.
By adding a product review system to your web site, you increase the chances that visitors will get back to your site, either to write a review for a product they bought, or to see what other people think about that product.
A review system can also help you learn your customers’ tastes, which enables you to improve the product recommendations, and even make changes in the web site or the struc- ture of the product catalog based on customer feedback.
To make things easy for both you and the customer, you’ll add the list of product reviews and the form to add a new product review to the products’ details pages. The form to add a new product shows up only for registered users because we decided not to allow anonymous reviews (however, you can easily change this if you like). You’ll create the code for this new fea- ture in the usual way, starting from the database and finishing with the user interface (UI). The final result of your work in this chapter will look like Figure 16-1.
537
C H A P T E R 1 6
Figure 16-1.The product details page containing product reviews
Implementing the Data Tier
For your review system, you have to create a reviewtable and two data tier functions in your hatshopdatabase. The catalog_get_product_reviewsfunction retrieves the reviews for a spe- cific product, and the catalog_create_product_reviewmethod adds a review to a product.