d_registry Payment method registry instance. */ public function register_payment_method_integrations( PaymentMethodRegistry $payment_method_registry ) { // This is temporarily registering Stripe until it's moved to the extension. if ( class_exists( '\WC_Stripe' ) && ! $payment_method_registry->is_registered( 'stripe' ) ) { $payment_method_registry->register( Package::container()->get( Stripe::class ) ); } $payment_method_registry->register( Package::container()->get( Cheque::class ) ); $payment_method_registry->register( Package::container()->get( PayPal::class ) ); $payment_method_registry->register( Package::container()->get( BankTransfer::class ) ); $payment_method_registry->register( Package::container()->get( CashOnDelivery::class ) ); } /** * Attempt to process a payment for the checkout API if no payment methods support the * woocommerce_rest_checkout_process_payment_with_context action. * * @param PaymentContext $context Holds context for the payment. * @param PaymentResult $result Result of the payment. */ public function process_legacy_payment( PaymentContext $context, PaymentResult &$result ) { if ( $result->status ) { return; } // phpcs:ignore WordPress.Security.NonceVerification $post_data = $_POST; // Set constants. wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true ); // Add the payment data from the API to the POST global. $_POST = $context->payment_data; // Call the process payment method of the chosen gateway. $payment_method_object = $context->get_payment_method_instance(); if ( ! $payment_method_object instanceof \WC_Payment_Gateway ) { return; } $payment_method_object->validate_fields(); // If errors were thrown, we need to abort. NoticeHandler::convert_notices_to_exceptions( 'woocommerce_rest_payment_error' ); // Process Payment. $gateway_result = $payment_method_object->process_payment( $context->order->get_id() ); // Restore $_POST data. $_POST = $post_data; // If `process_payment` added notices, clear them. Notices are not displayed from the API -- payment should fail, // and a generic notice will be shown instead if payment failed. wc_clear_notices(); // Handle result. $result->set_status( isset( $gateway_result['result'] ) && 'success' === $gateway_result['result'] ? 'success' : 'failure' ); // set payment_details from result. $result->set_payment_details( array_merge( $result->payment_details, $gateway_result ) ); $result->set_redirect_url( $gateway_result['redirect'] ); } /** * Verify all dependencies of registered payment methods have been registered. * If not, remove that payment method script from the list of dependencies * of Cart and Checkout block scripts so it doesn't break the blocks and show * an error in the admin. */ public function verify_payment_methods_dependencies() { $wp_scripts = wp_scripts(); $payment_method_scripts = $this->payment_method_registry->get_all_active_payment_method_script_dependencies(); foreach ( $payment_method_scripts as $payment_method_script ) { if ( ! array_key_exists( $payment_method_script, $wp_scripts->registered ) || ! property_exists( $wp_scripts->registered[ $payment_method_script ], 'deps' ) ) { continue; } $deps = $wp_scripts->registered[ $payment_method_script ]->deps; foreach ( $deps as $dep ) { if ( ! wp_script_is( $dep, 'registered' ) ) { $error_handle = $dep . '-dependency-error'; $error_message = sprintf( 'Payment gateway with handle \'%1$s\' has been deactivated because its dependency \'%2$s\' is not registered. Read the docs about registering assets for payment methods: https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/trunk/docs/extensibility/payment-method-integration.md#registering-assets', esc_html( $payment_method_script ), esc_html( $dep ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( $error_message ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion wp_register_script( $error_handle, '' ); wp_enqueue_script( $error_handle ); wp_add_inline_script( $error_handle, sprintf( 'console.error( "%s" );', $error_message ) ); $cart_checkout_scripts = [ 'wc-cart-block', 'wc-cart-block-frontend', 'wc-checkout-block', 'wc-checkout-block-frontend' ]; foreach ( $cart_checkout_scripts as $script_handle ) { if ( ! array_key_exists( $script_handle, $wp_scripts->registered ) || ! property_exists( $wp_scripts->registered[ $script_handle ], 'deps' ) ) { continue; } // Remove payment method script from dependencies. $wp_scripts->registered[ $script_handle ]->deps = array_diff( $wp_scripts->registered[ $script_handle ]->deps, [ $payment_method_script ] ); } } } } } }