get_payment_method() && $order->is_paid() && '' !== (string) $order->get_meta( AgentaOS_Gateway::META_TRANSACTION_ID ) && '' === (string) $order->get_meta( self::META_STATE ); } /** * @param WC_Order $order Order the action was run on. */ public static function request( WC_Order $order ) { $gateway = self::gateway(); if ( ! $gateway || ! self::can_request( $order ) ) { return; } try { $gateway->api()->request_refund( (string) $order->get_meta( AgentaOS_Gateway::META_TRANSACTION_ID ) ); } catch ( Exception $e ) { /* translators: %s: error from AgentaOS */ $order->add_order_note( sprintf( __( 'AgentaOS refund request failed: %s', 'agentaos-payments' ), $e->getMessage() ) ); return; } $order->update_meta_data( self::META_STATE, 'requested' ); $order->update_meta_data( self::META_ASKED, time() ); $order->save(); $order->add_order_note( __( 'Refund requested from AgentaOS. It reviews requests within 2 business days; nothing changes until it approves. The order is marked refunded automatically once AgentaOS has refunded the buyer.', 'agentaos-payments' ) ); } /** * Scheduled: record refunds AgentaOS made and disputes it lost. */ public static function check() { self::forget_completed_checkouts(); $refunds = self::orders( self::META_STATE, 'requested' ); $disputes = self::orders( '_agentaos_dispute_id', null, 'EXISTS' ); $gateway = self::gateway(); if ( ( ! $refunds && ! $disputes ) || ! $gateway ) { return; } try { $status = $gateway->api()->sale_statuses(); } catch ( Exception $e ) { $gateway->log( 'Sale status check failed: ' . $e->getMessage() ); return; } foreach ( $refunds as $order ) { if ( 'requested' !== $order->get_meta( self::META_STATE ) ) { continue; } $sale = self::sale_status( $status, $order ); if ( 'refunded' === $sale ) { if ( $order->get_remaining_refund_amount() > 0 ) { $refund = wc_create_refund( array( 'order_id' => $order->get_id(), 'amount' => $order->get_remaining_refund_amount(), 'reason' => __( 'Refunded by AgentaOS (full amount, VAT included).', 'agentaos-payments' ), 'refund_payment' => false, ) ); if ( is_wp_error( $refund ) ) { /* translators: %s: WooCommerce error */ $order->add_order_note( sprintf( __( 'AgentaOS refunded this sale, but WooCommerce could not record the refund: %s. It will try again at the next check.', 'agentaos-payments' ), $refund->get_error_message() ) ); continue; } } // Re-read: creating the refund saves the order, so set the state on the fresh copy. $order = wc_get_order( $order->get_id() ); $order->update_meta_data( self::META_STATE, 'refunded' ); $order->save(); } elseif ( time() - (int) $order->get_meta( self::META_ASKED ) > self::GIVE_UP_DAY * DAY_IN_SECONDS ) { $order->update_meta_data( self::META_STATE, 'check-dashboard' ); $order->save(); $order->add_order_note( __( 'No refund recorded after 30 days, so the plugin stopped checking. Look at the sale in your AgentaOS dashboard.', 'agentaos-payments' ) ); } } foreach ( $disputes as $order ) { $order = wc_get_order( $order->get_id() ); // The refund loop may have saved this order. if ( '' !== (string) $order->get_meta( '_agentaos_dispute_id' ) && 'charged_back' === self::sale_status( $status, $order ) ) { $order->delete_meta_data( '_agentaos_dispute_id' ); $order->delete_meta_data( '_agentaos_status_before_dispute' ); $order->update_status( 'failed', __( 'The dispute was lost: the buyer\'s bank returned the payment (chargeback).', 'agentaos-payments' ) ); } } } /** * Remove the "checkout completed" markers older than a day: by then the webhook and the return page * have both long arrived. */ private static function forget_completed_checkouts() { global $wpdb; $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s AND CAST(option_value AS UNSIGNED) < %d", $wpdb->esc_like( AgentaOS_Gateway::COMPLETED_PREFIX ) . '%', time() - DAY_IN_SECONDS ) ); } /** * @param array $status Sale id => mor_status. * @param WC_Order $order Order. * @return string */ private static function sale_status( array $status, WC_Order $order ) { $id = (string) $order->get_meta( AgentaOS_Gateway::META_TRANSACTION_ID ); return isset( $status[ $id ] ) ? (string) $status[ $id ] : ''; } /** * Uses meta_key/meta_value, which both order storages understand: the legacy posts storage ignores * a meta_query here and would return every order. * * @param string $key Meta key. * @param string|null $value Meta value, or null to match any. * @param string $compare Meta compare. * @return WC_Order[] */ private static function orders( $key, $value, $compare = '=' ) { $args = array( 'limit' => 50, 'payment_method' => 'agentaos', 'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_compare' => $compare, ); if ( null !== $value ) { $args['meta_value'] = $value; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value } return wc_get_orders( $args ); } /** * @return AgentaOS_Gateway|null */ private static function gateway() { $gateways = WC()->payment_gateways() ? WC()->payment_gateways()->payment_gateways() : array(); return isset( $gateways['agentaos'] ) ? $gateways['agentaos'] : null; } }