public function createOrderPayment($user = null, $orderInfo = null)
 {
     $total = $this->getCheckoutTotal();
     // figure out what payment method was used
     $paymentInfo = CakeSession::read('DrexCartGatewayProfile');
     if (isset($paymentInfo['id']) && is_numeric($paymentInfo['id'])) {
         // already have a payment profile
         $this->DrexCartGatewayProfile = ClassRegistry::init('DrexCart.DrexCartGatewayProfile');
         $this->DrexCartGatewayProfile->create();
         $gwCard = $this->DrexCartGatewayProfile->find('first', array('fields' => array('DrexCartGatewayProfile.*', 'DrexCartGatewayUser.*'), 'joins' => array(array('table' => 'drex_cart_gateway_users', 'alias' => 'DrexCartGatewayUser', 'type' => 'left', 'conditions' => array('DrexCartGatewayUser.id=DrexCartGatewayProfile.drex_cart_gateway_users_id'))), 'conditions' => array('DrexCartGatewayProfile.id' => $paymentInfo['id'], 'DrexCartGatewayUser.drex_cart_users_id' => $user['id'])));
         if ($gwCard) {
             $this->DrexCartGateway = ClassRegistry::init('DrexCart.DrexCartGateway');
             $this->DrexCartGateway->create();
             $gateway = $this->DrexCartGateway->getGatewayById($gwCard['DrexCartGatewayUser']['drex_cart_gateways_id']);
             if ($gateway) {
                 if ($gateway['DrexCartGateway']['type'] == 'authorize') {
                     // authorize.net for credit cards
                     $paymentModule = new AuthorizePaymentModule($gateway['DrexCartGateway']['id'], $gateway['DrexCartGateway']['wsdl_url'], $gateway['DrexCartGateway']['api_login'], $gateway['DrexCartGateway']['api_key']);
                 } else {
                     if ($gateway['DrexCartGateway']['type'] == 'paypal') {
                         // paypal
                     }
                 }
             }
             $transactionId = $paymentModule->authorizePayment($gwCard['DrexCartGatewayUser']['profile_id'], $gwCard['DrexCartGatewayProfile']['profile_id'], $total);
             return $transactionId;
         } else {
             throw new Exception('Payment profile does not exist!');
         }
     } else {
         // check for user gateway profile
         $gatewayId = $paymentInfo['drex_cart_gateways_id'];
         $this->DrexCartGateway = ClassRegistry::init('DrexCart.DrexCartGateway');
         $this->DrexCartGateway->create();
         $this->DrexCartGatewayUser = ClassRegistry::init('DrexCart.DrexCartGatewayUser');
         $this->DrexCartGatewayUser->create();
         $gateway = $this->DrexCartGateway->getGatewayById($gatewayId);
         if ($gateway) {
             if ($gateway['DrexCartGateway']['type'] == 'authorize') {
                 // authorize.net for credit cards
                 $paymentModule = new AuthorizePaymentModule($gatewayId, $gateway['DrexCartGateway']['wsdl_url'], $gateway['DrexCartGateway']['api_login'], $gateway['DrexCartGateway']['api_key']);
             } else {
                 if ($gateway['DrexCartGateway']['type'] == 'paypal') {
                     // paypal
                 }
             }
         }
         if ($gwUser = $this->DrexCartGatewayUser->find('first', array('conditions' => array('drex_cart_users_id' => $user['id'], 'drex_cart_gateways_id' => $gatewayId)))) {
             // gateway user account profile exists
             $userProfileId = $gwUser['DrexCartGatewayUser']['profile_id'];
         } else {
             if ($userProfileId = $paymentModule->createCustomer($user)) {
                 //echo 'userProfileId: '.$userProfileId;
             } else {
                 $errorMessage = '';
                 foreach ($paymentModule->errors as $error) {
                     $errorMessage .= $error . '<br />';
                 }
                 throw new Exception($errorMessage);
             }
         }
         if ($userCardProfileId = $paymentModule->addCard($userProfileId, $paymentInfo, $orderInfo)) {
             //echo '<br /> userCartProfileId: '.$userCardProfileId;
         } else {
             $errorMessage = '';
             foreach ($paymentModule->errors as $error) {
                 $errorMessage .= $error . '<br />';
             }
             throw new Exception($errorMessage);
         }
         //exit;
         $transactionId = $paymentModule->authorizePayment($userProfileId, $userCardProfileId, $total);
         return $transactionId;
     }
     /*
     	$soap->CreateCustomerProfile(new CreateCustomerPaymentProfile(new MerchantAuthenticationType('9eFfhH98Uz', '38UAqh26T7U3gc4y'),
     			null,
     			new CustomerPaymentProfileType(new PaymentType($bankAccount, $creditCard),
     					$driversLicense,
     					$taxId),
     			$validationMode));
     */
 }
 public function paymentProfilesEdit($gatewayProfileId = null)
 {
     if (!empty($this->request->data)) {
         //pr($this->request->data);
         $data = $this->request->data;
         if ($data['DrexCartAddress']['billing_zip'] && $data['DrexCartGatewayProfile']['account_number'] && $data['DrexCartGatewayProfile']['expiration'] && $data['DrexCartGatewayProfile']['code']) {
             // try to save card
             $this->DrexCartGateway = ClassRegistry::init('DrexCart.DrexCartGateway');
             $this->DrexCartGateway->create();
             $gateway = $this->DrexCartGateway->find('first', array('conditions' => array('type' => 'authorize')));
             $user = $this->userManager->getUserData();
             $payment = new AuthorizePaymentModule($gateway['DrexCartGateway']['id'], $gateway['DrexCartGateway']['wsdl_url'], $gateway['DrexCartGateway']['api_login'], $gateway['DrexCartGateway']['api_key']);
             $userProfileId = $payment->createCustomer($user['DrexCartUser']);
             $data['DrexCartGatewayProfile']['expiration'] = $data['DrexCartGatewayProfile']['expiration']['year'] . '-' . $data['DrexCartGatewayProfile']['expiration']['month'];
             $paymentProfileId = $payment->addCard($userProfileId, $data['DrexCartGatewayProfile'], $data['DrexCartAddress']);
             if ($paymentProfileId) {
                 //$this->Session->setFlash('Card added successfully!', 'default', array('class'=>'alert alert-success'));
                 $this->set('updated', true);
             } else {
                 $this->set('errors', $payment->errors);
             }
         }
     }
     $this->set('gatewayProfileId', $gatewayProfileId);
 }
 public function orderPaymentsCapture($orderPaymentsId = null)
 {
     $this->DrexCartOrderPayment = ClassRegistry::init('DrexCart.DrexCartOrderPayment');
     $this->DrexCartOrderPayment->create();
     $order_payment = $this->DrexCartOrderPayment->getPaymentsById($orderPaymentsId);
     $this->set('order_payment', $order_payment);
     // precaution
     if ($order_payment['DrexCartOrderPayment']['amount'] == $order_payment['DrexCartOrderPayment']['captured_amount']) {
         // all captured, nothing to do here
         //$this->redirect('/DrexCartAdmin/orderPayments/'.$order_payment['DrexCartOrderPayment']['drex_cart_orders_id']);
         exit;
     }
     //pr($order_payment);
     if (!empty($this->request->data)) {
         // check amount
         $amount = $this->request->data['DrexCartOrderPayment']['capture_amount'];
         // get gateway information
         $this->DrexCartOrderPayment = ClassRegistry::init('DrexCart.DrexCartOrderPayment');
         $this->DrexCartOrderPayment->create();
         $gateway = $this->DrexCartOrderPayment->getGatewayInfo($order_payment['DrexCartOrderPayment']['id']);
         if ($gateway['type'] == 'authorize') {
             $payment = new AuthorizePaymentModule($gateway['id'], $gateway['wsdl_url'], $gateway['api_login'], $gateway['api_key']);
         } else {
             // TODO other payment methods
         }
         $this->DrexCartGatewayProfile = ClassRegistry::init('DrexCart.DrexCartGatewayProfile');
         $this->DrexCartGatewayProfile->create();
         $this->DrexCartGatewayUser = ClassRegistry::init('DrexCart.DrexCartGatewayUser');
         $this->DrexCartGatewayUser->create();
         $profile = $this->DrexCartGatewayProfile->find('first', array('conditions' => array('id' => $order_payment['DrexCartOrderPayment']['drex_cart_gateway_profiles_id'])));
         $userProfile = $this->DrexCartGatewayUser->find('first', array('conditions' => array('id' => $profile['DrexCartGatewayProfile']['drex_cart_gateway_users_id'])));
         if ($tranasactionId = $payment->capturePayment($userProfile['DrexCartGatewayUser']['profile_id'], $profile['DrexCartGatewayProfile']['profile_id'], $amount, $order_payment['DrexCartOrderPayment']['transaction_id'])) {
             // capture was good
             $order_payment['DrexCartOrderPayment']['captured_amount'] = $amount;
             $order_payment['DrexCartOrderPayment']['captured_date'] = date('Y-m-d H:i:s');
             $this->DrexCartOrderPayment->id = $order_payment['DrexCartOrderPayment']['id'];
             $this->DrexCartOrderPayment->save($order_payment);
             $this->set('captured', true);
         } else {
             // capture was bad
             $this->set('captured', false);
         }
     }
     $this->set('order', $this->DrexCartOrder->getOrder($order_payment['DrexCartOrderPayment']['drex_cart_orders_id']));
     $this->DrexCartOrderTotal = ClassRegistry::init('DrexCart.DrexCartOrderTotal');
     $this->DrexCartOrderTotal->create();
     $this->set('order_totals', $this->DrexCartOrderTotal->getOrderTotals($order_payment['DrexCartOrderPayment']['drex_cart_orders_id']));
 }