$billto->setLastName("Doe");
$billto->setCompany("My company");
$billto->setAddress("123 Main St.");
$billto->setCity("Bellevue");
$billto->setState("WA");
$billto->setZip("98004");
$billto->setPhoneNumber("000-000-0000");
$billto->setfaxNumber("999-999-9999");
$billto->setCountry("USA");
// Create a new Customer Payment Profile
$paymentprofile = new AnetAPI\CustomerPaymentProfileType();
$paymentprofile->setCustomerType('individual');
$paymentprofile->setBillTo($billto);
$paymentprofile->setPayment($paymentCreditCard);
$paymentprofile->setTaxId("level2");
$paymentprofiles[] = $paymentprofile;
// Submit a CreateCustomerPaymentProfileRequest to create a new Customer Payment Profile
$paymentprofilerequest = new AnetAPI\CreateCustomerPaymentProfileRequest();
$paymentprofilerequest->setMerchantAuthentication($merchantAuthentication);
$paymentprofilerequest->setCustomerProfileId($getcustomerprofileid);
$paymentprofilerequest->setPaymentProfile($paymentprofile);
$paymentprofilerequest->setValidationMode("liveMode");
$controller = new AnetController\CreateCustomerPaymentProfileController($request);
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
if ($response != null && $response->getMessages()->getResultCode() == "Ok") {
    echo "Create Customer Payment Profile SUCCESS: " . "\n";
    $retrievedcustomerpaymentprofile = $response->getCustomerPaymentProfileId();
} else {
    echo "Create Customer Payment Profile: ERROR Invalid response\n";
    echo "Response : " . $response->getMessages()->getMessage()[0]->getCode() . "  " . $response->getMessages()->getMessage()[0]->getText() . "\n";
}
 public function individual_transaction_by_card()
 {
     // pr($this->request->data); exit;
     // PROCESS PAYMENT
     // Common setup for API credentials
     $loggedUser = $this->Auth->user();
     $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
     // $merchantAuthentication->setName("95x9PuD6b2"); // testing mode
     $merchantAuthentication->setName("7zKH4b45");
     //42UHbr9Qa9B live mode
     //   $merchantAuthentication->setTransactionKey("547z56Vcbs3Nz9R9");  // testing mode
     $merchantAuthentication->setTransactionKey("738QpWvHH4vS59vY");
     // live mode 7UBSq68ncs65p8QX
     $refId = 'ref' . time();
     // Create the payment data for a credit card
     $creditCard = new AnetAPI\CreditCardType();
     $msg = '<ul>';
     //$this->request->data['Transaction']['id'] = $cid;
     $card = $this->request->data['Transaction'];
     $cid = $this->request->data['Transaction']['package_customer_id'];
     //      pr($card);
     //        exit;
     $creditCard->setCardNumber($card['card_no']);
     $exp_date = $card['exp_date']['month'] . '-' . $card['exp_date']['year'];
     $this->request->data['Transaction']['exp_date'] = $exp_date;
     // pr($this->request->data); exit;
     // echo $exp_date; exit;
     $creditCard->setExpirationDate($exp_date);
     //    $creditCard->setCardNumber("4117733943147221"); // live
     // $creditCard->setExpirationDate("07-2019"); //live
     $creditCard->setcardCode($card['cvv_code']);
     //live
     $paymentOne = new AnetAPI\PaymentType();
     $paymentOne->setCreditCard($creditCard);
     //    Bill To
     $billto = new AnetAPI\CustomerAddressType();
     $billto->setFirstName($card['fname']);
     $billto->setLastName($card['lname']);
     $billto->setCompany($card['company']);
     //$billto->setAddress("14 Main Street");
     $billto->setAddress($card['address']);
     $billto->setCity($card['city']);
     $billto->setState($card['state']);
     $billto->setZip($card['zip_code']);
     $billto->setCountry($card['country']);
     $billto->setphoneNumber($card['phone']);
     $billto->setfaxNumber($card['fax']);
     $paymentprofile = new AnetAPI\CustomerPaymentProfileType();
     $paymentprofile->setCustomerType('individual');
     $paymentprofile->setBillTo($billto);
     $customerProfile = new AnetAPI\CreateCustomerPaymentProfileRequest();
     $customerProfile->setPaymentProfile($paymentprofile);
     $transactionRequestType = new AnetAPI\TransactionRequestType();
     $transactionRequestType->setTransactionType("authCaptureTransaction");
     $transactionRequestType->setAmount($card['payable_amount']);
     // to do set amount from form
     $transactionRequestType->setPayment($paymentOne);
     $request = new AnetAPI\CreateTransactionRequest();
     $request->setMerchantAuthentication($merchantAuthentication);
     $request->setRefId($refId);
     $request->setTransactionRequest($transactionRequestType);
     $controller = new AnetController\CreateTransactionController($request);
     // $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
     $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);
     $this->loadModel('Transaction');
     $this->loadModel('Track');
     $this->loadModel('Ticket');
     $this->request->data['Transaction']['error_msg'] = '';
     $this->request->data['Transaction']['status'] = '';
     $this->request->data['Transaction']['trx_id'] = '';
     $this->request->data['Transaction']['auth_code'] = '';
     $amount = $this->request->data['Transaction']['payable_amount'];
     $alert = '<div class="alert alert-success"> ';
     if ($response != null) {
         $tresponse = $response->getTransactionResponse();
         if ($tresponse != null && $tresponse->getResponseCode() == "1") {
             $this->request->data['Transaction']['trx_id'] = $tresponse->getTransId();
             $this->request->data['Transaction']['auth_code'] = $tresponse->getAuthCode();
             $this->request->data['Transaction']['status'] = 'success';
             $id = $this->request->data['Transaction']['id'];
             $this->request->data['Transaction']['transaction_id'] = $id;
             //  pr($this->request->data['Transaction']); exit;
             unset($this->request->data['Transaction']['id']);
             //creatre transaction History
             $this->Transaction->save($this->request->data['Transaction']);
             unset($this->request->data['Transaction']['transaction_id']);
             $status = 'close';
             // check due amount
             $due = $this->getDue($id);
             if ($due > 0) {
                 $status = 'open';
             }
             unset($this->request->data['Transaction']['payable_amount']);
             $this->Transaction->id = $id;
             $this->Transaction->saveField("status", $status);
             $msg .= '<li> Transaction   successfull</li>';
             $tdata['Ticket'] = array('content' => "Transaction successfull <br> <b>Amount : </b>{$amount} <br> <b> payment Mode: </b> Card");
             $tickect = $this->Ticket->save($tdata);
             // Data save in Ticket
             $trackData['Track'] = array('package_customer_id' => $cid, 'ticket_id' => $tickect['Ticket']['id'], 'status' => 'closed', 'forwarded_by' => $loggedUser['id']);
             $this->Track->save($trackData);
         } else {
             $alert = '<div class="alert alert-error"> ';
             $tresponse = $response->getTransactionResponse();
             $message = $response->getMessages();
             $message = $message->getMessage();
             $message = $message[0];
             $errorCode = $message->getCode();
             if ($errorCode == 'E00003') {
                 $errorMsg = 'Invalid Card number. Check Card Number, remove space or any special carachter inserted by mistkae <br> <b> Error Code: ' . $errorCode . '</b>';
             } else {
                 if ($errorCode == 'E00007') {
                     $errorMsg = 'Transaction failed due to Marchant Account credential changed. Please contact with administrator <br> <b> Error Code: ' . $errorCode . '</b>';
                 } else {
                     $errors = $tresponse->getErrors();
                     $errors = $errors[0];
                     $errorMsg = $errors->getErrorText() . '<br> <b> Error Code: ' . $errorCode . '</b>';
                 }
             }
             $msg .= '<li>' . $errorMsg . ' </li>';
             $tdata['Ticket'] = array('content' => $errorMsg . "<br> <b>Amount</b> : {$amount} <br> <b> payment Mode: </b> Card");
             $tickect = $this->Ticket->save($tdata);
             // Data save in Ticket
             $trackData['Track'] = array('package_customer_id' => $cid, 'ticket_id' => $tickect['Ticket']['id'], 'status' => 'closed', 'forwarded_by' => $loggedUser['id']);
             $this->Track->save($trackData);
         }
     } else {
         $alert = '<div class="alert alert-error"> ';
         $msg .= '<li> Transaction failed due to Marchant Account credential changed. Please contact with administrator</li>';
         $tdata['Ticket'] = array('content' => "Transaction failed due to Marchant Account credential changed. Please contact with administrator <br> <b> Amount : </b> {$amount} <br> <b> payment Mode: </b> Card");
         $tickect = $this->Ticket->save($tdata);
         // Data save in Ticket
         $trackData['Track'] = array('package_customer_id' => $cid, 'ticket_id' => $tickect['Ticket']['id'], 'status' => 'closed', 'forwarded_by' => $loggedUser['id']);
         $this->Track->save($trackData);
     }
     $transactionMsg = $alert . '
     <button type="button" class="close" data-dismiss="alert">&times;</button>
     <strong>' . $msg . '</strong>
 </div>';
     $this->Session->setFlash($transactionMsg);
     return $this->redirect($this->referer());
     //$this->set(compact('msg'));
 }
$billto = new AnetAPI\CustomerAddressType();
$billto->setFirstName("Mary");
$billto->setLastName("Smith");
$billto->setCompany("Tennis Shirts Are Us");
$billto->setAddress("588 Willis Court");
$billto->setCity("Pecan Springs");
$billto->setState("TX");
$billto->setZip("44628");
$billto->setCountry("USA");
// Create additional payment data and add a new credit card
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber("4007000000027");
$creditCard->setExpirationDate("2038-12");
$paymentTwo = new AnetAPI\PaymentType();
$paymentTwo->setCreditCard($creditCard);
$request = new AnetAPI\CreateCustomerPaymentProfileRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId($refId);
$request->setCustomerProfileId($customerProfileId);
$paymentprofile2 = new AnetAPI\CustomerPaymentProfileType();
$paymentprofile2->setCustomerType('business');
$paymentprofile2->setBillTo($billto);
$paymentprofile2->setPayment($paymentTwo);
$paymentprofiles2[] = $paymentprofile2;
$request->setPaymentProfile($paymentprofile2);
$controller = new AnetController\CreateCustomerPaymentProfileController($request);
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
if ($response != null && $response->getMessages()->getResultCode() == "Ok") {
    echo "CreateCustomerPaymentProfileRequest SUCCESS: PROFILE ID : " . $response->getCustomerPaymentProfileId() . "\n";
    $customerProfileId = $response->getCustomerPaymentProfileId();
} else {