/** * If a Cart Payment exists for the shopping cart it is returned * Otherwise we create one for the shopping cart. * * @return bool|CartPayment */ public static function getOrCreateCartPayment() { $objPayment = null; if (is_null(Yii::app()->shoppingcart->payment_id) === false) { $objPayment = CartPayment::model()->findByPk(Yii::app()->shoppingcart->payment_id); } else { $objPayment = new CartPayment(); if ($objPayment->save() === false) { Yii::log("Error saving payment:\n" . print_r($objPayment->getErrors(), true), 'error', 'application.' . __CLASS__ . '.' . __FUNCTION__); return false; } } return $objPayment; }
/** * Our AIM payment handling function * * @param $intPaymentId * @return bool */ protected function runPayment($intPaymentId) { $objCart = Yii::app()->shoppingcart; $objPayment = CartPayment::model()->findByPk($intPaymentId); Yii::log("Running payment on " . $objCart->id_str, 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $arrPaymentResult = Yii::app()->getComponent($objPayment->payment_module)->setCheckoutForm($this->checkoutForm)->run(); Yii::log("{$objPayment->payment_module} result:\n" . print_r($arrPaymentResult, true), 'info', 'application.' . __CLASS__ . '.' . __FUNCTION__); return $this->finalizeOrder($arrPaymentResult); }
/** * This function will run parse an order that we get from Amazon MWS. * It saves orders of the customers to the DB. * @param $response ListOrderItemsResponse Contains the orders from Amazon * Marketplace WebService * @return void */ public function parseListOrders($response) { $checkDate = date("Y-m-d", strtotime($this->amazon_check_time)); $listOrdersResult = $response->getListOrdersResult(); if ($listOrdersResult->isSetOrders()) { $orders = $listOrdersResult->getOrders(); $orderList = $orders->getOrder(); foreach ($orderList as $order) { if ($order->isSetAmazonOrderId()) { $strOrderId = $order->getAmazonOrderId(); Yii::log("Found Amazon Order " . $strOrderId, 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $objCart = Cart::LoadByIdStr($strOrderId); if (!$objCart instanceof Cart) { //We ignore orders we've already downloaded $objCart = new Cart(); $objCart->id_str = $strOrderId; $objCart->origin = 'amazon'; //We mark this as just a cart, not an order, because we download the items next $objCart->cart_type = CartType::cart; $objOrderTotal = $order->getOrderTotal(); Yii::log("Order total information " . print_r($objOrderTotal, true), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $objCart->total = $objOrderTotal->getAmount(); $objCart->currency = $objOrderTotal->getCurrencyCode(); $objCart->status = OrderStatus::Requested; $objCart->datetime_cre = $order->getPurchaseDate(); $objCart->modified = $order->getLastUpdateDate(); if (!$objCart->save()) { Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } //Since email from is Anonymous, we probably will have to create a shell record $objCustomer = Customer::LoadByEmail($order->getBuyerEmail()); if (!$objCustomer) { $customerName = $this->_getCustomerName($order->getBuyerName()); $objCustomer = new Customer(); $objCustomer->email = $order->getBuyerEmail(); $objCustomer->first_name = $customerName['first_name']; $objCustomer->last_name = $customerName['last_name']; $objCustomer->record_type = Customer::EXTERNAL_SHELL_ACCOUNT; $objCustomer->allow_login = Customer::UNAPPROVED_USER; $objCustomer->save(); } $objCart->customer_id = $objCustomer->id; if (!$objCart->save()) { Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } if ($order->isSetShippingAddress()) { $shippingAddress = $order->getShippingAddress(); $countrycode = Country::IdByCode($shippingAddress->getCountryCode()); if ($shippingAddress->isSetStateOrRegion()) { $objState = State::LoadByCode($shippingAddress->getStateOrRegion(), $countrycode); } $customerName = $this->_getCustomerName($shippingAddress->getName()); $config = array('address_label' => 'amazon', 'customer_id' => $objCustomer->id, 'first_name' => $customerName['first_name'], 'last_name' => $customerName['last_name'], 'address1' => $shippingAddress->getAddressLine1(), 'address2' => trim($shippingAddress->getAddressLine2() . " " . $shippingAddress->getAddressLine3()), 'city' => $shippingAddress->getCity(), 'state_id' => $objState->id, 'postal' => $shippingAddress->getPostalCode(), 'country_id' => $countrycode, 'phone' => $shippingAddress->getPhone()); $objCustAddress = CustomerAddress::findOrCreate($config); $objCustomer->default_billing_id = $objCustAddress->id; $objCustomer->default_shipping_id = $objCustAddress->id; $objCustomer->save(); $objCart->shipaddress_id = $objCustAddress->id; $objCart->billaddress_id = $objCustAddress->id; //Amazon doesn't provide billing data, just dupe if (!$objCart->save()) { Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } Yii::log("Looking for destination " . $objState->country_code . " " . $objState->code . " " . $shippingAddress->getPostalCode(), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $objDestination = Destination::LoadMatching($objState->country_code, $objState->code, $shippingAddress->getPostalCode()); if ($objDestination === null) { Yii::log("Did not find destination, using default in Web Store ", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $objDestination = Destination::getAnyAny(); } $objCart->tax_code_id = $objDestination->taxcode; $objCart->recalculateAndSave(); } if ($order->isSetShipServiceLevel()) { $strShip = $order->getShipServiceLevel(); //If we have a shipping object already, update it, otherwise create it if (isset($objCart->shipping)) { $objShipping = $objCart->shipping; } else { //create $objShipping = new CartShipping(); if (!$objShipping->save()) { Yii::log("Error saving shipping info for cart " . print_r($objShipping->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } } if ($order->isSetShipmentServiceLevelCategory()) { $strShip = $order->getShipmentServiceLevelCategory(); } $objShipping->shipping_module = get_class($this); $objShipping->shipping_data = $strShip; $objShipping->shipping_method = $this->objModule->getConfig('product'); $objShipping->shipping_cost = 0; $objShipping->shipping_sell = 0; $objShipping->save(); $objCart->shipping_id = $objShipping->id; if (!$objCart->save()) { Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } } //Because Amazon comes down with no payment info, just generate one here $objP = new CartPayment(); $objP->payment_method = $this->objModule->getConfig('ls_payment_method'); $objP->payment_module = get_class($this); $objP->payment_data = 'Amazon'; $objP->payment_amount = $objOrderTotal->getAmount(); $objP->datetime_posted = $order->getPurchaseDate(); if (!$objP->save()) { Yii::log("Error saving payment " . print_r($objP->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } $objCart->payment_id = $objP->id; if (!$objCart->save()) { Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } TaskQueue::CreateEvent('integration', get_class($this), 'ListOrderDetails', $objCart->id_str . "," . $checkDate); } } } } }
/** * Extract shipping and billing address information, create address book and map to the carts */ protected function actionConvertAddressBook() { $sql = "select * from xlsws_cart where billaddress_id IS NULL and address_bill IS NOT NULL order by id limit 500"; $arrProducts = Yii::app()->db->createCommand($sql)->query(); while (($result = $arrProducts->read()) !== false) { $result['email'] = strtolower($result['email']); //verify that Customer ID really exists in customer table $objCust = Customer::model()->findByPk($result['customer_id']); if (!$objCust instanceof Customer) { $result['customer_id'] = 0; } if (strlen($result['address_bill']) > 0) { $arrAddress = explode("\n", $result['address_bill']); if (count($arrAddress) == 5) { //old format address, should be 6 pieces $arrAddress[5] = $arrAddress[4]; $strSt = $arrAddress[3]; if ($strSt[0] == " ") { //no state on this address $arrAddress[4] = substr($strSt, 1, 100); $arrAddress[3] = ""; } else { $arrSt = explode(" ", $strSt); $arrAddress[3] = $arrSt[0]; $arrAddress[4] = str_replace($arrSt[0] . " ", "", $strSt); } } $objAddress = new CustomerAddress(); if (count($arrAddress) >= 5) { $objCountry = Country::LoadByCode($arrAddress[5]); if ($objCountry) { $objAddress->country_id = $objCountry->id; $objState = State::LoadByCode($arrAddress[3], $objCountry->id); if ($objState) { $objAddress->state_id = $objState->id; } } $objAddress->address1 = $arrAddress[0]; $objAddress->address2 = $arrAddress[1]; $objAddress->city = $arrAddress[2]; $objAddress->postal = $arrAddress[4]; $objAddress->first_name = $result['first_name']; $objAddress->last_name = $result['last_name']; $objAddress->company = $result['company']; $objAddress->phone = $result['phone']; $objAddress->residential = CustomerAddress::RESIDENTIAL; $objAddress->created = $result['datetime_cre']; $objAddress->modified = $result['datetime_cre']; $objAddress->active = 1; if (empty($objAddress->address2)) { $objAddress->address2 = null; } if (empty($objAddress->company)) { $objAddress->company = null; } $blnFound = false; if ($result['customer_id'] > 0) { //See if this is already in our database $objPriorAddress = CustomerAddress::model()->findByAttributes(array('address1' => $objAddress->address1, 'address2' => $objAddress->address2, 'city' => $objAddress->city, 'postal' => $objAddress->postal, 'first_name' => $objAddress->first_name, 'last_name' => $objAddress->last_name, 'company' => $objAddress->company, 'phone' => $objAddress->phone)); if ($objPriorAddress instanceof CustomerAddress) { Yii::app()->db->createCommand("update xlsws_cart set billaddress_id=" . $objPriorAddress->id . " where id=" . $result['id'])->execute(); $blnFound = true; } else { $objAddress->customer_id = $result['customer_id']; } } else { //We need a shell customer record just for the email $objC = Customer::model()->findByAttributes(array('email' => $result['email'])); if ($objC instanceof Customer) { Yii::app()->db->createCommand("UPDATE xlsws_cart set customer_id=" . $objC->id . " where id=" . $result['id'])->execute(); } else { $objC = new Customer(); $objC->record_type = Customer::GUEST; $objC->email = $result['email']; $objC->first_name = $objAddress->first_name; $objC->last_name = $objAddress->last_name; $objC->company = $objAddress->company; if (!$objC->validate()) { $arrErr = $objC->getErrors(); if (isset($arrErr['email'])) { $objC->email = $result['id'] . "*****@*****.**"; } if (!$objC->validate()) { return print_r($objC->getErrors(), true); } } if (!$objC->save()) { Yii::log("Import Error " . print_r($objC->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return print_r($objC->getErrors(), true); } else { $cid = $objC->id; } Yii::app()->db->createCommand("UPDATE xlsws_cart set customer_id=" . $cid . " where id=" . $result['id'])->execute(); } $result['customer_id'] = $objC->id; $objAddress->customer_id = $result['customer_id']; } if (!$blnFound) { if (!$objAddress->save()) { //We have a corrupt billing address, just blank it out so import goes on Yii::app()->db->createCommand("update xlsws_cart set address_bill=null where id=" . $result['id'])->execute(); } else { $cid = $objAddress->id; Yii::app()->db->createCommand("update xlsws_cart set billaddress_id=" . $cid . " where id=" . $result['id'])->execute(); } } } else { //We have a corrupt billing address, just blank it out so import goes on Yii::app()->db->createCommand("update xlsws_cart set address_bill=null where id=" . $result['id'])->execute(); } $objAddress = new CustomerAddress(); $objCountry = Country::LoadByCode($result['ship_country']); if ($objCountry) { $objAddress->country_id = $objCountry->id; $objState = State::LoadByCode($result['ship_state'], $objCountry->id); if ($objState) { $objAddress->state_id = $objState->id; } } $objAddress->first_name = $result['ship_firstname']; $objAddress->last_name = $result['ship_lastname']; $objAddress->company = $result['ship_company']; $objAddress->address1 = $result['ship_address1']; $objAddress->address2 = $result['ship_address2']; $objAddress->city = $result['ship_city']; $objAddress->postal = $result['ship_zip']; $objAddress->phone = $result['ship_phone']; $objAddress->residential = CustomerAddress::RESIDENTIAL; $objAddress->created = $result['datetime_cre']; $objAddress->modified = $result['datetime_cre']; $objAddress->active = 1; if (empty($objAddress->address2)) { $objAddress->address2 = null; } if (empty($objAddress->company)) { $objAddress->company = null; } $blnFound = false; if ($result['customer_id'] > 0) { //See if this is already in our database $objPriorAddress = CustomerAddress::model()->findByAttributes(array('address1' => $objAddress->address1, 'city' => $objAddress->city, 'postal' => $objAddress->postal, 'first_name' => $objAddress->first_name, 'last_name' => $objAddress->last_name, 'company' => $objAddress->company, 'phone' => $objAddress->phone)); if ($objPriorAddress instanceof CustomerAddress) { Yii::app()->db->createCommand("update xlsws_cart set shipaddress_id=" . $objPriorAddress->id . " where id=" . $result['id'])->execute(); $blnFound = true; } else { $objAddress->customer_id = $result['customer_id']; } } if (!$blnFound) { if (!$objAddress->save()) { Yii::log("Import Error " . print_r($objAddress->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } else { $cid = $objAddress->id; Yii::app()->db->createCommand("update xlsws_cart set shipaddress_id=" . $cid . " where id=" . $result['id'])->execute(); } } } $objShipping = new CartShipping(); $objShipping->shipping_method = $result['shipping_method']; $objShipping->shipping_module = $result['shipping_module']; $objShipping->shipping_data = $result['shipping_data']; $objShipping->shipping_cost = $result['shipping_cost']; $objShipping->shipping_sell = $result['shipping_sell']; if (!$objShipping->save()) { return print_r($objShipping->getErrors()); } else { $cid = $objShipping->id; } Yii::app()->db->createCommand("update xlsws_cart set shipping_id=" . $cid . " where id=" . $result['id'])->execute(); $objPayment = new CartPayment(); $objPayment->payment_method = $result['payment_method']; $objPayment->payment_module = str_replace(".php", "", $result['payment_module']); $objPayment->payment_data = $result['payment_data']; $objPayment->payment_amount = $result['payment_amount']; $objPayment->datetime_posted = $result['datetime_posted']; if ($result['fk_promo_id'] > 0) { $objPromo = PromoCode::model()->findByPk($result['fk_promo_id']); if ($objPromo) { $objPayment->promocode = $objPromo->code; } } if (!$objPayment->save()) { return print_r($objPayment->getErrors()); } else { $cid = $objPayment->id; } Yii::app()->db->createCommand("update xlsws_cart set payment_id=" . $cid . " where id=" . $result['id'])->execute(); } $results2 = Yii::app()->db->createCommand("select count(*) from xlsws_cart where billaddress_id IS NULL and address_bill IS NOT NULL")->queryScalar(); if ($results2 == 0) { $remain = 8; } else { $remain = 3; } return array('result' => "success", 'makeline' => $remain, 'total' => 50, 'tag' => 'Converting cart addresses, ' . $results2 . ' remaining'); }
/** * Mark a cart as paid manually */ public function actionPay() { $id = Yii::app()->getRequest()->getQuery('id'); $objCartPayment = CartPayment::model()->findByPk($id); $objCart = Cart::model()->findByAttributes(array('payment_id' => $id)); if (isset($_POST['Cart']) && isset($_POST['CartPayment'])) { echo self::processManualPayment($objCart, $objCartPayment); } else { echo $this->renderPartial("_pay", array('objCart' => $objCart, 'model' => $objCartPayment), true); } }
/** * Process payments coming in from third party systems, such as Paypal IPN and other SIM integrations */ public function actionPayment() { $strModule = Yii::app()->getRequest()->getQuery('id'); Yii::log("Incoming message for " . $strModule, 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); try { $retVal = Yii::app()->getComponent($strModule)->gateway_response_process(); Yii::log("Gateway response {$strModule}:\n" . print_r($retVal, true), 'info', 'application.' . __CLASS__ . '.' . __FUNCTION__); if (is_array($retVal)) { if ($retVal['success']) { $objCart = Cart::model()->findByAttributes(array('id_str' => $retVal['order_id'])); if (is_null($objCart)) { Yii::log($retVal['order_id'] . " is not our order, ignoring", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); } if ($objCart instanceof Cart && $objCart->cart_type == CartType::awaitpayment) { $objPayment = CartPayment::model()->findByPk($objCart->payment_id); $objPayment->payment_amount = isset($retVal['amount']) ? $retVal['amount'] : 0; $objPayment->payment_data = $retVal['data']; $objPayment->datetime_posted = isset($retVal['payment_date']) ? date("Y-m-d H:i:s", strtotime($retVal['payment_date'])) : new CDbExpression('NOW()'); $objPayment->save(); self::EmailReceipts($objCart); Yii::log('Receipt e-mails added to the queue', 'info', __CLASS__ . '.' . __FUNCTION__); self::FinalizeCheckout($objCart, Yii::app()->getComponent($strModule)->performInternalFinalizeSteps); Yii::log('Checkout Finalized', 'info', __CLASS__ . '.' . __FUNCTION__); if (!isset($retVal['output'])) { Yii::app()->controller->redirect(Yii::app()->controller->createAbsoluteUrl('cart/receipt', array('getuid' => $objCart->linkid), 'http')); } } } if (isset($retVal['output'])) { echo $retVal['output']; } else { $objCart = Cart::LoadByIdStr($retVal['order_id']); if ($objCart instanceof Cart) { Yii::app()->controller->redirect(Yii::app()->controller->createAbsoluteUrl('cart/restore', array('getuid' => $objCart->linkid))); } echo Yii::t('global', 'Payment Error: Was not successful, and payment attempt did not return a proper error message'); } } } catch (Exception $e) { //Can't find module. if $val=="fancyshipping" then filename must be "FancyshippingModule.php" (case sensitive) Yii::log("Received payment but could not process {$e}", 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } }