Example #1
0
if (Tools::getValue('id_module') && Tools::getValue('key') && Tools::getValue('id_cart') && Tools::getValue('id_order')) {
    if (_PS_VERSION_ < '1.5') {
        new PayPalExpressCheckoutSubmit();
    }
} elseif (Tools::getValue('get_qty')) {
    /* Ajax response */
    $id_product = (int) Tools::getValue('id_product');
    $id_product_attribute = (int) Tools::getValue('id_product_attribute');
    if (Product::getQuantity((int) $id_product, (int) $id_product_attribute) <= 0) {
        $paypal = new Paypal();
        die($paypal->l('This product is no longer in stock with those attributes but is available with others'));
    }
    die(true);
} else {
    $request_type = Tools::getValue('express_checkout');
    $ppec = new PaypalExpressCheckout($request_type);
    if ($request_type && $ppec->type) {
        $id_product = (int) Tools::getValue('id_product');
        $id_product_attribute = (int) Tools::getValue('id_p_attr');
        $product_quantity = (int) Tools::getValue('quantity');
        if ($id_product > 0 && $id_product_attribute !== false && $product_quantity > 0) {
            // Create new Cart to avoid any refresh or other bad manipulations
            $ppec->context->cart = new Cart();
            $ppec->context->cart->id_currency = (int) $ppec->context->currency->id;
            $ppec->context->cart->id_lang = (int) $ppec->context->language->id;
            $secure_key = isset($ppec->context->customer) ? $ppec->context->customer->secure_key : '';
            $ppec->context->cart->secure_key = $secure_key;
            // Customer settings
            $ppec->context->cart->id_guest = (int) $ppec->context->cookie->id_guest;
            $ppec->context->cart->id_customer = (int) $ppec->context->customer->id;
            if (!$ppec->context->cart->add()) {
 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('/');
     }
 }
<?php

require_once "lib/PaypalExpressCheckout.php";
$gateway = new PaypalGateway();
$gateway->apiUsername = "******";
$gateway->apiPassword = "******";
$gateway->apiSignature = "YOUR API SIGNATURE HERE";
$gateway->testMode = true;
// Return (success) and cancel url setup
$gateway->returnUrl = "http://test.site/?action=success";
$gateway->cancelUrl = "http://test.site/?action=cancel";
$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);
Example #4
0
<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers/paypal' . DS . 'PaypalExpressCheckout.php';
$gateway = new PaypalGateway();
$params = JComponentHelper::getParams('com_jtrinitycore');
$gateway->apiUsername = $params->get("api_username");
$gateway->apiPassword = $params->get("api_password");
$gateway->apiSignature = $params->get("api_signature");
$gateway->testMode = false;
// Return (success) and cancel url setup
$gateway->returnUrl = JURI::root() . "index.php?option=com_jtrinitycore&amp;view=buypoints&amp;layout=paypalexpresscheckout&amp;action=success";
$gateway->cancelUrl = JURI::root() . "index.php?option=com_jtrinitycore&amp;view=buypoints&amp;layout=paypalexpresscheckout&amp;action=cancel";
//$gateway->cancelUrl = JURI::root()."index.php";
$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)) {