array(), 'missingOrder' => array(), 'multipleOrders' => array(), 'foundWithEmail' => array(), 'foundWithPaypalTable' => array(), 'foundWithFnLn' => array(), 'lostCustomers' => array(), 'orderToProcess' => array() ); if ($fp) { echo 'Reading ' . __DIR__ . '/Download.csv' . "\n"; while (($line = fgets($fp)) !== false) { $splitCsvLine = str_getcsv($line, ',', '"'); $error = ''; if ($splitCsvLine[1] !== 'Heure' && $splitCsvLine[10] !== 'contact@bebeboutik.com') { $count++; $values = array(); $values['amount'] = str_replace(',', '.', $splitCsvLine[7]); $values['email'] = $splitCsvLine[10]; $values['firstname_lastname'] = $splitCsvLine[3]; $values['id_transaction'] = $splitCsvLine[12]; $values['shipping_cost'] = str_replace(',', '.', $splitCsvLine[17]); $values['date'] = $splitCsvLine[0] . ' ' . $splitCsvLine[1]; $exploded = explode(' ', $values['firstname_lastname']); if (count($exploded) === 2) { $values['possible_first_name'] = $exploded[0]; $values['possible_last_name'] = $exploded[1]; } else { $values['possible_first_name'] = $exploded[0]; unset($exploded[0]); $values['possible_last_name'] = implode(' ', $exploded); } if (!array_key_exists($values['email'], $emailToIdCustomerArray)) { //check paypal table first $result = Db::getInstance()->getRow(' SELECT id_customer FROM ps_paypal_customer WHERE paypal_email = \'' . pSQL($values['email']) . '\''); if ($result) { $customer = new Customer((int)$result['id_customer']); } if ($customer->id !== null && $result) { $report['foundWithPaypalTable'][$values['email']] = $customer->id; $emailToIdCustomerArray[$values['email']] = (int)$customer->id; } else { $customer = new Customer(); $customer->getByEmail($values['email']); if (Validate::isLoadedObject($customer)) { $emailToIdCustomerArray[$values['email']] = (int)$customer->id; $report['foundWithEmail'][$values['email']] = (int)$customer->id; } else { $result = Db::getInstance()->getRow(' SELECT id_customer FROM `ps_customer` WHERE `active` = 1 AND lower(`firstname`) = \'' . pSQL(strtolower($values['possible_first_name'])) . '\' AND lower(`lastname`) = \'' . pSQL(strtolower($values['possible_last_name'])) . '\' AND `deleted` = 0 AND `is_guest` = 0'); if ($result) { $customer = new Customer((int)$result['id_customer']); $report['foundWithFnLn'][$values['email']] = $customer->id; $emailToIdCustomerArray[$values['email']] = (int)$customer->id; } else { $result = Db::getInstance()->getRow(' SELECT id_customer FROM `ps_customer` WHERE `active` = 1 AND lower(`lastname`) = \'' . pSQL(strtolower($values['possible_first_name'])) . '\' AND lower(`firstname`) = \'' . pSQL(strtolower($values['possible_last_name'])) . '\' AND `deleted` = 0 AND `is_guest` = 0'); if ($result) { $customer = new Customer((int)$result['id_customer']); $report['foundWithFnLn'][$values['email']] = $customer->id; $emailToIdCustomerArray[$values['email']] = (int)$customer->id; } else { $report['lostCustomers'][] = array('email' => $values['email'], 'info' => $splitCsvLine); } } } } } if (Validate::isLoadedObject($customer)) { $order = Db::getInstance()->getRow(' SELECT po.id_order, o.total_paid FROM ps_paypal_order po JOIN ps_orders o ON o.id_order = po.id_order WHERE id_transaction = \'' . $values['id_transaction'] . '\'' ); if ($order) { if ($values['amount'] !== $order['total_paid']) { $report['wrongTotal'][] = $order['id_order']; } } else { $orders = Db::getInstance()->ExecuteS(' SELECT o.id_order , o.total_paid FROM `ps_orders` o WHERE o.`id_customer` = ' . (int)$customer->id . ' AND o.date_add BETWEEN "2017-12-20 00:00:00" AND "2017-12-23 23:59:00"' ); if ($orders) { if (count($orders) === 1) { if ($values['amount'] !== $orders[0]['total_paid']) { $report['wrongTotal'][] = $orders[0]['id_order']; } } else { $report['multipleOrders'][] = $customer->id; } } else { //try to find //now we try to find id_cart that could do the job if (534175 === (int)$customer->id) { echo 'test 534175'; } $result = Db::getInstance()->ExecuteS(' SELECT c.`id_cart` , c.date_upd FROM `ps_cart` c WHERE c.`id_customer` = ' . (int)$customer->id . ' ORDER BY c.date_upd desc'); if (534175 === (int)$customer->id) { echo json_encode($result); } if ($result && count($result) > 0) { $selectedCartId = $result[0]['id_cart']; foreach ($result as $cartResult) { $cart = new Cart((int)$cartResult['id_cart']); $cartValue = $cart->getOrderTotal(); if ((float)$values['amount'] === $cartValue) { $selectedCartId = (int)$cartResult['id_cart']; continue; } } $selectedCart = new Cart($selectedCartId); $cartValue = $selectedCart->getOrderTotal(); if ((float)$values['amount'] === $cartValue) { $errorWIthCartValue = 'ok'; } else { $errorWIthCartValue = 'not ok'; } $foundVia = 'nothing'; if (array_key_exists($values['email'], $report['foundWithPaypalTable'])) { $foundVia = 'paypaltable'; } elseif (array_key_exists($values['email'], $report['foundWithEmail'])) { $foundVia = 'email'; } elseif (array_key_exists($values['email'], $report['foundWithFnLn'])) { $foundVia = 'lastname'; } $report['missingOrder'][] = array('customer_id' => $customer->id, 'amount' => $values['amount'], 'transaction_id' => $values['id_transaction'], 'id_cart' => $selectedCart->id, 'cart_value' => $cartValue, 'error_with_cart_value' => $errorWIthCartValue, 'foundVia' => $foundVia ); if ($errorWIthCartValue === 'ok') { $report['orderToProcess'][$values['id_transaction']] = array('customer_id' => $customer->id, 'amount' => $values['amount'], 'transaction_id' => $values['id_transaction'], 'id_cart' => $selectedCart->id, 'email' => $values['email'], 'shipping_cost' => $values['shipping_cost'], 'order_time' => $values['date'] ); } } else { $report['missingOrder'][] = array('customer_id' => $customer->id, 'amount' => $values['amount'], 'transaction_id' => $values['id_transaction'], 'id_cart' => 'not found' ); } } } } } } $report['missingOrderCount'] = count($report['missingOrder']); $report['wrongTotalCount'] = count($report['wrongTotal']); $report['missingOrderCount'] = count($report['missingOrder']); $report['foundWithEmailCount'] = count($report['foundWithEmail']); $report['foundWithPaypalTableCount'] = count($report['foundWithPaypalTable']); $report['foundWithFnLnCount'] = count($report['foundWithFnLn']); $report['lostCustomersCount'] = count($report['lostCustomers']); $report['orderToProcessCount'] = count($report['orderToProcess']); fclose($fp); } if ($scriptDebug) { echo 'count nb lines ' . $count . "\n"; echo 'unique customers found ' . count($emailToIdCustomerArray) . "\n"; echo 'customer not found ' . $customerNotFoundCount . "\n"; echo '---report' . "\n"; echo json_encode($report) . "\n"; } unset($values); foreach ($report['orderToProcess'] as $id_transaction => $orderToProcess) { echo 'treating cart ' . $orderToProcess['id_cart'] . "\n"; $request_type = 'payment_cart'; $ppec = new PaypalExpressCheckout($request_type); $cart = new Cart((int)$orderToProcess['id_cart']); $customer = new Customer((int)$cart->id_customer); if (!$cart->OrderExists()) { $payment_type = (int)Configuration::get('PS_OS_PAYMENT'); $payment_status = 'Completed'; $message = $ppec->l('Payment accepted.') . '
'; $transaction = array( 'currency' => 'EUR', 'id_invoice' => null, 'id_transaction' => $id_transaction, 'transaction_id' => $id_transaction, 'total_paid' => (float)$orderToProcess['amount'], 'shipping' => (float)$orderToProcess['shipping_cost'], 'payment_date' => $orderToProcess['order_time'], 'payment_status' => $payment_status ); $order_total = (float)$cart->getOrderTotal(true, Cart::BOTH); if ($scriptDebug) { $params = array( 'cart_id ' => (int)$cart->id, 'payment_type' => $payment_type, 'order_total' => $order_total, 'displayName' => 'PayPal', 'message' => $message, 'transaction' => $transaction, 'cart->id_currency' => (int)$cart->id_currency, 'customer->secure_key' => $customer->secure_key ); echo json_encode($params); } else { $ppec->validateOrder((int)$cart->id, $payment_type, $order_total, 'PayPal', $message, $transaction, (int)$cart->id_currency, false, $customer->secure_key); } } }