예제 #1
0
파일: shop.php 프로젝트: tareqy/Caracal
 /**
  * Handle drawing checkout form
  *
  * @param array $tag_params
  * @param array $children
  */
 public function tag_CheckoutForm($tag_params, $children)
 {
     $account_information = array();
     $shipping_information = array();
     $billing_information = array();
     $payment_method = null;
     $stage = isset($_REQUEST['stage']) ? fix_chars($_REQUEST['stage']) : null;
     $recurring = isset($_SESSION['recurring_plan']) && !empty($_SESSION['recurring_plan']);
     // decide whether to include shipping and account information
     if (isset($tag_params['include_shipping'])) {
         $include_shipping = fix_id($tag_params['include_shipping']) == 1;
     } else {
         $include_shipping = true;
     }
     $bad_fields = array();
     $info_available = false;
     // grab user information
     if (!is_null($stage)) {
         // get payment method
         $payment_method = $this->getPaymentMethod($tag_params);
         if (is_null($payment_method)) {
             throw new PaymentMethodError('No payment method selected!');
         }
         // get billing information
         $billing_information = $this->getBillingInformation($payment_method);
         $billing_required = array('billing_full_name', 'billing_card_type', 'billing_credit_card', 'billing_expire_month', 'billing_expire_year', 'billing_cvv');
         $bad_fields = $this->checkFields($billing_information, $billing_required, $bad_fields);
         // get shipping information
         if ($include_shipping && $stage == 'set_info') {
             $shipping_information = $this->getShippingInformation();
             $shipping_required = array('name', 'email', 'street', 'city', 'zip', 'country');
             $bad_fields = $this->checkFields($shipping_information, $shipping_required, $bad_fields);
         }
     }
     $info_available = count($bad_fields) == 0 && !is_null($payment_method);
     if ($info_available) {
         $address_manager = ShopDeliveryAddressManager::getInstance();
         $currency_manager = ShopCurrenciesManager::getInstance();
         // get fields for payment method
         $return_url = url_Make('checkout_completed', 'shop', array('payment_method', $payment_method->get_name()));
         $cancel_url = url_Make('checkout_canceled', 'shop', array('payment_method', $payment_method->get_name()));
         // get currency info
         $currency = $this->settings['default_currency'];
         $currency_item = $currency_manager->getSingleItem(array('id'), array('currency' => $currency));
         if (is_object($currency_item)) {
             $transaction_data['currency'] = $currency_item->id;
         }
         // get buyer
         $buyer = $this->getUserAccount();
         if ($include_shipping) {
             $address = $this->getAddress($buyer, $shipping_information);
         } else {
             $address = null;
         }
         // update transaction
         $transaction_type = $recurring ? TransactionType::SUBSCRIPTION : TransactionType::SHOPPING_CART;
         $summary = $this->updateTransaction($transaction_type, $payment_method, '', $buyer, $address);
         // emit signal and return if handled
         if ($stage == 'set_info') {
             Events::trigger('shop', 'before-checkout', $payment_method->get_name(), $return_url, $cancel_url);
             foreach ($result_list as $result) {
                 if ($result) {
                     $this->showCheckoutRedirect();
                     return;
                 }
             }
         }
         // create new payment
         if ($recurring) {
             // recurring payment
             $checkout_fields = $payment_method->new_recurring_payment($_SESSION['recurring_plan'], $billing_information, $return_url, $cancel_url);
         } else {
             // regular payment
             $checkout_fields = $payment_method->new_payment($transaction_data, $billing_information, $summary['items_for_checkout'], $return_url, $cancel_url);
         }
         // load template
         $template = $this->loadTemplate($tag_params, 'checkout_form.xml');
         $template->registerTagHandler('cms:checkout_items', $this, 'tag_CheckoutItems');
         $template->registerTagHandler('cms:delivery_methods', $this, 'tag_DeliveryMethodsList');
         // parse template
         $params = array('checkout_url' => $payment_method->get_url(), 'checkout_fields' => $checkout_fields, 'checkout_name' => $payment_method->get_title(), 'currency' => $this->getDefaultCurrency(), 'recurring' => $recurring, 'include_shipping' => $include_shipping);
         // for recurring plans add additional params
         if ($recurring) {
             $plans = $payment_method->get_recurring_plans();
             $plan_name = $_SESSION['recurring_plan'];
             $plan = $plans[$plan_name];
             $params['plan_name'] = $plan['name'];
             $params['plan_description'] = $this->formatRecurring(array('price' => $plan['price'], 'period' => $plan['interval_count'], 'period' => $plan['interval_count'], 'unit' => $plan['interval'], 'setup' => $plan['setup_price'], 'trial_period' => $plan['trial_count'], 'trial_unit' => $plan['trial']));
         } else {
             $params['sub-total'] = number_format($summary['total'], 2);
             $params['shipping'] = number_format($summary['shipping'], 2);
             $params['handling'] = number_format($summary['handling'], 2);
             $params['total_weight'] = number_format($summary['weight'], 2);
             $params['total'] = number_format($summary['total'] + $summary['shipping'] + $summary['handling'], 2);
         }
         $template->restoreXML();
         $template->setLocalParams($params);
         $template->parse();
     } else {
         // no information available, show form
         $template = new TemplateHandler('buyer_information.xml', $this->path . 'templates/');
         $template->setMappedModule($this->name);
         $template->registerTagHandler('cms:card_type', $this, 'tag_CardType');
         // get fixed country if set
         $fixed_country = '';
         if (isset($this->settings['fixed_country'])) {
             $fixed_country = $this->settings['fixed_country'];
         }
         $params = array('include_shipping' => $include_shipping, 'fixed_country' => $fixed_country, 'bad_fields' => $bad_fields, 'recurring' => $recurring);
         $template->restoreXML();
         $template->setLocalParams($params);
         $template->parse();
     }
 }