| 1 |
<?php |
| 2 |
/** |
| 3 |
* WooCommerce Cart API |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Timetics\Core\Integrations\Woocommerce; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
use Timetics\Base\Api; |
| 13 |
use Timetics\Core\Appointments\Appointment; |
| 14 |
use Timetics\Utils\Singleton; |
| 15 |
|
| 16 |
/** |
| 17 |
* |
| 18 |
*/ |
| 19 |
class Cart_Api extends Api { |
| 20 |
use Singleton; |
| 21 |
|
| 22 |
/** |
| 23 |
* Store api namespace |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $namespace = 'timetics/v1'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Store rest base |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $rest_base = 'woocommerce'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Register rest routes |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function register_routes() { |
| 42 |
/** |
| 43 |
* Register route |
| 44 |
* |
| 45 |
* @var void |
| 46 |
*/ |
| 47 |
register_rest_route( |
| 48 |
$this->namespace, $this->rest_base . '/cart', [ |
| 49 |
[ |
| 50 |
'methods' => \WP_REST_Server::CREATABLE, |
| 51 |
'callback' => [$this, 'set_up_woocommerce'], |
| 52 |
'permission_callback' => function () { |
| 53 |
return true; |
| 54 |
}, |
| 55 |
], |
| 56 |
] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Add to cart booking |
| 62 |
* |
| 63 |
* @param Object $requst |
| 64 |
* |
| 65 |
* @return JSON |
| 66 |
*/ |
| 67 |
public function set_up_woocommerce( $requst ) { |
| 68 |
$data = json_decode( $requst->get_body(), true ); |
| 69 |
|
| 70 |
$booking_id = ! empty( $data['booking_id'] ) ? intval( $data['booking_id'] ) : 0; |
| 71 |
$token = ! empty( $data['security_token'] ) ? sanitize_text_field( $data['security_token'] ) : ''; |
| 72 |
|
| 73 |
$booking = new \Timetics\Core\Bookings\Booking( $booking_id ); |
| 74 |
// everything from the booking record itself once ownership is proven. |
| 75 |
if ( ! $booking_id || '' === $token || ! $booking->is_booking() ) { |
| 76 |
return new \WP_HTTP_Response( |
| 77 |
[ |
| 78 |
'success' => 0, |
| 79 |
'status_code' => 404, |
| 80 |
'message' => __( 'Invalid booking.', 'timetics' ), |
| 81 |
], |
| 82 |
404 |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
$stored = (string) $booking->get_security_token(); |
| 87 |
|
| 88 |
if ( '' === $stored || ! hash_equals( $stored, $token ) ) { |
| 89 |
return new \WP_HTTP_Response( |
| 90 |
[ |
| 91 |
'success' => 0, |
| 92 |
'status_code' => 403, |
| 93 |
'message' => __( 'Invalid booking token.', 'timetics' ), |
| 94 |
], |
| 95 |
403 |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
$meeting_id = (int) $booking->get_appointment(); |
| 100 |
$price = (int) $booking->get_total(); |
| 101 |
|
| 102 |
// Set session for timetics data for woocommerce. |
| 103 |
WC()->session->set( 'timetics_data', [ |
| 104 |
'booking_id' => $booking_id, |
| 105 |
'meeting_id' => $meeting_id, |
| 106 |
'price' => $price, |
| 107 |
] ); |
| 108 |
|
| 109 |
// Remove all items from cart. |
| 110 |
WC()->cart->empty_cart(); |
| 111 |
|
| 112 |
// Product Add to cart |
| 113 |
$this->add_to_cart(); |
| 114 |
|
| 115 |
$data = [ |
| 116 |
'success' => 1, |
| 117 |
'status_code' => 200, |
| 118 |
'data' => array_merge([ |
| 119 |
'checkout_url' => wc_get_checkout_url(), |
| 120 |
], WC()->session->get( 'timetics_data' ) ) |
| 121 |
]; |
| 122 |
|
| 123 |
return rest_ensure_response( $data ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Add to cart for woocommerce |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public function add_to_cart() { |
| 132 |
$session_data = WC()->session->get( 'timetics_data' ); |
| 133 |
|
| 134 |
if ( ! $session_data ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
$meeting_id = $session_data['meeting_id']; |
| 139 |
|
| 140 |
$meeting = new Appointment( $meeting_id ); |
| 141 |
$product_id = $meeting->get_wc_product_id(); |
| 142 |
$quantity = 1; |
| 143 |
|
| 144 |
if ( ! $product_id ) { |
| 145 |
$product = new Product(); |
| 146 |
$product_id = $product->create( $meeting ); |
| 147 |
} |
| 148 |
|
| 149 |
if ( ! $product_id ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
WC()->cart->add_to_cart( $product_id, $quantity ); |
| 154 |
} |
| 155 |
} |
| 156 |
|