| @@ -5,9 +5,8 @@ | ||
| 5 | 5 | * @author Paul Kilmurray <paul@kilbot.com> |
| 6 | 6 | * |
| 7 | 7 | * @see https://wcpos.com |
| 8 | 8 | * |
| 9 | - * @extends WC_Payment_Gateway | |
| 10 | 9 | * @package WCPOS\WooCommercePOS |
| 11 | 10 | */ |
| 12 | 11 | |
| 13 | 12 | namespace WCPOS\WooCommercePOS\Gateways; |
| @@ -13,14 +12,16 @@ | ||
| 13 | 12 | namespace WCPOS\WooCommercePOS\Gateways; |
| 14 | 13 | |
| 15 | 14 | use WC_Order; |
| 16 | 15 | use WC_Order_Item_Fee; |
| 17 | -use WC_Payment_Gateway; | |
| 16 | +use WCPOS\WooCommercePOS\Payments\Abstract_POS_Gateway; | |
| 17 | +use WCPOS\WooCommercePOS\Services\Order_Notes; | |
| 18 | +use WP_REST_Request; | |
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | 21 | * Cash class. |
| 21 | 22 | */ |
| 22 | -class Cash extends WC_Payment_Gateway { | |
| 23 | +class Cash extends Abstract_POS_Gateway { | |
| 23 | 24 | /** |
| 24 | 25 | * Constructor for the gateway. |
| 25 | 26 | */ |
| 26 | 27 | public function __construct() { |
| @@ -41,13 +42,9 @@ | ||
| 41 | 42 | 'process_admin_options', |
| 42 | 43 | ) |
| 43 | 44 | ); |
| 44 | 45 | add_action( 'woocommerce_thankyou_pos_cash', array( $this, 'calculate_change' ) ); |
| 45 | - add_filter( 'wcpos_payment_gateway_provider', array( $this, 'wcpos_provider' ), 10, 3 ); | |
| 46 | - add_filter( 'wcpos_payment_gateway_pos_type', array( $this, 'wcpos_pos_type' ), 10, 3 ); | |
| 47 | - add_filter( 'wcpos_payment_gateway_provider_data', array( $this, 'wcpos_provider_data' ), 10, 3 ); | |
| 48 | - add_filter( 'wcpos_payment_gateway_bootstrap', array( $this, 'wcpos_bootstrap' ), 10, 4 ); | |
| 49 | - add_filter( 'wcpos_process_checkout_action_' . $this->id, array( $this, 'wcpos_process_checkout_action' ), 10, 5 ); | |
| 46 | + $this->register_pos_gateway_contract_hooks(); | |
| 50 | 47 | } |
| 51 | 48 | |
| 52 | 49 | /** |
| 53 | 50 | * Get payment details. |
| @@ -191,18 +188,23 @@ | ||
| 191 | 188 | * |
| 192 | 189 | * @param int $order_id Order ID. |
| 193 | 190 | */ |
| 194 | 191 | public function calculate_change( $order_id ): void { |
| 192 | + $order = wc_get_order( $order_id ); | |
| 193 | + if ( ! $order ) { | |
| 194 | + return; | |
| 195 | + } | |
| 196 | + | |
| 195 | 197 | $message = ''; |
| 196 | - $tendered = get_post_meta( $order_id, '_pos_cash_amount_tendered', true ); | |
| 197 | - $change = get_post_meta( $order_id, '_pos_cash_change', true ); | |
| 198 | + $tendered = $order->get_meta( '_pos_cash_amount_tendered' ); | |
| 199 | + $change = $order->get_meta( '_pos_cash_change' ); | |
| 198 | 200 | |
| 199 | 201 | // construct message. |
| 200 | 202 | if ( $tendered && $change ) { |
| 201 | 203 | $message = /* translators: Order note label for the cash amount received from the customer at checkout. */ __( 'Amount Tendered', 'woocommerce-pos' ) . ': '; |
| 202 | - $message .= wc_price( $tendered ) . '<br>'; | |
| 204 | + $message .= wc_price( $tendered, array( 'currency' => $order->get_currency() ) ) . '<br>'; | |
| 203 | 205 | $message .= /* translators: Money returned to the customer after a cash payment. */ _x( 'Change', 'Money returned from cash sale', 'woocommerce-pos' ) . ': '; |
| 204 | - $message .= wc_price( $change ); | |
| 206 | + $message .= wc_price( $change, array( 'currency' => $order->get_currency() ) ); | |
| 205 | 207 | } |
| 206 | 208 | |
| 207 | 209 | echo wp_kses_post( $message ); |
| 208 | 210 | } |
| @@ -207,91 +209,28 @@ | ||
| 207 | 209 | echo wp_kses_post( $message ); |
| 208 | 210 | } |
| 209 | 211 | |
| 210 | 212 | /** |
| 211 | - * POS provider family. | |
| 213 | + * Provider family identifier. | |
| 212 | 214 | * |
| 213 | - * @param string $provider Provider value. | |
| 214 | - * @param mixed $gateway Gateway object. | |
| 215 | - * @param mixed $request REST request. | |
| 215 | + * @param WP_REST_Request|null $request Request object. | |
| 216 | 216 | */ |
| 217 | - public function wcpos_provider( $provider, $gateway, $request = null ) { | |
| 218 | - if ( $gateway instanceof WC_Payment_Gateway && $this->id === $gateway->id ) { | |
| 219 | - return 'wcpos'; | |
| 220 | - } | |
| 221 | - | |
| 222 | - return $provider; | |
| 217 | + public function get_pos_provider( ?WP_REST_Request $request = null ): string { | |
| 218 | + return 'wcpos'; | |
| 223 | 219 | } |
| 224 | 220 | |
| 225 | 221 | /** |
| 226 | - * POS type. | |
| 222 | + * Process a POS checkout action for cash. | |
| 227 | 223 | * |
| 228 | - * @param string $pos_type POS type. | |
| 229 | - * @param mixed $gateway Gateway object. | |
| 230 | - * @param mixed $request REST request. | |
| 231 | - */ | |
| 232 | - public function wcpos_pos_type( $pos_type, $gateway, $request = null ) { | |
| 233 | - if ( $gateway instanceof WC_Payment_Gateway && $this->id === $gateway->id ) { | |
| 234 | - return 'manual'; | |
| 235 | - } | |
| 236 | - | |
| 237 | - return $pos_type; | |
| 238 | - } | |
| 239 | - | |
| 240 | - /** | |
| 241 | - * Non-secret provider data. | |
| 224 | + * @param array $state Checkout state. | |
| 225 | + * @param string $action Checkout action. | |
| 226 | + * @param array $payment_data Payment data. | |
| 227 | + * @param WC_Order $order Order object. | |
| 228 | + * @param WP_REST_Request|null $request Request object. | |
| 242 | 229 | * |
| 243 | - * @param array $provider_data Provider data. | |
| 244 | - * @param mixed $gateway Gateway object. | |
| 245 | - * @param mixed $request REST request. | |
| 230 | + * @return array|\WP_Error | |
| 246 | 231 | */ |
| 247 | - public function wcpos_provider_data( $provider_data, $gateway, $request = null ) { | |
| 248 | - if ( $gateway instanceof WC_Payment_Gateway && $this->id === $gateway->id ) { | |
| 249 | - return array(); | |
| 250 | - } | |
| 251 | - | |
| 252 | - return $provider_data; | |
| 253 | - } | |
| 254 | - | |
| 255 | - /** | |
| 256 | - * Bootstrap response for POS cash. | |
| 257 | - * | |
| 258 | - * @param array $response Bootstrap response. | |
| 259 | - * @param string $gateway_id Gateway ID. | |
| 260 | - * @param array $context Bootstrap context. | |
| 261 | - * @param mixed $request REST request. | |
| 262 | - */ | |
| 263 | - public function wcpos_bootstrap( $response, $gateway_id, $context = array(), $request = null ) { | |
| 264 | - if ( $this->id === $gateway_id ) { | |
| 265 | - return array( | |
| 266 | - 'gateway_id' => $gateway_id, | |
| 267 | - 'status' => 'ready', | |
| 268 | - 'expires_at' => null, | |
| 269 | - 'provider_data' => array(), | |
| 270 | - ); | |
| 271 | - } | |
| 272 | - | |
| 273 | - return $response; | |
| 274 | - } | |
| 275 | - | |
| 276 | - /** | |
| 277 | - * Handle POS checkout contract. | |
| 278 | - * | |
| 279 | - * @param array|\WP_Error $state Checkout state. | |
| 280 | - * @param string $action Checkout action. | |
| 281 | - * @param array $payment_data Payment data. | |
| 282 | - * @param WC_Order $order Order object. | |
| 283 | - * @param mixed $request REST request. | |
| 284 | - */ | |
| 285 | - public function wcpos_process_checkout_action( $state, $action, $payment_data, $order, $request = null ) { | |
| 286 | - if ( is_wp_error( $state ) ) { | |
| 287 | - return $state; | |
| 288 | - } | |
| 289 | - | |
| 290 | - if ( ( $state['gateway_id'] ?? '' ) !== $this->id ) { | |
| 291 | - return $state; | |
| 292 | - } | |
| 293 | - | |
| 232 | + public function process_pos_checkout_action( array $state, string $action, array $payment_data, WC_Order $order, ?WP_REST_Request $request = null ) { | |
| 294 | 233 | if ( 'cancel' === $action ) { |
| 295 | 234 | $state['status'] = 'cancelled'; |
| 296 | 235 | return $state; |
| 297 | 236 | } |
| @@ -345,8 +284,9 @@ | ||
| 345 | 284 | $order->update_meta_data( '_pos_cash_amount_tendered', $tendered ); |
| 346 | 285 | $order->update_meta_data( '_pos_cash_change', $change ); |
| 347 | 286 | |
| 348 | 287 | if ( $tendered >= $order->get_total() ) { |
| 288 | + Order_Notes::add_cash_note( $order, $tendered, $change ); | |
| 349 | 289 | $order->payment_complete(); |
| 350 | 290 | return true; |
| 351 | 291 | } |
| 352 | 292 | |
| @@ -373,8 +313,9 @@ | ||
| 373 | 313 | $fee->save(); |
| 374 | 314 | |
| 375 | 315 | $order->add_item( $fee ); |
| 376 | 316 | $order->set_total( wc_format_decimal( floatval( $order->get_total() ) - floatval( $tendered ) ) ); |
| 317 | + Order_Notes::add_cash_note( $order, $tendered, $change ); | |
| 377 | 318 | $order->update_status( 'wc-pos-partial' ); |
| 378 | 319 | |
| 379 | 320 | return true; |
| 380 | 321 | } |