public function checkout()
 {
     $this->layout = 'front';
     $userId = $this->Auth->user('id');
     //pr($this->request->data);die;
     if ($this->request->is('post')) {
         App::import('Vendor', 'checkout/PaypalExpressCheckout');
         $gateway = new PaypalGateway();
         $gateway->apiUsername = Configure::read('Settings.PAYPAL_API_USERNAME');
         //API_USERNAME;
         $gateway->apiPassword = Configure::read('Settings.PAYPAL_API_PASSWORD');
         //API_PASSWORD;
         $gateway->apiSignature = Configure::read('Settings.PAYPAL_API_SIGNATURE');
         //API_SIGNATURE;
         $gateway->testMode = true;
         // Return (success) and cancel url setup
         $gateway->returnUrl = Configure::read('Settings.AUTHORIZENET_ENABLED');
         $gateway->cancelUrl = Configure::read('Settings.AUTHORIZENET_ENABLED');
         //create instance of paypal
         $paypal = new PaypalExpressCheckout($gateway);
         $shipping = false;
         $userId = $this->Auth->user('id');
         //$requestUrl = $this->params->params['pass'][0];
         if (!isset($resultData)) {
             $resultData = array();
         }
         if (isset($_GET['action'])) {
             $action = $_GET['action'];
         }
         $transaction = array();
         if ($userId) {
             // $PriceArr=$this->ProductPrice($userId);
             //pr($PriceArr);die;
             $_SESSION["userId"] = $userId;
             $_SESSION["price"] = $this->request->data['Product']['amount'];
             // $_SESSION["product_ids"] =$PriceArr['product_ids'];
             // $_SESSION["pet_ids"] =$PriceArr['pet_ids'];
             $amount = $this->request->data['Product']['amount'];
             $paypal->doExpressCheckout($amount, 'Paypal Express', '', 'USD', $shipping, $resultData);
         }
         if (isset($_GET['action']) && base64_decode($_GET['action']) == "success") {
             if ($transaction = $paypal->doPayment($_GET['token'], $_GET['PayerID'], $resultData)) {
                 //pr($transaction); die;
                 // insert data in payment table
                 $data = array();
                 $data['transaction_id'] = $transaction['TRANSACTIONID'];
                 $data['status'] = $transaction['ACK'];
                 $data['amount'] = $transaction['AMT'];
                 $data['other_info'] = json_encode($transaction);
                 $data['product_id'] = $_SESSION["product_ids"];
                 $data['user_id'] = $_SESSION["userId"];
                 $this->loadModel('Payment');
                 $retPayment = $this->Payment->save($data);
                 //save expire date in product table
                 $productIds = explode(',', $_SESSION["product_ids"]);
                 $petIds = explode(',', $_SESSION["pet_ids"]);
                 $this->loadModel('Product');
                 $productArr = array();
                 $productArr['id'] = $retPayment['Payment']['product_id'];
                 $dt = date("Y-m-d");
                 $expiredate = date("Y-m-d", strtotime("{$dt} +3 day"));
                 // pr($this->Product);die;
                 //$this->Product->save($productArr);
                 $Pro_Arr = array();
                 $giftArr = array();
                 foreach ($productIds as $key => $value) {
                     $Pro_Arr[$key]['id'] = $value;
                     $Pro_Arr[$key]['expire_num'] = 3;
                     $Pro_Arr[$key]['expire_type'] = 'days';
                     $Pro_Arr[$key]['expire_date'] = $expiredate;
                     $giftArr[$key]['product_id'] = $value;
                     $giftArr[$key]['user_id'] = $_SESSION["userId"];
                     $giftArr[$key]['pet_id'] = $petIds[$key];
                 }
                 $this->loadModel('UserGift');
                 $this->UserGift->saveAll($giftArr);
                 if ($this->Product->saveAll($Pro_Arr)) {
                     $this->loadModel('Cart');
                     $this->loadModel('Pet');
                     $this->Cart->deleteAll(array('Cart.user_id' => $userId));
                 }
                 $this->Session->setFlash(__('your have successfully purchase the product !'), 'success_flash');
                 $petSlug = $this->Pet->find('first', array('fields' => 'Pet.slug', 'conditions' => array('Pet.id' => $petIds[0])));
                 $this->redirect('/p/' . $petSlug['Pet']['slug']);
                 //$this->set('data',$this->Payment->find('first',array('conditions'=>array('Payment.id'=>$retPayment['Payment']['id']),'contain'=>array('User'))));
             } else {
                 //unset($_SESSION["userId"]);
                 unset($_SESSION["product_ids"]);
                 unset($_SESSION["price"]);
                 $this->redirect('/pages/products/');
             }
         } else {
             echo 'error';
         }
     } else {
         $this->redirect('/');
     }
 }
$paypal = new PaypalExpressCheckout($gateway);
$shipping = false;
if (!isset($resultData)) {
    $resultData = array();
}
if (isset($_GET['action'])) {
    $action = $_GET['action'];
}
switch ($_GET['action']) {
    case "":
        // Index page, here you should be redirected to Paypal
        $paypal->doExpressCheckout(123.45, 'Test service', 'inv123', 'USD', $shipping, $resultData);
        break;
    case "success":
        // Paypal says everything's fine, do the charge (user redirected to $gateway->returnUrl)
        if ($transaction = $paypal->doPayment($_GET['token'], $_GET['PayerID'], $resultData)) {
            echo "Success! Transaction ID: " . $transaction['TRANSACTIONID'];
        } else {
            echo "Debugging what went wrong: ";
            print_r($resultData);
        }
        break;
    case "refund":
        $transactionId = '9SU82364E9556505C';
        if ($paypal->doRefund($transactionId, 'inv123', false, 0, 'USD', '', $resultData)) {
            echo 'Refunded: ' . $resultData['GROSSREFUNDAMT'];
        } else {
            echo "Debugging what went wrong: ";
            print_r($resultData);
        }
        break;
$paypal = new PaypalExpressCheckout($gateway);
$shipping = false;
switch (JRequest::getVar('action')) {
    case "":
        // Index page, here you should be redirected to Paypal
        JLog::add('doExpressCheckout - Begin', JLog::INFO, 'paypal');
        $paypal->doExpressCheckout(JRequest::getVar('points'), 'Donationshop get points', '', $params->get("currency"), $shipping, $resultData);
        //JLog::add('doExpressCheckout - Result Data='.print_r($resultData),JLog::INFO,'paypal');
        JLog::add('doExpressCheckout - End', JLog::INFO, 'paypal');
        break;
    case "success":
        // Paypal says everything's fine, do the charge (user redirected to $gateway->returnUrl)
        $user = JFactory::getUser();
        $model = $this->getModel();
        //if ($result = $paypal->doPayment($_GET['token'], $_GET['PayerID'], $resultData)) {
        if ($result = $paypal->doPayment(JRequest::getVar('token'), JRequest::getVar('PayerID'), $resultData)) {
            //echo "Success! Transaction ID: ".$result['TRANSACTIONID'];
            foreach ($result as $c => $v) {
                JLog::add('doPayment - ' . $c . '=' . $v, JLog::INFO, 'paypal');
            }
            JLog::add('doPayment - End', JLog::INFO, 'paypal');
            //print_r($resultData);
            // Add points to the user
            $amount = $result['AMT'];
            $paypal_txn_id = $result['TRANSACTIONID'];
            $model->addUserPoints($user->id, $amount);
            // Add transaction ID to the database with status COMPLETED
            $model->insertDonation($user->id, $amount, $paypal_txn_id, true);
            // Send notification mail
            $mail = JMail::getInstance();
            $mail->setSubject(JText::_('COM_JTRINITYCORE_PAYPAL_EMAIL_NOTIFICATION_SUBJECT'));