Example #1
0
 /**
  *   Get the form variables for the purchase button.
  *
  *   @uses   PaymentGw::_Supports()
  *   @uses   _encButton()
  *   @uses   getActionUrl()
  *   @return string      HTML for purchase button
  */
 public function CheckoutButton($cart)
 {
     global $_PP_CONF, $_USER, $_TABLES;
     if (!$this->_Supports('checkout')) {
         return '';
     }
     $cartItems = $cart->Cart();
     $cartID = $cart->CartID();
     $custom_arr = array('uid' => $_USER['uid'], 'transtype' => 'cart_upload', 'cart_id' => $cartID);
     $fields = array('cmd' => '_cart', 'upload' => '1', 'cancel_return' => PAYPAL_URL . '/index.php?view=cart', 'return' => PAYPAL_URL . '/index.php?thanks=paypal', 'rm' => '2', 'paymentaction' => 'sale', 'notify_url' => $this->ipn_url, 'currency_code' => $this->currency_code, 'custom' => str_replace('"', '\'', serialize($custom_arr)));
     $address = $cart->getAddress('shipto');
     if (!empty($address)) {
         list($fname, $lname) = explode(' ', $address['name']);
         $fields['first_name'] = htmlspecialchars($fname);
         $fields['last_name'] = htmlspecialchars($lname);
         $fields['address1'] = htmlspecialchars($address['address1']);
         $fields['address2'] = htmlspecialchars($address['address2']);
         $fields['city'] = htmlspecialchars($address['city']);
         $fields['state'] = htmlspecialchars($address['state']);
         $fields['country'] = htmlspecialchars($address['country']);
         $fields['zip'] = htmlspecialchars($address['zip']);
     }
     $i = 1;
     $total_amount = 0;
     $shipping = 0;
     $weight = 0;
     foreach ($cartItems as $cart_item_id => $item) {
         //$opt_str = '';
         list($db_item_id, $options) = explode('|', $item['item_id']);
         if (is_numeric($db_item_id)) {
             $P = new Product($db_item_id);
             $db_item_id = DB_escapeString($db_item_id);
             $oc = 0;
             if (is_array($item['options'])) {
                 $opts = explode(',', $options);
                 foreach ($opts as $optval) {
                     $opt_info = $P->getOption($optval);
                     if ($opt_info) {
                         $opt_str .= ', ' . $opt_info['value'];
                         $fields['on' . $oc . '_' . $i] = $opt_info['name'];
                         $fields['os' . $oc . '_' . $i] = $opt_info['value'];
                         $oc++;
                     }
                 }
                 //$item['descrip'] .= $opt_str;
             } else {
                 $opts = array();
             }
             $fields['amount_' . $i] = $P->getPrice($opts, $item['quantity']);
             if ($P->taxable == 0) {
                 $fields['tax_' . $i] = '0.00';
             }
         } else {
             // Plugin item
             $fields['amount_' . $i] = $item['price'];
         }
         //$fields['item_number_' . $i] = htmlspecialchars($item['item_id']);
         $fields['item_number_' . $i] = (int) $cart_item_id;
         $fields['item_name_' . $i] = htmlspecialchars($item['descrip']);
         $total_amount += $item['price'];
         if (is_array($item['extras']['custom'])) {
             foreach ($item['extras']['custom'] as $id => $val) {
                 $fields['on' . $oc . '_' . $i] = $P->getCustom($id);
                 $fields['os' . $oc . '_' . $i] = $val;
                 $oc++;
             }
         }
         $fields['quantity_' . $i] = $item['quantity'];
         if (isset($item['shipping'])) {
             $fields['shipping_' . $i] = $item['shipping'];
             $shipping += $item['shipping'];
         }
         if (isset($item['weight']) && $item['weight'] > 0) {
             $weight += $item['weight'];
         }
         if (isset($item['tax'])) {
             $fields['tax_' . $i] = $item['tax'];
         } elseif (isset($item['options']['tax'])) {
             $fields['tax_' . $i] = $item['options']['tax'];
         }
         $i++;
     }
     if ($shipping > 0) {
         $total_amount += $shipping;
     }
     if ($weight > 0) {
         $fields['weight_cart'] = $weight;
         $fields['weight_unit'] = $_PP_CONF['weight_unit'] == 'kgs' ? 'kgs' : 'lbs';
     }
     // Set the business e-mail address based on the total puchase amount
     // There must be an address configured; if not then this gateway can't
     // be used for this purchase
     $this->setReceiver($total_amount);
     $fields['business'] = $this->receiver_email;
     if (empty($fields['business'])) {
         return '';
     }
     $gatewayVars = array();
     $enc_btn = '';
     if ($this->config['encrypt']) {
         $enc_btn = self::_encButton($fields);
         if (!empty($enc_btn)) {
             $gatewayVars[] = '<input type="hidden" name="cmd" value="_s-xclick" />';
             $gatewayVars[] = '<input type="hidden" name="encrypted" ' . 'value="' . $enc_btn . '" />';
         }
     }
     if (empty($enc_btn)) {
         // If we didn't get an encrypted button, set the plaintext vars
         foreach ($fields as $name => $value) {
             $gatewayVars[] = '<input type="hidden" name="' . $name . '" value="' . $value . '" />';
         }
     }
     $gateway_vars = implode("\n", $gatewayVars);
     $T = new Template(PAYPAL_PI_PATH . '/templates/buttons/' . $this->gw_name);
     $T->set_file(array('btn' => 'btn_checkout.thtml'));
     $T->set_var('paypal_url', $this->getActionUrl());
     $T->set_var('gateway_vars', $gateway_vars);
     $retval = $T->parse('', 'btn');
     return $retval;
 }