Commit 8ec5cdee authored by acrollet's avatar acrollet Committed by GitHub

Merge pull request #7 from Roomify/payment

Add commerce integration with payment functionality
parents d2c780ce b9e30fba
......@@ -97,6 +97,10 @@ function bee_uninstall() {
Drupal::configFactory()->getEditable('field.field.node.' . $node_type->id() . '.field_open_hours')->delete();
Drupal::configFactory()->getEditable('field.field.node.' . $node_type->id() . '.field_use_open_hours')->delete();
Drupal::configFactory()->getEditable('field.field.node.' . $node_type->id() . '.field_product')->delete();
Drupal::configFactory()->getEditable('field.field.node.' . $node_type->id() . '.field_price')->delete();
Drupal::configFactory()->getEditable('field.field.node.' . $node_type->id() . '.field_price_frequency')->delete();
}
Drupal::configFactory()->getEditable('field.storage.node.field_availability_daily')->delete();
......@@ -105,6 +109,10 @@ function bee_uninstall() {
Drupal::configFactory()->getEditable('field.storage.node.field_open_hours')->delete();
Drupal::configFactory()->getEditable('field.storage.node.field_use_open_hours')->delete();
Drupal::configFactory()->getEditable('field.storage.node.field_product')->delete();
Drupal::configFactory()->getEditable('field.storage.node.field_price')->delete();
Drupal::configFactory()->getEditable('field.storage.node.field_price_frequency')->delete();
$states = [
'bee_daily_available',
'bee_daily_not_available',
......
This diff is collapsed.
......@@ -9,3 +9,12 @@ services:
arguments: ['@entity.manager']
tags:
- { name: access_check, applies_to: _bee_add_reservation_access }
bee.saleprice_resolver:
class: Drupal\bee\Resolvers\SalepriceResolver
arguments: ['@request_stack']
tags:
- { name: commerce_price.price_resolver, priority: 600 }
bee.order_subscriber:
class: Drupal\bee\EventSubscriber\OrderEventSubscriber
tags:
- { name: event_subscriber }
<?php
namespace Drupal\bee\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
class OrderEventSubscriber implements EventSubscriberInterface {
/**
* Constructs a new OrderEventSubscriber object.
*/
public function __construct() {
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = ['commerce_order.place.pre_transition' => 'finalizeCart'];
return $events;
}
/**
* Finalizes the cart when the order is placed.
*
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
* The workflow transition event.
*/
public function finalizeCart(WorkflowTransitionEvent $event) {
$order = $event->getEntity();
foreach ($order->getItems() as $item) {
if ($booking = $item->get('field_booking')->entity) {
$node = $item->get('field_node')->entity;
$start_date = new \DateTime($booking->get('booking_start_date')->value);
$end_date = new \DateTime($booking->get('booking_end_date')->value);
$bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee');
if ($bee_settings['bookable_type'] == 'daily') {
$booked_state = bat_event_load_state_by_machine_name('bee_daily_booked');
$event = bat_event_create(['type' => 'availability_daily']);
$event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_date->format('Y-m-d\TH:i:00'),
];
$event->set('event_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id());
}
else {
$booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked');
$event = bat_event_create(['type' => 'availability_hourly']);
$event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_date->format('Y-m-d\TH:i:00'),
];
$event->set('event_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id());
}
$available_units = $this->getAvailableUnits($node, $start_date, $end_date);
$event->set('event_bat_unit_reference', reset($available_units));
$event->save();
$booking->set('booking_event_reference', $event->id());
$booking->save();
}
}
}
/**
* Get available Units.
*
* @param $node
* @param $start_date
* @param $end_date
*
* return array
*/
protected function getAvailableUnits($node, $start_date, $end_date) {
$bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee');
$units_ids = [];
foreach ($node->get('field_availability_' . $bee_settings['bookable_type']) as $unit) {
$units_ids[] = $unit->entity->id();
}
$temp_end_date = clone($end_date);
$temp_end_date->sub(new \DateInterval('PT1M'));
if ($bee_settings['bookable_type'] == 'daily') {
$available_units_ids = bat_event_get_matching_units($start_date, $temp_end_date, ['bee_daily_available'], $bee_settings['type_id'], 'availability_daily');
}
else {
$available_units_ids = bat_event_get_matching_units($start_date, $temp_end_date, ['bee_hourly_available'], $bee_settings['type_id'], 'availability_hourly');
}
return array_intersect($units_ids, $available_units_ids);
}
}
......@@ -164,48 +164,91 @@ class AddReservationForm extends FormBase {
$bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee');
$units_ids = [];
foreach ($node->get('field_availability_' . $bee_settings['bookable_type']) as $unit) {
$units_ids[] = $unit->entity->id();
}
if ($bee_settings['bookable_type'] == 'daily') {
$start_date = new \DateTime($start_date);
$end_date = new \DateTime($end_date);
$booked_state = bat_event_load_state_by_machine_name('bee_daily_booked');
$event = bat_event_create(['type' => 'availability_daily']);
$event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_date->format('Y-m-d\TH:i:00'),
];
$event->set('event_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id());
}
else {
$start_date = new \DateTime($start_date->format('Y-m-d H:i'));
$end_date = new \DateTime($end_date->format('Y-m-d H:i'));
}
if ($bee_settings['payment']) {
$booking = bat_booking_create([
'type' => 'bee',
'label' => $node->label(),
]);
$booking->set('booking_start_date', $start_date->format('Y-m-d H:i:s'));
$booking->set('booking_end_date', $end_date->format('Y-m-d H:i:s'));
$booking->save();
$product = $node->get('field_product')->entity;
$stores = $product->getStores();
$store = reset($stores);
$booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked');
$variations = $product->getVariations();
$product_variation = reset($variations);
$event = bat_event_create(['type' => 'availability_hourly']);
$event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_date->format('Y-m-d\TH:i:00'),
];
$event->set('event_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id());
$cart_manager = \Drupal::service('commerce_cart.cart_manager');
$cart_provider = \Drupal::service('commerce_cart.cart_provider');
$cart = $cart_provider->getCart('default', $store);
if (!$cart) {
$cart = $cart_provider->createCart('default', $store);
}
else {
$cart_manager->emptyCart($cart);
}
$order_item = \Drupal::entityManager()->getStorage('commerce_order_item')->create([
'title' => $node->label(),
'type' => 'bee',
'purchased_entity' => $product_variation->id(),
'quantity' => 1,
'unit_price' => $product_variation->getPrice(),
]);
$order_item->set('field_booking', $booking);
$order_item->set('field_node', $node);
$order_item->save();
$cart_manager->addOrderItem($cart, $order_item);
$form_state->setRedirect('commerce_checkout.form', ['commerce_order' => $cart->id()]);
}
else {
if ($bee_settings['bookable_type'] == 'daily') {
$booked_state = bat_event_load_state_by_machine_name('bee_daily_booked');
$event = bat_event_create(['type' => 'availability_daily']);
$event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_date->format('Y-m-d\TH:i:00'),
];
$event->set('event_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id());
}
else {
$booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked');
$event = bat_event_create(['type' => 'availability_hourly']);
$event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_date->format('Y-m-d\TH:i:00'),
];
$event->set('event_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id());
}
$available_units = $this->getAvailableUnits($values);
$available_units = $this->getAvailableUnits($values);
$event->set('event_bat_unit_reference', reset($available_units));
$event->save();
$event->set('event_bat_unit_reference', reset($available_units));
$event->save();
drupal_set_message(t('Reservation created!'));
drupal_set_message(t('Reservation created!'));
$form_state->setRedirect('entity.node.canonical', ['node' => $node->id()]);
$form_state->setRedirect('entity.node.canonical', ['node' => $node->id()]);
}
}
/**
......
<?php
namespace Drupal\bee\Resolvers;
use Drupal\node\Entity\Node;
use Drupal\commerce\Context;
use Drupal\commerce\PurchasableEntityInterface;
use Drupal\commerce_price\Price;
use Drupal\commerce_price\Resolver\PriceResolverInterface;
/**
* Class SalepriceResolver
*
* @package Drupal\bee\Resolvers
*/
class SalepriceResolver implements PriceResolverInterface {
/**
* {@inheritdoc}
*/
public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {
if ($entity->bundle() != 'bee') {
return;
}
$cart_provider = \Drupal::service('commerce_cart.cart_provider');
$stores = $entity->getProduct()->getStores();
$store = reset($stores);
if ($cart = $cart_provider->getCart('default', $store)) {
$order_items = $cart->getItems();
foreach ($order_items as $order_item) {
if ($order_item->bundle() == 'bee') {
if ($order_item->getPurchasedEntityId() == $entity->id()) {
$query = \Drupal::entityQuery('node')
->condition('field_product', $entity->getProductId());
$nids = $query->execute();
$node = Node::load(reset($nids));
$bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee');
$booking = $order_item->get('field_booking')->entity;
$start_date = new \DateTime($booking->get('booking_start_date')->value);
$end_date = new \DateTime($booking->get('booking_end_date')->value);
$interval = $start_date->diff($end_date);
$reservation_context = [
'order_item' => $order_item,
'booking' => $booking,
'node' => $node,
];
$base_price = $node->get('field_price')->number;
$currency_code = $node->get('field_price')->currency_code;
if ($bee_settings['bookable_type'] == 'daily') {
$days = $interval->days;
$amount = number_format($base_price * $days, 2, '.', '');
}
else {
$field_price_frequency = $node->get('field_price_frequency')->value;
if ($field_price_frequency == 'hour') {
$hours = ($interval->days * 24) + $interval->h;
$amount = number_format($base_price * $hours, 2, '.', '');
}
else {
$minutes = ($interval->days * 24 * 60) + ($interval->h * 60) + $interval->i;
$amount = number_format($base_price * $minutes, 2, '.', '');
}
}
$price = new Price($amount, $currency_code);
\Drupal::moduleHandler()->alter('bee_reservation_price', $price, $reservation_context);
return $price;
}
}
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment