}
/*
 * creates a new "Auth & Capture" transaction with the customer
 * profile ID and payment profile ID selected or created above.
 * An "Auth & Capture" transaction completes both the authorization
 * of the payment information and the transfer of funds in one call.
 */
$transaction = new AuthorizeNetTransaction();
$transaction->amount = $subAmount;
$transaction->customerProfileId = $customerProfileId;
$transaction->customerPaymentProfileId = $paymentProfileId;
$response = $request->createCustomerProfileTransaction("AuthCapture", $transaction);
if (ANet_Response_getResultCode($response) == 'Error') {
    // throws an expception based on the resultCode returned in the response
    $errorMsg = '';
    $code = ANet_Response_getMessageCode($response);
    switch ($code) {
        case 'E00027':
            $msg = ANet_Response_getMessageText($response);
            switch ($msg) {
                case 'A duplicate transaction has been submitted.':
                    // Authorize.net will deny transactions for identical
                    // amounts that are submitted within two minutes of
                    // each other
                    $errorMsg = 'You are submitting orders too rapidly and have ' . 'triggered the duplicate transaction prevention ' . 'mechanisms with the payment gateway. We ' . 'apologize for the inconvenience. Please wait ' . 'two minutes and try again.';
                    break;
                case 'The credit card number is invalid.':
                    $errorMsg = 'Your payment information was declined by ' . 'the payment gateway. Please verify you ' . 'have entered your information correctly ' . 'and try again.';
                    // deletes the invalid payment profile
                    $response = $request->deleteCustomerPaymentProfile($customerProfileId, $paymentProfileId);
                    break;
 $db1->query($query);
 $db1->next_record();
 $fldFirstName = $db1->f('fldFirstName');
 $fldLastName = $db1->f('fldLastName');
 $fldCustomerProfileId = $db1->f('fldANetCustomerProfileId');
 $fldEmail = $db1->f('fldEmail');
 // attempts to charge the user for the subscription
 $transaction = new AuthorizeNetTransaction();
 $transaction->amount = $fldAmount;
 $transaction->customerProfileId = $fldCustomerProfileId;
 $transaction->customerPaymentProfileId = $fldPaymentProfileId;
 $request = new AuthorizeNetCIM();
 $response = $request->createCustomerProfileTransaction("AuthCapture", $transaction);
 // if the transaction fails, cancel the subscription
 if (ANet_Response_getResultCode($response) == 'Error') {
     $data = array('fldActive' => 0, 'fldCancelDate' => $today, 'fldCancelReasonOther' => 'Automatic renewal failed (' . ANet_Response_getMessageCode($response) . ': ' . ANet_Response_getMessageText($response) . ')');
     $db1->updateRec(TBL_COLLEGE_SUBSCRIPTION, $data, 'fldId=' . $fldId);
     // gets the active number of subscriptions for the current coach
     $query = "SELECT COUNT(*) AS subsCount FROM " . TBL_COLLEGE_SUBSCRIPTION . " WHERE fldActive=1 AND fldCoach=" . $fldCoach;
     $db1->query($query);
     $db1->next_record();
     $subsCount = $db1->f('subsCount');
     // increments the coach's cancel count by 1. If the
     // coach has no subscriptions now that this subscription
     // is canceled, set fldSubscribe to 0
     $query = "SELECT fldCancelCount FROM " . TBL_COLLEGE_COACH_REGISTER . " WHERE fldId=" . $fldCoach;
     $db1->query($query);
     $db1->next_record();
     $fldCancelCount = $db1->f('fldCancelCount');
     $new_fldSubscribe = $subsCount > 0 ? 1 : 0;
     $new_fldCancelCount = $fldCancelCount + 1;
<?php

require_once 'anet_php_sdk/AuthorizeNet.php';
require_once 'AuthorizeNetMerchantAccount.php';
// gets the Authorize.net payment profile ID created
// for the subscription from the database
$queryString = "SELECT `%s` FROM `%s` WHERE fldId='%s'";
$query = sprintf($queryString, 'fldPaymentProfileId', TBL_COLLEGE_SUBSCRIPTION, $subId);
$db1->query($query);
$db1->next_record();
$paymentProfileId = $db1->f('fldPaymentProfileId');
// updates the payment profile with the new information
$paymentProfile = new AuthorizeNetPaymentProfile();
$paymentProfile->payment->creditCard->cardNumber = $subCardnumber;
$paymentProfile->payment->creditCard->expirationDate = $subCardExpDate;
$paymentProfile->billTo->firstName = $subFName;
$paymentProfile->billTo->lastName = $subLName;
$paymentProfile->billTo->address = $subAddress;
$paymentProfile->billTo->city = $subCity;
$paymentProfile->billTo->state = $subState;
$paymentProfile->billTo->zip = $subZip;
$request = new AuthorizeNetCIM();
$response = $request->updateCustomerPaymentProfile($customerProfileId, $paymentProfileId, $paymentProfile);
if (ANet_Response_getResultCode($response) == 'Error') {
    $e = ANet_Response_getMessageCode($response) . ': ' . ANet_Response_getMessageText($response);
    throw new Exception($e);
}