/** * Creates an order for the specified user, and redirects to the edit page. * * @param \Drupal\user\UserInterface $user * The user to create the order for. */ public function createForUser(UserInterface $user) { $order = Order::create(['uid' => $user->id(), 'order_status' => uc_order_state_default('post_checkout')]); $order->save(); uc_order_comment_save($order->id(), \Drupal::currentUser()->id(), $this->t('Order created by the administration.'), 'admin'); return $this->redirect('entity.uc_order.edit_form', ['uc_order' => $order->id()]); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $items = \Drupal::service('uc_cart.manager')->get()->getContents(); $paypal_config = $this->config('uc_paypal.settings'); if (empty($items)) { drupal_set_message($this->t('You do not have any items in your shopping cart.')); return; } list($desc, $subtotal) = _uc_paypal_product_details($items); $order = Order::create(['uid' => $this->currentUser()->id()]); $order->save(); $nvp_request = array('METHOD' => 'SetExpressCheckout', 'RETURNURL' => Url::fromRoute('uc_paypal.ec_review', [], ['absolute' => TRUE])->toString(), 'CANCELURL' => Url::fromRoute('uc_cart.cart', [], ['absolute' => TRUE])->toString(), 'AMT' => uc_currency_format($subtotal, FALSE, FALSE, '.'), 'CURRENCYCODE' => $order->getCurrency(), 'PAYMENTACTION' => $paypal_config->get('wpp_cc_txn_type') == 'authorize' ? 'Authorization' : 'Sale', 'DESC' => substr($desc, 0, 127), 'INVNUM' => $order->id() . '-' . REQUEST_TIME, 'REQCONFIRMSHIPPING' => $paypal_config->get('ec_rqconfirmed_addr'), 'BUTTONSOURCE' => 'Ubercart_ShoppingCart_EC_US', 'NOTIFYURL' => Url::fromRoute('uc_paypal.ipn', [], ['absolute' => TRUE])->toString(), 'LANDINGPAGE' => $paypal_config->get('ec_landingpage_style')); $order->products = $items; $order->save(); $nvp_response = uc_paypal_api_request($nvp_request, $paypal_config->get('wpp_server')); if ($nvp_response['ACK'] != 'Success') { drupal_set_message($this->t('PayPal reported an error: @code: @message', ['@code' => $nvp_response['L_ERRORCODE0'], '@message' => $nvp_response['L_LONGMESSAGE0']]), 'error'); return; } $session = \Drupal::service('session'); $session->set('cart_order', $order->id()); $session->set('TOKEN', $nvp_response['TOKEN']); $sandbox = ''; if (strpos($paypal_config->get('wpp_server'), 'sandbox') > 0) { $sandbox = 'sandbox.'; } header('Location: https://www.' . $sandbox . 'paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=' . $session->get('TOKEN')); exit; }
/** * Tests customer overview. */ public function testCustomerAdminPages() { $this->drupalLogin($this->adminUser); $country = Country::load('US'); Order::create(array('uid' => $this->customer->id(), 'billing_country' => $country->id(), 'billing_zone' => 'AK'))->save(); $this->drupalGet('admin/store/customers/view'); $this->assertResponse(200); $this->assertLinkByHref('user/' . $this->customer->id()); $this->assertText($country->getZones()['AK']); $this->assertText($country->label()); }
public function testShipmentsUI() { $this->drupalLogin($this->adminUser); $method = $this->createPaymentMethod('other'); // Process an anonymous, shippable order. $order = Order::create(['uid' => 0, 'primary_email' => $this->randomString() . '@example.org', 'payment_method' => $method['id']]); // Add three more products to use for our tests. $products = array(); for ($i = 1; $i <= 4; $i++) { $product = $this->createProduct(array('uid' => $this->adminUser->id(), 'promote' => 0)); $order->products[$i] = OrderProduct::create(array('nid' => $product->nid->target_id, 'title' => $product->title->value, 'model' => $product->model, 'qty' => 1, 'cost' => $product->cost->value, 'price' => $product->price->value, 'weight' => $product->weight, 'data' => [])); $order->products[$i]->data->shippable = 1; } $order->save(); $order = Order::load($order->id()); uc_payment_enter($order->id(), $method['id'], $order->getTotal()); // Now quickly package all the products in this order. $this->drupalGet('admin/store/orders/' . $order->id() . '/packages'); $this->drupalPostForm(NULL, array('shipping_types[small_package][table][1][checked]' => 1, 'shipping_types[small_package][table][2][checked]' => 1, 'shipping_types[small_package][table][3][checked]' => 1, 'shipping_types[small_package][table][4][checked]' => 1), t('Create one package')); // Test "Ship" operations for this package. $this->drupalGet('admin/store/orders/' . $order->id() . '/packages'); $this->assertLink(t('Ship')); $this->clickLink(t('Ship')); $this->assertUrl('admin/store/orders/' . $order->id() . '/shipments/new?pkgs=1'); foreach ($order->products as $sequence => $item) { $this->assertText($item->qty->value . ' x ' . $item->model->value, 'Product quantity x SKU found.'); // Test for weight here too? How do we compute this? } // We're shipping a specific package, so it should already be checked. foreach ($order->products as $sequence => $item) { $this->assertFieldByName('shipping_types[small_package][table][1][checked]', 1, 'Package is available for shipping.'); } $this->assertFieldByName('method', 'manual', 'Manual shipping method selected.'); // // Test presence and operation of ship operation on order admin View. // $this->drupalGet('admin/store/orders/view'); $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/shipments'); // Test action. $this->clickLink(t('Ship')); $this->assertResponse(200); $this->assertUrl('admin/store/orders/' . $order->id() . '/shipments/new'); $this->assertText('No shipments have been made for this order.', 'Ship action found.'); $this->assertText($order->products[1]->qty->value . ' x ' . $order->products[1]->model->value, 'Product quantity x SKU found.'); $this->assertFieldByName('method', 'manual', 'Manual shipping method selected.'); // Test reaching this through the shipments tab too ... }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { switch ($form_state->getValue('customer_type')) { case 'search': $uid = $form_state->getValue(['customer', 'uid']); break; case 'create': // Create new account. $email = trim($form_state->getValue(['customer', 'email'])); $fields = array('name' => uc_store_email_to_username($email), 'mail' => $email, 'pass' => user_password(), 'status' => $this->config('uc_cart.settings')->get('new_customer_status_active') ? 1 : 0); $account = \Drupal\user\Entity\User::create($fields); $account->save(); $uid = $account->id(); if ($form_state->getValue(['customer', 'sendmail'])) { // Manually set the password so it appears in the e-mail. $account->password = $fields['pass']; \Drupal::service('plugin.manager.mail')->mail('user', 'register_admin_created', $email, uc_store_mail_recipient_langcode($email), array('account' => $account), uc_store_email_from()); drupal_set_message(t('A welcome message has been e-mailed to the new user.')); } break; default: $uid = 0; } $order = \Drupal\uc_order\Entity\Order::create(array('uid' => $uid, 'order_status' => uc_order_state_default('post_checkout'))); $order->save(); uc_order_comment_save($order->id(), \Drupal::currentUser()->id(), t('Order created by the administration.'), 'admin'); $form_state->setRedirect('entity.uc_order.edit_form', ['uc_order' => $order->id()]); }
/** * Creates a new order directly, without going through checkout. */ protected function createOrder($edit = []) { if (empty($edit['primary_email'])) { $edit['primary_email'] = $this->randomString() . '@example.org'; } $order = Order::create($edit); if (!isset($edit['products'])) { $order->products[] = OrderProduct::create(array('nid' => $this->product->nid->target_id, 'title' => $this->product->title->value, 'model' => $this->product->model, 'qty' => 1, 'cost' => $this->product->cost->value, 'price' => $this->product->price->value, 'weight' => $this->product->weight, 'data' => [])); } $order->save(); return Order::load($order->id()); }
/** * Displays the cart checkout page built of checkout panes from enabled modules. */ public function checkout() { $cart_config = $this->config('uc_cart.settings'); $items = $this->cartManager->get()->getContents(); if (count($items) == 0 || !$cart_config->get('checkout_enabled')) { return $this->redirect('uc_cart.cart'); } // Send anonymous users to login page when anonymous checkout is disabled. if ($this->currentUser()->isAnonymous() && !$cart_config->get('checkout_anonymous')) { drupal_set_message($this->t('You must login before you can proceed to checkout.')); if ($this->config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { drupal_set_message($this->t('If you do not have an account yet, you should <a href=":url">register now</a>.', [':url' => Url::fromRoute('user.register', [], ['query' => drupal_get_destination()])->toString()])); } return $this->redirect('user.page', [], ['query' => drupal_get_destination()]); } // Load an order from the session, if available. if ($this->session->has('cart_order')) { $order = $this->loadOrder(); if ($order) { // Don't use an existing order if it has changed status or owner, or if // there has been no activity for 10 minutes (to prevent identity theft). if ($order->getStateId() != 'in_checkout' || $this->currentUser()->isAuthenticated() && $this->currentUser()->id() != $order->getOwnerId() || $order->getChangedTime() < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { if ($order->getStateId() == 'in_checkout' && $order->getChangedTime() < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { // Mark expired orders as abandoned. $order->setStatusId('abandoned')->save(); } unset($order); } } else { // Ghost session. $this->session->remove('cart_order'); drupal_set_message($this->t('Your session has expired or is no longer valid. Please review your order and try again.')); return $this->redirect('uc_cart.cart'); } } // Determine whether the form is being submitted or built for the first time. if (isset($_POST['form_id']) && $_POST['form_id'] == 'uc_cart_checkout_form') { // If this is a form submission, make sure the cart order is still valid. if (!isset($order)) { drupal_set_message($this->t('Your session has expired or is no longer valid. Please review your order and try again.')); return $this->redirect('uc_cart.cart'); } elseif ($this->session->has('uc_cart_order_rebuild')) { drupal_set_message($this->t('Your shopping cart contents have changed. Please review your order and try again.')); return $this->redirect('uc_cart.cart'); } } else { // Prepare the cart order. $rebuild = FALSE; if (!isset($order)) { // Create a new order if necessary. $order = Order::create(array('uid' => $this->currentUser()->id())); $order->save(); $this->session->set('cart_order', $order->id()); $rebuild = TRUE; } elseif ($this->session->has('uc_cart_order_rebuild')) { // Or, if the cart has changed, then remove old products and line items. $result = \Drupal::entityQuery('uc_order_product')->condition('order_id', $order->id())->execute(); if (!empty($result)) { $storage = $this->entityTypeManager()->getStorage('uc_order_product'); $entities = $storage->loadMultiple(array_keys($result)); $storage->delete($entities); } uc_order_delete_line_item($order->id(), TRUE); $rebuild = TRUE; } if ($rebuild) { // Copy the cart contents to the cart order. $order->products = array(); foreach ($items as $item) { $order->products[] = $item->toOrderProduct(); } $this->session->remove('uc_cart_order_rebuild'); } elseif (!uc_order_product_revive($order->products)) { drupal_set_message($this->t('Some of the products in this order are no longer available.'), 'error'); return $this->redirect('uc_cart.cart'); } } $min = $cart_config->get('minimum_subtotal'); if ($min > 0 && $order->getSubtotal() < $min) { drupal_set_message($this->t('The minimum order subtotal for checkout is @min.', ['@min' => uc_currency_format($min)]), 'error'); return $this->redirect('uc_cart.cart'); } // Trigger the "Customer starts checkout" hook and event. $this->moduleHandler()->invokeAll('uc_cart_checkout_start', array($order)); // rules_invoke_event('uc_cart_checkout_start', $order); return $this->formBuilder()->getForm('Drupal\\uc_cart\\Form\\CheckoutForm', $order); }
public function testPackagesUI() { $this->drupalLogin($this->adminUser); $method = $this->createPaymentMethod('other'); // Process an anonymous, shippable order. $order = Order::create(['uid' => 0, 'primary_email' => $this->randomString() . '@example.org', 'payment_method' => $method['id']]); // Add three more products to use for our tests. $products = array(); for ($i = 1; $i <= 4; $i++) { $product = $this->createProduct(array('uid' => $this->adminUser->id(), 'promote' => 0)); $order->products[$i] = OrderProduct::create(array('nid' => $product->nid->target_id, 'title' => $product->title->value, 'model' => $product->model, 'qty' => 1, 'cost' => $product->cost->value, 'price' => $product->price->value, 'weight' => $product->weight, 'data' => [])); $order->products[$i]->data->shippable = 1; } $order->save(); $order = Order::load($order->id()); uc_payment_enter($order->id(), $method['id'], $order->getTotal()); // Order with 4 products shippable products. (where do we test not-shippable?) // Check all, make one package, verify we're on packages page with only one packge. // Try create package link, should see there are no products message. // Delete package. // Check all, make shipment, verify we're on packages page with N packages. // Delete packages. // How does Sep work? how does making 2 packages out of 4 products work? // Check all, cancel, verify we're on order page. // After packages made and check for # (check make one and make shipment, use sep. as well) // Can use edit/delete actions to package then start over with the same order. // and check for full table at /packages and check for action on /packages page, // goto shipments tab and look for No shipments have been made for this order. as well as a list of all the packages. // // Test presence and operation of package operation on order admin View. // $this->drupalGet('admin/store/orders/view'); $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/packages'); // Test action. $this->clickLink(t('Package')); $this->assertResponse(200); $this->assertText('This order\'s products have not been organized into packages.', 'Package action found.'); // Now package the products in this order. $this->drupalGet('admin/store/orders/' . $order->id() . '/packages'); $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new'); // First time through we'll be verbose - skip this on subsequent tests. foreach ($order->products as $sequence => $item) { $this->assertText($item->title->value, 'Product title found.'); $this->assertText($item->model->value, 'Product SKU found.'); $this->assertFieldByName('shipping_types[small_package][table][' . $sequence . '][checked]', 0, 'Product is available for packaging.'); } // Select all products and test the "Cancel" button. $this->drupalPostForm(NULL, array('shipping_types[small_package][table][1][checked]' => 1, 'shipping_types[small_package][table][2][checked]' => 1, 'shipping_types[small_package][table][3][checked]' => 1, 'shipping_types[small_package][table][4][checked]' => 1), t('Cancel')); // Go back to Packages tab and try something else. $this->assertUrl('admin/store/orders/' . $order->id()); $this->clickLink(t('Packages')); $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new'); $this->assertText('This order\'s products have not been organized into packages.', 'Package action found.'); // Now test the "Create one package" button without selecting anything. $this->drupalPostForm(NULL, array(), t('Create one package')); $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new'); $this->assertText('Packages must contain at least one product.', 'Validation that there must be products in a package.'); // Now test the "Create one package" button with all products selected. $this->drupalPostForm(NULL, array('shipping_types[small_package][table][1][checked]' => 1, 'shipping_types[small_package][table][2][checked]' => 1, 'shipping_types[small_package][table][3][checked]' => 1, 'shipping_types[small_package][table][4][checked]' => 1), t('Create one package')); // Check that we're now on the package list page. $this->assertUrl('admin/store/orders/' . $order->id() . '/packages'); foreach ($order->products as $sequence => $item) { $this->assertText($item->qty->value . ' x ' . $item->model->value, 'Product quantity x SKU found.'); } // The "Create packages" local action should now be available too. $this->assertLink(t('Create packages')); $this->clickLink(t('Create packages')); $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new'); // But we've already packaged everything... $this->assertText('There are no products available for this type of package.', 'Create packages local action found.'); // // Test "Ship", "Edit", and "Delete" operations for this package. // // First "Ship". $this->drupalGet('admin/store/orders/' . $order->id() . '/packages'); $this->assertLink(t('Ship')); $this->clickLink(t('Ship')); $this->assertUrl('admin/store/orders/' . $order->id() . '/shipments/new?pkgs=1'); foreach ($order->products as $sequence => $item) { $this->assertText($item->qty->value . ' x ' . $item->model->value, 'Product quantity x SKU found.'); } // Second, "Edit". $this->drupalGet('admin/store/orders/' . $order->id() . '/packages'); // (Use Href to distinguish Edit operation from Edit tab.) $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/packages/1/edit'); $this->drupalGet('admin/store/orders/' . $order->id() . '/packages/1/edit'); // We're editing the package we already made, so all the // products should be checked. foreach ($order->products as $sequence => $item) { $this->assertFieldByName('products[' . $sequence . '][checked]', 1, 'Product is available for packaging.'); } // Third, "Delete". $this->drupalGet('admin/store/orders/' . $order->id() . '/packages'); $this->assertLink(t('Delete')); $this->clickLink(t('Delete')); // Delete takes us to confirm page. $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/1/delete'); $this->assertText('The products it contains will be available for repackaging.', 'Deletion confirm question found.'); // "Cancel" returns to the package list page. $this->clickLink(t('Cancel')); $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/packages'); // Again with the "Delete". $this->clickLink(t('Delete')); $this->drupalPostForm(NULL, array(), t('Delete')); // Delete returns to new packages page with all packages unchecked. $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new'); $this->assertText('Package 1 has been deleted.', 'Package deleted message found.'); foreach ($order->products as $sequence => $item) { $this->assertFieldByName('shipping_types[small_package][table][' . $sequence . '][checked]', 0, 'Product is available for packaging.'); } // Back to no packages. Now test making more than one package. // Now test the "Create one package" button with all products selected. $this->drupalPostForm(NULL, array('shipping_types[small_package][table][1][checked]' => 1, 'shipping_types[small_package][table][2][checked]' => 1), t('Create one package')); // Check that we're now on the package list page. $this->assertUrl('admin/store/orders/' . $order->id() . '/packages'); $this->assertText($order->products[1]->qty->value . ' x ' . $order->products[1]->model->value, 'Product quantity x SKU found.'); $this->assertText($order->products[2]->qty->value . ' x ' . $order->products[2]->model->value, 'Product quantity x SKU found.'); $this->assertNoText($order->products[3]->qty->value . ' x ' . $order->products[3]->model->value, 'Product quantity x SKU not found.'); $this->assertNoText($order->products[4]->qty->value . ' x ' . $order->products[4]->model->value, 'Product quantity x SKU not found.'); // Use "Create packages" to create a second package. $this->assertLink(t('Create packages')); $this->clickLink(t('Create packages')); $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new'); $this->assertNoText($order->products[1]->model->value, 'Product SKU not found.'); $this->assertNoText($order->products[2]->model->value, 'Product SKU not found.'); $this->assertText($order->products[3]->model->value, 'Product SKU found.'); $this->assertText($order->products[4]->model->value, 'Product SKU found.'); $this->drupalPostForm(NULL, array('shipping_types[small_package][table][3][checked]' => 1, 'shipping_types[small_package][table][4][checked]' => 1), t('Create one package')); $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/packages'); foreach ($order->products as $sequence => $item) { $this->assertText($item->qty->value . ' x ' . $item->model->value, 'Product quantity x SKU found.'); } // How do we test for two packages? Look for two "Ship" links $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/shipments/new?pkgs=2'); $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/shipments/new?pkgs=3'); // Now delete both packages. $this->clickLink(t('Delete')); $this->drupalPostForm(NULL, array(), t('Delete')); $this->assertText('Package 2 has been deleted.', 'Package deleted message found.'); // There's still one left to delete... $this->clickLink(t('Delete')); $this->drupalPostForm(NULL, array(), t('Delete')); $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new'); $this->assertText('Package 3 has been deleted.', 'Package deleted message found.'); // Back to no packages. Now test "Make packages" button. $this->drupalPostForm(NULL, array('shipping_types[small_package][table][1][checked]' => 1, 'shipping_types[small_package][table][2][checked]' => 1, 'shipping_types[small_package][table][3][checked]' => 1, 'shipping_types[small_package][table][4][checked]' => 1), t('Make packages')); // Check that we're now on the package list page. $this->assertUrl('admin/store/orders/' . $order->id() . '/packages'); foreach ($order->products as $sequence => $item) { $this->assertText($item->qty->value . ' x ' . $item->model->value, 'Product quantity x SKU found.'); } }
protected function ucCreateOrder($customer) { $order = Order::create(array('uid' => $customer->id())); $order->save(); uc_order_comment_save($order->id(), 0, t('Order created programmatically.'), 'admin'); $order_ids = \Drupal::entityQuery('uc_order')->condition('order_id', $order->id())->execute(); $this->assertTrue(in_array($order->id(), $order_ids), SafeMarkup::format('Found order ID @order_id', ['@order_id' => $order->id()])); $country_manager = \Drupal::service('country_manager'); $country = array_rand($country_manager->getEnabledList()); $zones = $country_manager->getZoneList($country); $delivery_address = new Address(); $delivery_address->first_name = $this->randomMachineName(12); $delivery_address->last_name = $this->randomMachineName(12); $delivery_address->street1 = $this->randomMachineName(12); $delivery_address->street2 = $this->randomMachineName(12); $delivery_address->city = $this->randomMachineName(12); $delivery_address->zone = array_rand($zones); $delivery_address->postal_code = mt_rand(10000, 99999); $delivery_address->country = $country; $billing_address = new Address(); $billing_address->first_name = $this->randomMachineName(12); $billing_address->last_name = $this->randomMachineName(12); $billing_address->street1 = $this->randomMachineName(12); $billing_address->street2 = $this->randomMachineName(12); $billing_address->city = $this->randomMachineName(12); $billing_address->zone = array_rand($zones); $billing_address->postal_code = mt_rand(10000, 99999); $billing_address->country = $country; $order->setAddress('delivery', $delivery_address)->setAddress('billing', $billing_address)->save(); // Force the order to load from the DB instead of the entity cache. $db_order = \Drupal::entityManager()->getStorage('uc_order')->loadUnchanged($order->id()); // Compare delivery and billing addresses to those loaded from the database. $db_delivery_address = $db_order->getAddress('delivery'); $db_billing_address = $db_order->getAddress('billing'); $this->assertEqual($delivery_address, $db_delivery_address, 'Delivery address is equal to delivery address in database.'); $this->assertEqual($billing_address, $db_billing_address, 'Billing address is equal to billing address in database.'); return $order; }