예제 #1
0
$paypal = new Paypal();
$cart = new Cart(intval($cookie->id_cart));
$address = new Address(intval($cart->id_address_invoice));
$country = new Country(intval($address->id_country));
$state = NULL;
if ($address->id_state) {
    $state = new State(intval($address->id_state));
}
$customer = new Customer(intval($cart->id_customer));
$business = Configuration::get('PAYPAL_BUSINESS');
$header = Configuration::get('PAYPAL_HEADER');
$currency_order = new Currency(intval($cart->id_currency));
$currency_module = $paypal->getCurrency();
if (!Validate::isEmail($business)) {
    die($paypal->getL('Paypal error: (invalid or undefined business account email)'));
}
if (!Validate::isLoadedObject($address) or !Validate::isLoadedObject($customer) or !Validate::isLoadedObject($currency_module)) {
    die($paypal->getL('Paypal error: (invalid address or customer)'));
}
// check currency of payment
if ($currency_order->id != $currency_module->id) {
    $cookie->id_currency = $currency_module->id;
    $cart->id_currency = $currency_module->id;
    $cart->update();
}
$smarty->assign(array('redirect_text' => $paypal->getL('Please wait, redirecting to Paypal... Thanks.'), 'cancel_text' => $paypal->getL('Cancel'), 'cart_text' => $paypal->getL('My cart'), 'return_text' => $paypal->getL('Return to shop'), 'paypal_url' => $paypal->getPaypalUrl(), 'address' => $address, 'country' => $country, 'state' => $state, 'amount' => floatval($cart->getOrderTotal(true, 4)), 'customer' => $customer, 'total' => floatval($cart->getOrderTotal(true, 3)), 'shipping' => Tools::ps_round(floatval($cart->getOrderShippingCost()) + floatval($cart->getOrderTotal(true, 6)), 2), 'discount' => $cart->getOrderTotal(true, 2), 'business' => $business, 'currency_module' => $currency_module, 'cart_id' => intval($cart->id), 'products' => $cart->getProducts(), 'paypal_id' => intval($paypal->id), 'header' => $header, 'url' => Tools::getHttpHost(true, true) . __PS_BASE_URI__));
if (is_file(_PS_THEME_DIR_ . 'modules/paypal/redirect.tpl')) {
    $smarty->display(_PS_THEME_DIR_ . 'modules/' . $paypal->name . '/redirect.tpl');
} else {
    $smarty->display(_PS_MODULE_DIR_ . $paypal->name . '/redirect.tpl');
}
 function paypal_set_ec($order_no = '')
 {
     //pr($this->data);
     if (!empty($this->data)) {
         $user_id = $this->Session->read('Auth.User.id');
         if ($this->data['payment_option'] == 'paypal') {
             $order_details = $this->OrderDetail->find('all', array('conditions' => array('order_no' => $order_no, 'is_paid' => '0')));
             if (empty($order_details)) {
                 $this->redirect('/');
             } else {
                 $total = 0;
                 foreach ($order_details as $order_detail) {
                     $total = $total + $order_detail['OrderDetail']['total_amount'];
                 }
                 //pr($order_details);exit;
             }
             /*$save_order = array();
               $save_order['Payment']['user_id'] = $user_id;
               $save_order['Payment']['order_no'] = $order_no;
               $save_order['Payment']['amount'] = $total;
               $save_order['Payment']['method'] = '0';
               $save_order['Payment']['shipping_amount'] = '0';
               $save_order['Payment']['token'] = '0';
               pr($this->data);exit;*/
             $min_order_price = $this->Orderprices->find('first');
             $ShippingPrice = $this->ShippingPrice->find('first');
             if ($min_order_price['Orderprices']['price'] >= $total) {
                 $total1 = $total + $ShippingPrice['ShippingPrice']['price'];
                 $shipping = 'ship';
                 $shipping_amount = $ShippingPrice['ShippingPrice']['price'];
             } else {
                 $total1 = $total;
                 $shipping_amount = 0;
             }
             //build nvp string
             //use your own logic to get and set each variable
             $returnURL = Router::url(array('controller' => 'purchases', 'action' => 'paypal_return'), true);
             $cancelURL = Router::url(array('controller' => 'purchases', 'action' => 'paypal_cancel'), true);
             $nvpStr = "RETURNURL={$returnURL}&CANCELURL={$cancelURL}";
             $nvpStr .= "&PAYMENTREQUEST_0_CURRENCYCODE=MYR";
             $nvpStr .= "&PAYMENTREQUEST_0_AMT=" . number_format($total1, 2, '.', '');
             $nvpStr .= "&PAYMENTREQUEST_0_ITEMAMT=" . number_format($total, 2, '.', '');
             $nvpStr .= "&AYMENTREQUEST_0_PAYMENTACTION=sale";
             $nvpStr .= "&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital";
             $nvpStr .= "&L_PAYMENTREQUEST_0_NAME0=Harimau LTD \n Order#" . $order_no;
             $nvpStr .= "&L_PAYMENTREQUEST_0_QTY0=1";
             $nvpStr .= "&L_PAYMENTREQUEST_0_AMT0=" . number_format($total, 2, '.', '');
             if (isset($shipping) and $shipping == 'ship') {
                 $nvpStr .= "&PAYMENTREQUEST_0_SHIPPINGAMT=" . number_format($ShippingPrice['ShippingPrice']['price'], 2, '.', '');
             }
             /*$nvpStr=
                "RETURNURL=$returnURL&CANCELURL=$cancelURL"
               ."&PAYMENTREQUEST_0_CURRENCYCODE=MYR"           
               ."&PAYMENTREQUEST_0_AMT=15"
               ."&PAYMENTREQUEST_0_ITEMAMT=10"
               ."&AYMENTREQUEST_0_PAYMENTACTION=Sale"
               ."&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital"
               ."&L_PAYMENTREQUEST_0_NAME0=test"
               ."&L_PAYMENTREQUEST_0_QTY0=1"
               ."&L_PAYMENTREQUEST_0_AMT0=10"          
               ."&PAYMENTREQUEST_0_SHIPPINGAMT=5.00"
               ;     */
             //do paypal setECCheckout
             App::import('Model', 'Paypal');
             $paypal = new Paypal();
             if ($paypal->setExpressCheckout($nvpStr)) {
                 $save_order = array();
                 $payment_order = $this->Payment->findByOrderNo($order_no);
                 if (empty($payment_order)) {
                     $save_order['Payment']['user_id'] = $user_id;
                     $save_order['Payment']['order_no'] = $order_no;
                     $save_order['Payment']['amount'] = number_format($total, 2, '.', '');
                     $save_order['Payment']['method'] = '0';
                     $save_order['Payment']['shipping_amount'] = $shipping_amount;
                     $save_order['Payment']['token'] = $paypal->token;
                     $this->Payment->save($save_order);
                 }
                 $result = $paypal->getPaypalUrl($paypal->token);
             } else {
                 $this->log($paypal->errors);
                 $result = false;
             }
             pr($paypal->errors);
             if (false !== $result) {
                 $this->redirect($result);
             } else {
                 $this->Session->setFlash(__('Error while connecting to PayPal, Please try again', true));
             }
             exit;
         } else {
             if ($this->data['payment_option'] == 'molpay') {
                 $order_details = $this->OrderDetail->find('all', array('conditions' => array('order_no' => $order_no, 'is_paid' => '0')));
                 $merchant = 2;
                 $auth_key = 'QZRSggdz';
                 $total = 0;
                 foreach ($order_details as $order_detail) {
                     $total = $total + $order_detail['OrderDetail']['total_amount'];
                 }
                 $name = $order_details['0']['OrderDetail']['name'];
                 $email = $order_details['0']['User']['username'];
                 $mobile = '';
                 //substr($order_details['0']['OrderDetail']['phone'],1,20);
                 $str = strtoupper(md5($merchant . '' . $auth_key . '' . $order_no . '' . $total . '' . $name . '' . $email . '' . $mobile));
                 $url = "http://payment.theviko.com/api/merchant/{$merchant}/{$str}/{$total}/{$order_no}/{$name}/{$email}/{$mobile}";
                 $this->redirect($url);
             } else {
                 $this->Session->setFlash('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> Invalid order details<button data-dismiss="alert" class="close" type="button">×</button> </div>');
                 $this->redirect('/store/makepayment/' . $order_no);
             }
         }
     }
 }
예제 #3
0
 /**
  * Starts the transaction and gets the token. Afterwards it's passed off to the final transaction method.
  * @param 
  */
 public function paypal_set_ec()
 {
     if ($this->request->is('post')) {
         //Abort if cancel button was pressed
         if (isset($this->request->data['cancel'])) {
             //Pass the user along to an action that will clear the account and the upload
             $this->redirect(array('controller' => 'users', 'action' => 'clear_user_data', $this->request->data['Upload']['user_id']));
             break;
         }
         //Check to make sure that the total codes haven't already been added to this file
         $upload = $this->Upload->read(null, $this->request->data['Upload']['id']);
         if (intval($upload['Upload']['total_codes']) > 0) {
             if (intval($upload['Upload']['total_codes']) == count($upload['Code'])) {
                 $this->Session->setFlash(__('Error! No more codes can be added to this upload. Please re-upload the file.', true), 'message_fail');
                 $this->render('paypal_back_to_add');
                 return;
             }
         }
         //do paypal setECCheckout
         App::import('Model', 'Paypal');
         $paypal = new Paypal();
         $codePrice = $this->Upload->Code->getPrice($this->request->data['Upload']['total_codes']);
         $itemName = $this->Upload->Code->getItemName($this->request->data['Upload']['total_codes']);
         $nvpStr = $paypal->buildNVPString($codePrice, $itemName, $this->request->data['Upload']['user_id'], $this->request->data['Upload']['id'], $this->request->data['Upload']['total_codes']);
         if ($paypal->setExpressCheckout($nvpStr)) {
             $result = $paypal->getPaypalUrl($paypal->token);
         } else {
             $this->log($paypal->errors);
             $result = false;
         }
         //debug($this->request);
         if (false !== $result) {
             //The result should look like the following
             //https://www.sandbox.paypal.com/incontext?token=EC-09N44269CG053064W
             $this->redirect($result);
         } else {
             $this->Session->setFlash(__('Error while connecting to PayPal, Please try again', true));
         }
     }
     $payment_options = $this->Upload->Code->getPaymentOptions();
     //Get the options array for the select list
     $this->set(compact('payment_options'));
 }