Home /Tutorials /Making a purchase for a client
Making a purchase for a client

In this tutorial, we’ll start by identifying the product the client wants to buy from the store.

To identify a product for the client to buy, we can first get a list of available products. When selecting an item, we’ll take note of the id_sale value showing the general category of item along with the k_id value that acts as an identifier within that category. The same k_id value can be used with multiple id_sale values, which means that both are necessary to identify a product.

// Gets a list of products from the store.
$o_catalog_model = new \WellnessLiving\Wl\Catalog\StaffApp\CatalogList\CatalogListModel($o_config);
$o_catalog_model->cookieSet($o_notepad->cookieGet()); // Keep cookies to keep session.
$o_catalog_model->k_business='3';
$o_catalog_model->k_location='3';
// Perform the request.
$o_catalog_model->get();

// Print each product's k_id, id_sale, and name.
foreach ($o_catalog_model->a_shop_product as $a_product)
{

  if($a_product['id_sale'] === WlSaleSid::PROMOTION_SERVICE)
    echo 'Appointment Membership: ';
  else if($a_product['id_sale'] === WlSaleSid::PROMOTION_CLASS)
    echo 'Class Membership: ';

  echo $a_product['k_id'].' '.$a_product['id_sale'].' '.$a_product['text_title']."\n";
}

After a product is selected, we’ll determine the total price the client will have to pay. This is the price including taxes and any other service fees or charges.

// Get the list of store categories.
$o_cart_model = new \WellnessLiving\Wl\Catalog\StaffApp\CatalogCart\CatalogCartModel($o_config);
$o_cart_model->cookieSet($o_notepad->cookieGet()); // Keep cookies to keep session.
$uid = 4;
$o_cart_model->a_item=[
 [
   'i_quantity' => 1,
   'id_sale' => 1,
   'k_id' => 1
 ]
];
$o_cart_model->is_check_cart_item=true;
$o_cart_model->k_business='3';
$o_cart_model->k_location='3';
$o_cart_model->uid_current=$uid;
$o_cart_model->uid_customer=$uid;
// Perform the request.
$o_cart_model->get();

echo "\n\n\n";
echo "Total price: ". $o_cart_model->m_total."\n"

We’ll also need to get the client’s address. This is needed to complete a payment with a credit card.

// Get the client's address.
$o_info_model = new \WellnessLiving\Wl\Pay\Address\AddressModel($o_config);
$o_info_model->cookieSet($o_notepad->cookieGet()); // Keep cookies to keep session.
$o_info_model->id_pay_owner=\WellnessLiving\WlPayOwnerSid::USER;
$o_info_model->k_id='4';
// Perform the request.
$o_info_model->get();

// Print address information.
if($o_info_model->a_pay_address && $o_info_model->a_pay_address[0])
 var_dump($o_info_model->a_pay_address[0]);

Once we’ve determined the product’s id_sale, k_id, and total price, we can move to the next step. We’ll use the m_total amount from the previous step as the f_total value in this next call. To make a purchase online, we’ll need to use a credit card. Upon success, we’ll receive a purchase key (k_purchase).

// Perform the purchase.
$o_cart_model = new \WellnessLiving\Wl\Catalog\Payment\PaymentModel($o_config);
$o_cart_model->cookieSet($o_notepad->cookieGet()); // Keep cookies to keep session.

$o_cart_model->a_pay_form=[
 \WellnessLiving\WlPayMethodSid::ECOMMERCE => [
   'a_pay_card' => [
     'a_pay_address' => [
       'is_new' => 0,
       'k_geo_country' => 1,
       'k_geo_region' => 11,
       'k_pay_address' => 14,
       's_city' => 'New-York',
       's_name' => 'John',
       's_postal' => '12345',
       's_street1' => 'Baker str.',
       's_street2' => ''
     ],
     'i_csc' => '111',
     'i_month' => '10',
     'i_year' => '25',
     'is_new' => '1',
     'k_pay_bank' => null,
     's_number' => '4111111111111111'
   ],
   'f_amount' => '55.00',
   'is_enable_surcharge' => true,
   'm_surcharge' => '0.00',
   's_test' => 'q',
   'sid_pay_method' => 'ecommerce'
 ]
];

$o_cart_model->a_item = [
 [
   'i_quantity' => 1, // The number of sessions booked.
   'id_purchase_item' => WlPurchaseItemSid::PROMOTION,
   'id_sale' => \WellnessLiving\WlSaleSid::PROMOTION_CLASS,
   'k_id' => '1',
 ]
];

$o_cart_model->id_mode=\WellnessLiving\Wl\Mode\ModeSid::WEB_BACKEND;
$o_cart_model->is_staff=true;
$o_cart_model->k_business='3';
$o_cart_model->k_location='3';
$o_cart_model->uid=100;
// Perform the request
$o_cart_model->post();

echo "\n\n\n";
echo "Purchase key: ". $o_cart_model->k_purchase."\n";