*
* @see https://wcpos.com
*
* @package WCPOS\WooCommercePOS
*/
namespace WCPOS\WooCommercePOS\Gateways;
use WC_Order;
use WCPOS\WooCommercePOS\Payments\Abstract_POS_Gateway;
use WP_REST_Request;
/**
* Card class.
*/
class Card extends Abstract_POS_Gateway {
/**
* Constructor for the gateway.
*/
public function __construct() {
$this->id = 'pos_card';
$this->title = /* translators: POS payment gateway label shown during checkout. */ __( 'Card', 'woocommerce-pos' );
$this->description = '';
$this->icon = apply_filters( 'woocommerce_pos_card_icon', '' );
$this->has_fields = true;
$this->enabled = 'no';
$this->supports = array( 'products', 'refunds' );
// Actions.
// @phpstan-ignore-next-line -- Action hook type mismatch.
add_action(
'woocommerce_pos_update_options_payment_gateways_' . $this->id,
array(
$this,
'process_admin_options',
)
);
add_action( 'woocommerce_thankyou_pos_card', array( $this, 'calculate_cashback' ) );
$this->register_pos_gateway_contract_hooks();
}
/**
* Display the payment fields in the checkout.
*/
public function payment_fields(): void {
if ( $this->description ) {
echo '
' . wp_kses_post( $this->description ) . '
';
}
$currency_pos = get_option( 'woocommerce_currency_pos' );
if ( 'left' == $currency_pos || 'left_space' == $currency_pos ) {
$left_addon = '' . get_woocommerce_currency_symbol( get_woocommerce_currency() ) . '';
$right_addon = '';
} else {
$left_addon = '';
$right_addon = '' . get_woocommerce_currency_symbol( get_woocommerce_currency() ) . '';
}
echo '
';
}
/**
* Process the payment.
*
* @param int $order_id Order ID.
*
* @return array
*/
public function process_payment( $order_id ) {
// Check nonce.
if ( ! isset( $_POST['pos_card_payment_nonce_field'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['pos_card_payment_nonce_field'] ) ), 'pos_card_payment_nonce' ) ) {
wp_die( /* translators: Checkout error shown when card nonce validation fails. */ esc_html__( 'Nonce verification failed', 'woocommerce-pos' ) );
}
// get order object.
$order = wc_get_order( $order_id );
$cashback = 0; // Initialize with default value.
if ( isset( $_POST['pos-cashback'] ) && ! empty( $_POST['pos-cashback'] ) ) {
$cashback = wc_format_decimal( sanitize_text_field( wp_unslash( $_POST['pos-cashback'] ) ) );
}
$result = $this->apply_card_payment( $order, $cashback );
if ( is_wp_error( $result ) ) {
wc_add_notice( $result->get_error_message(), 'error' );
return array(
'result' => 'failure',
'redirect' => '',
);
}
// success.
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
);
}
/**
* Process a refund.
*
* Card refunds are handled manually, so just return true.
*
* @param int $order_id Order ID.
* @param float|null $amount Refund amount.
* @param string $reason Refund reason.
*
* @return bool
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
return true;
}
/**
* Calculate and display cashback.
*
* @param int $order_id Order ID.
*/
public function calculate_cashback( $order_id ): void {
$message = '';
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
$cashback = $order->get_meta( '_pos_card_cashback' );
// construct message.
if ( $cashback ) {
$message = /* translators: POS payment gateway label shown during checkout. */ __( 'Cashback', 'woocommerce-pos' ) . ': ';
$message .= wc_price( $cashback, array( 'currency' => $order->get_currency() ) );
}
echo wp_kses_post( $message );
}
/**
* Provider family identifier.
*
* @param WP_REST_Request|null $request Request object.
*/
public function get_pos_provider( ?WP_REST_Request $request = null ): string {
return 'wcpos';
}
/**
* Process a POS checkout action for card.
*
* @param array $state Checkout state.
* @param string $action Checkout action.
* @param array $payment_data Payment data.
* @param WC_Order $order Order object.
* @param WP_REST_Request|null $request Request object.
*
* @return array|\WP_Error
*/
public function process_pos_checkout_action( array $state, string $action, array $payment_data, WC_Order $order, ?WP_REST_Request $request = null ) {
if ( 'cancel' === $action ) {
$state['status'] = 'cancelled';
return $state;
}
if ( 'start' !== $action ) {
return $state;
}
$cashback = isset( $payment_data['cashback_amount'] ) && '' !== $payment_data['cashback_amount']
? wc_format_decimal( $payment_data['cashback_amount'] )
: 0;
$result = $this->apply_card_payment( $order, $cashback );
if ( is_wp_error( $result ) ) {
return $result;
}
$state['status'] = 'completed';
$state['provider_data'] = array(
'cashback' => $order->get_meta( '_pos_card_cashback' ),
);
return $state;
}
/**
* Apply card payment changes to an order.
*
* @param \WC_Order $order Order object.
* @param float|int|string $cashback Cashback amount.
*
* @return true|\WP_Error
*/
private function apply_card_payment( $order, $cashback ) {
$cashback = wc_format_decimal( $cashback );
if ( (float) $cashback < 0 ) {
return new \WP_Error(
'wcpos_invalid_cashback_amount',
__( 'Cashback amount must be zero or greater.', 'woocommerce-pos' ),
array( 'status' => 400 )
);
}
$order->set_payment_method( $this->id );
$order->set_payment_method_title( $this->title );
if ( 0 !== $cashback && '0' !== (string) $cashback ) {
$order->update_meta_data( '_pos_card_cashback', $cashback );
$item_id = wc_add_order_item(
$order->get_id(),
array(
'order_item_name' => /* translators: Fee line-item name for cashback charged on card payment. */ __( 'Cashback', 'woocommerce-pos' ),
'order_item_type' => 'fee',
)
);
if ( $item_id ) {
wc_add_order_item_meta( $item_id, '_line_total', $cashback );
wc_add_order_item_meta( $item_id, '_line_tax', 0 );
wc_add_order_item_meta( $item_id, '_line_subtotal', $cashback );
wc_add_order_item_meta( $item_id, '_line_subtotal_tax', 0 );
wc_add_order_item_meta( $item_id, '_tax_class', 'zero-rate' );
}
$order->set_total( wc_format_decimal( \floatval( $order->get_total() ) + \floatval( $cashback ) ) );
}
$order->payment_complete();
return true;
}
}