Ejemplo n.º 1
0
 public function getStepView()
 {
     $out = '';
     $key = 0;
     $hasUser = !empty($this->user) ? true : false;
     if (in_array($this->step, $this->authorizedStep)) {
         // Récupération du controller en fonction de la step
         $this->controllerStep($this->step);
         // Récupération de la clé
         $key = array_search($this->step, $this->authorizedStep);
         $key++;
         // Gestion du dépassement
         $countMax = count($this->authorizedStep) - 1;
         if ($key >= $countMax) {
             $key = $countMax;
         }
         // Prochaine étape
         $nextStep = $this->authorizedStep[$key];
         $nextStepUrl = URL . 'checkout/?' . $nextStep . '&lg=' . $this->langue;
         // Récupération de la devise
         $currencyCode = !array_key_exists($this->doorGets->configWeb['currency'], Constant::$currency) ? 'eur' : $this->doorGets->configWeb['currency'];
         $currencyIcon = Constant::$currencyIcon[$currencyCode];
         $Products = $this->products;
         // Init
         $total = 0;
         $shippingCost = 20;
         $countries = $this->doorGets->getArrayForms('country');
         $default = array('company' => '', 'lastname' => '', 'firstname' => '', 'phone' => '', 'address' => '', 'city' => '', 'zipcode' => '', 'country' => '');
         $address = array('shipping' => $default, 'billing' => $default);
         if ($hasUser) {
             $phone = !empty($this->user['tel_mobil']) ? $this->user['tel_mobil'] : $this->user['tel_fix'];
             $default = array('company' => $this->user['company'], 'lastname' => $this->user['last_name'], 'firstname' => $this->user['first_name'], 'phone' => $phone, 'address' => $this->user['adresse'], 'city' => $this->user['city'], 'zipcode' => $this->user['zipcode'], 'country' => $this->user['country']);
             $address = array('shipping' => $default, 'billing' => $default);
         }
         $listingCartParams = array('Products' => $Products, 'total' => $total, 'currencyIcon' => $currencyIcon);
         $formParams = array('form' => $this->doorGets->Form, 'countries' => $countries, 'address' => $address);
         if ($this->step === 'success' || $this->step === 'cancel') {
             if (!empty($this->order['id']) && is_numeric($this->order['id'])) {
                 $orderEntity = new OrderEntity($this->order['id'], $this->doorGets);
                 if ($this->order['method_billing'] === 'paypal') {
                     $token = filter_input(INPUT_GET, 'token', FILTER_SANITIZE_STRING);
                     $payerId = filter_input(INPUT_GET, 'PayerID', FILTER_SANITIZE_STRING);
                     if (!empty($token) && !empty($payerId)) {
                         $paramsGet = array('TOKEN' => $token);
                         $paypalService = new PaypalService($this->doorGets, $this->cart);
                         $response = $paypalService->request('GetExpressCheckoutDetails', $paramsGet);
                         if (empty($response) || $response['ACK'] !== 'Success') {
                             header('Location:./?cancel&lg=' . $this->langue);
                             exit;
                         }
                         if ($response['CHECKOUTSTATUS'] === 'PaymentActionCompleted') {
                             header('Location:./?bis&lg=' . $this->langue);
                             exit;
                         }
                         $paramsGet = array('TOKEN' => $token, 'PAYERID' => $payerId);
                         $response = $paypalService->request('DoExpressCheckoutPayment', $paramsGet);
                         if (empty($response) || $response['ACK'] !== 'Success') {
                             header('Location:./?cancel&lg=' . $this->langue);
                             exit;
                         }
                         $responseToSave = array();
                         foreach ($response as $key => $value) {
                             $key = strtolower($key);
                             $responseToSave[$key] = $value;
                         }
                         $paypalEntity = new PaypalEntity($responseToSave, $this->doorGets);
                         $time = time();
                         $timeHuman = ucfirst(strftime("%A %d %B %Y %H:%M", $time));
                         $paypalEntity->setDateCreation($time);
                         $paypalEntity->setDateModification($time);
                         $paypalEntity->setDateCreationHuman($timeHuman);
                         $paypalEntity->setDateModificationHuman($timeHuman);
                         $paypalEntity->setUserId($this->user['id']);
                         $paypalEntity->setUserGroupe($this->user['groupe']);
                         $paypalEntity->setUserPseudo($this->user['pseudo']);
                         $paypalEntity->save();
                         $orderEntity->setTransactionId($response['PAYMENTINFO_0_TRANSACTIONID']);
                     } else {
                         if ($this->step !== 'cancel') {
                             header('Location:./?cancel&lg=' . $this->langue);
                             exit;
                         }
                     }
                 }
                 $orderEntity->setStatus('payment_' . $this->step);
                 $orderEntity->save(false);
                 $this->cart->status = $this->step;
                 // @todo: Send mail to user
                 $this->cart->reset();
             } else {
                 $this->cart->reset();
                 header('Location:./?lg=' . $this->langue);
                 exit;
             }
         }
         if ($this->step === 'payment') {
             $orderEntity = new OrderEntity($this->order['id'], $this->doorGets);
             $order = $orderEntity->getData();
             $billingMethod = $order['method_billing'];
             switch ($billingMethod) {
                 case 'transfer':
                 case 'check':
                 case 'cash':
                     $orderEntity->setStatus('waiting_' . $billingMethod);
                     $orderEntity->save(false);
                     $this->cart->reset();
                     break;
                 case 'stripe':
                     $postFinish = false;
                     $success = true;
                     $amount = $order['amount'] + $order['shipping_amount'];
                     $log = '';
                     $currency = $order['currency'];
                     $orderId = $order['id'];
                     $isUser = $this->doorGets->dbQS($this->user['id'], '_user_stripe', 'id_user');
                     StripeService::init($this->doorGets);
                     if (array_key_exists('stripeToken', $_POST)) {
                         $token = $_POST['stripeToken'];
                         $isUser = $this->doorGets->dbQS($this->user['id'], '_user_stripe', 'id_user');
                         if (empty($isUser)) {
                             try {
                                 $customer = \Stripe\Customer::create(array('email' => $this->user['login'], 'card' => $token));
                                 $dataCharge = array('customer' => $customer->id, 'amount' => $amount * 100, 'currency' => $currency, "metadata" => array("order_id" => $orderId));
                                 $dataUser = array('id_stripe' => $customer->id, 'id_user' => $this->user['id'], 'date_creation' => time(), 'date_modification' => time());
                                 $this->doorGets->dbQI($dataUser, '_user_stripe');
                             } catch (Stripe_CardError $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                             } catch (Stripe_InvalidRequestError $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                                 // Invalid parameters were supplied to Stripe's API
                             } catch (Stripe_AuthenticationError $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                                 // Authentication with Stripe's API failed
                                 // (maybe you changed API keys recently)
                             } catch (Stripe_ApiConnectionError $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                                 // Network communication with Stripe failed
                             } catch (Stripe_Error $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                                 // Display a very generic error to the user, and maybe send
                                 // yourself an email
                             } catch (Exception $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                                 // Something else happened, completely unrelated to Stripe
                             }
                         } else {
                             $dataCharge = array("amount" => $amount * 100, "currency" => $currency, "customer" => $isUser['id_stripe'], "metadata" => array("order_id" => $orderId));
                         }
                         if ($success) {
                             try {
                                 $charge = \Stripe\Charge::create($dataCharge);
                                 $dataChargeToSave = array("id_user" => $this->user['id'], "id_stripe" => $dataCharge['customer'], "id_charge" => $charge->id, "id_order" => $orderId, "status" => $charge->status, "amount" => $charge->amount, "currency" => $charge->currency, "data" => base64_encode(serialize($charge)), 'date_creation' => time(), 'date_modification' => time());
                             } catch (Stripe_CardError $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                             } catch (Stripe_InvalidRequestError $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                                 // Invalid parameters were supplied to Stripe's API
                             } catch (Stripe_AuthenticationError $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                                 // Authentication with Stripe's API failed
                                 // (maybe you changed API keys recently)
                             } catch (Stripe_ApiConnectionError $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                                 // Network communication with Stripe failed
                             } catch (Stripe_Error $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                                 // Display a very generic error to the user, and maybe send
                                 // yourself an email
                             } catch (Exception $e) {
                                 $success = false;
                                 $log .= $e->getMessage() . "\n";
                                 // Something else happened, completely unrelated to Stripe
                             }
                             if ($success) {
                                 $idNewCharge = $this->doorGets->dbQI($dataChargeToSave, '_user_stripe_charge');
                                 $orderEntity->setStatus('card_success');
                                 $orderEntity->setTransactionId($charge->id);
                                 $orderEntity->save(false);
                                 $postFinish = true;
                                 $paymentUrl = './?success&lg=' . $this->langue;
                                 header('Location:' . $paymentUrl);
                                 exit;
                             }
                         }
                     }
                     if (!$success) {
                         $log .= $orderEntity->getErrorLog();
                         $orderEntity->setStatus('card_denied');
                         $orderEntity->setErrorLog($log);
                         $orderEntity->save(false);
                         $paymentUrl = './?cancel&lg=' . $this->langue;
                         header('Location:' . $paymentUrl);
                         exit;
                     }
                     break;
                 default:
                     header('Location:./?cancel&lg=' . $this->langue);
                     exit;
                     break;
             }
         }
         // Récupération de la view
         $fileName = 'checkout/step/checkout_' . $this->step;
         $tpl = Template::getView($fileName, $formParams);
         ob_start();
         if (is_file($tpl)) {
             include $tpl;
         }
         $out .= ob_get_clean();
     }
     return $out;
 }