2019-07-09 00:49:11 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Mutation - checkout
|
|
|
|
|
*
|
|
|
|
|
* Registers mutation for checking out.
|
|
|
|
|
*
|
2019-10-25 19:13:36 -04:00
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
2019-07-09 00:49:11 -04:00
|
|
|
* @since 0.2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-25 19:13:36 -04:00
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
2019-07-09 00:49:11 -04:00
|
|
|
|
|
|
|
|
use GraphQL\Error\UserError;
|
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
|
use WPGraphQL\AppContext;
|
2019-10-25 19:13:36 -04:00
|
|
|
use WPGraphQL\WooCommerce\Data\Mutation\Checkout_Mutation;
|
|
|
|
|
use WPGraphQL\WooCommerce\Data\Mutation\Order_Mutation;
|
|
|
|
|
use WPGraphQL\WooCommerce\Model\Customer;
|
2023-07-19 23:01:50 +03:00
|
|
|
use WPGraphQL\WooCommerce\Model\Order;
|
2019-07-09 00:49:11 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Checkout
|
|
|
|
|
*/
|
|
|
|
|
class Checkout {
|
|
|
|
|
/**
|
|
|
|
|
* Registers mutation
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @return void
|
2019-07-09 00:49:11 -04:00
|
|
|
*/
|
|
|
|
|
public static function register_mutation() {
|
|
|
|
|
register_graphql_mutation(
|
|
|
|
|
'checkout',
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2019-07-09 00:49:11 -04:00
|
|
|
'inputFields' => self::get_input_fields(),
|
|
|
|
|
'outputFields' => self::get_output_fields(),
|
|
|
|
|
'mutateAndGetPayload' => self::mutate_and_get_payload(),
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2019-07-09 00:49:11 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation input field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_input_fields() {
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
|
|
|
|
'paymentMethod' => [
|
2019-07-09 00:49:11 -04:00
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Payment method ID.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'shippingMethod' => [
|
|
|
|
|
'type' => [ 'list_of' => 'String' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Order shipping method', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'shipToDifferentAddress' => [
|
2019-07-09 00:49:11 -04:00
|
|
|
'type' => 'Boolean',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Ship to a separate address', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'billing' => [
|
2019-07-09 00:49:11 -04:00
|
|
|
'type' => 'CustomerAddressInput',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Order billing address', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'shipping' => [
|
2019-07-09 00:49:11 -04:00
|
|
|
'type' => 'CustomerAddressInput',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Order shipping address', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'account' => [
|
2019-07-09 00:49:11 -04:00
|
|
|
'type' => 'CreateAccountInput',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Create new customer account', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'transactionId' => [
|
2020-02-03 19:30:43 -05:00
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Order transaction ID', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'isPaid' => [
|
2020-02-03 19:30:43 -05:00
|
|
|
'type' => 'Boolean',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Define if the order is paid. It will set the status to processing and reduce stock items.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'metaData' => [
|
|
|
|
|
'type' => [ 'list_of' => 'MetaDataInput' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Order meta data', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'customerNote' => [
|
2020-04-30 18:19:38 -04:00
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Order customer note', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
2026-03-18 16:59:08 -04:00
|
|
|
'fees' => [
|
|
|
|
|
'type' => [ 'list_of' => 'FeeInput' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Fees to add to the order.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2026-03-18 16:59:08 -04:00
|
|
|
],
|
2026-06-12 13:16:53 -04:00
|
|
|
'createdVia' => [
|
|
|
|
|
'type' => 'String',
|
|
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Source of the order. Useful when WooCommerce is driven from multiple sources. Defaults to "checkout".', 'graphql-for-ecommerce' );
|
2026-06-12 13:16:53 -04:00
|
|
|
},
|
|
|
|
|
],
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2019-07-09 00:49:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation output field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_output_fields() {
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
|
|
|
|
'order' => [
|
2019-07-09 00:49:11 -04:00
|
|
|
'type' => 'Order',
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $payload ) {
|
2019-07-09 00:49:11 -04:00
|
|
|
return new Order( $payload['id'] );
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'customer' => [
|
2019-07-09 16:02:42 -04:00
|
|
|
'type' => 'Customer',
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function () {
|
2023-06-21 13:21:29 -04:00
|
|
|
return is_user_logged_in() ? new Customer( get_current_user_id() ) : new Customer();
|
2019-07-09 16:02:42 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'result' => [
|
2019-08-26 17:39:46 -04:00
|
|
|
'type' => 'String',
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $payload ) {
|
2019-08-26 17:39:46 -04:00
|
|
|
return $payload['result'];
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'redirect' => [
|
2019-08-26 17:39:46 -04:00
|
|
|
'type' => 'String',
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $payload ) {
|
2019-08-26 17:39:46 -04:00
|
|
|
return $payload['redirect'];
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
2026-03-18 17:51:27 -04:00
|
|
|
'notices' => [
|
|
|
|
|
'type' => [ 'list_of' => 'CartNotice' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'WooCommerce notices generated during checkout', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2026-03-18 17:51:27 -04:00
|
|
|
'resolve' => static function ( $payload ) {
|
|
|
|
|
return $payload['notices'] ?? [];
|
|
|
|
|
},
|
|
|
|
|
],
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2019-07-09 00:49:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation data modification closure.
|
|
|
|
|
*
|
|
|
|
|
* @return callable
|
|
|
|
|
*/
|
|
|
|
|
public static function mutate_and_get_payload() {
|
2023-07-20 00:11:25 +03:00
|
|
|
return static function ( $input, AppContext $context, ResolveInfo $info ) {
|
2019-07-09 00:49:11 -04:00
|
|
|
// Create order.
|
|
|
|
|
$order = null;
|
|
|
|
|
try {
|
|
|
|
|
$args = Checkout_Mutation::prepare_checkout_args( $input, $context, $info );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Action called before checking out.
|
|
|
|
|
*
|
|
|
|
|
* @param array $args Order data.
|
|
|
|
|
* @param array $input Raw input data .
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param \WPGraphQL\AppContext $context Request AppContext instance.
|
|
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info Request ResolveInfo instance.
|
2019-07-09 00:49:11 -04:00
|
|
|
*/
|
2020-02-16 00:04:40 -05:00
|
|
|
do_action( 'graphql_woocommerce_before_checkout', $args, $input, $context, $info );
|
2019-07-09 00:49:11 -04:00
|
|
|
|
2023-07-19 23:12:36 +03:00
|
|
|
// We define this now and pass it as a reference.
|
|
|
|
|
$results = [];
|
|
|
|
|
|
2020-02-03 19:30:43 -05:00
|
|
|
$order_id = Checkout_Mutation::process_checkout( $args, $input, $context, $info, $results );
|
2019-07-09 00:49:11 -04:00
|
|
|
|
2022-02-16 00:06:19 +01:00
|
|
|
$order = \WC_Order_Factory::get_order( $order_id );
|
2023-06-13 23:17:02 +03:00
|
|
|
|
|
|
|
|
if ( ! is_object( $order ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Failed to retrieve order after checkout', 'graphql-for-ecommerce' ) );
|
2023-06-13 23:17:02 +03:00
|
|
|
}//end if
|
|
|
|
|
|
2026-03-18 17:51:27 -04:00
|
|
|
// Capture any non-error notices for successful checkouts.
|
|
|
|
|
$notices = wc_get_notices();
|
|
|
|
|
$formatted_notices = self::format_notices_for_response( $notices );
|
|
|
|
|
|
|
|
|
|
// Clear notices to prevent persistence.
|
|
|
|
|
wc_clear_notices();
|
|
|
|
|
|
2019-07-09 00:49:11 -04:00
|
|
|
/**
|
|
|
|
|
* Action called after checking out.
|
|
|
|
|
*
|
2023-06-13 23:17:02 +03:00
|
|
|
* @param \WC_Order $order WC_Order instance.
|
2019-07-09 00:49:11 -04:00
|
|
|
* @param array $input Input data describing order.
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param \WPGraphQL\AppContext $context Request AppContext instance.
|
|
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info Request ResolveInfo instance.
|
2019-07-09 00:49:11 -04:00
|
|
|
*/
|
2022-02-16 00:06:19 +01:00
|
|
|
do_action( 'graphql_woocommerce_after_checkout', $order, $input, $context, $info );
|
2019-07-09 00:49:11 -04:00
|
|
|
|
2026-03-18 17:51:27 -04:00
|
|
|
return array_merge( [ 'id' => $order_id ], $results, [ 'notices' => $formatted_notices ] );
|
2023-07-19 23:01:50 +03:00
|
|
|
} catch ( \Throwable $e ) {
|
2023-06-13 23:17:02 +03:00
|
|
|
// Delete order if it was created.
|
|
|
|
|
if ( is_object( $order ) ) {
|
|
|
|
|
Order_Mutation::purge( $order );
|
|
|
|
|
}
|
2026-03-18 17:51:27 -04:00
|
|
|
|
|
|
|
|
// Capture any WC notices that were added during checkout process.
|
|
|
|
|
$notices = wc_get_notices();
|
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
|
|
|
|
|
|
// If there are notices, use them instead of the original error.
|
|
|
|
|
if ( ! empty( $notices ) ) {
|
|
|
|
|
$formatted_notices = self::format_notices_for_error( $notices );
|
|
|
|
|
if ( ! empty( $formatted_notices ) ) {
|
|
|
|
|
$error_message = $formatted_notices;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear notices to prevent them from persisting to next request.
|
|
|
|
|
wc_clear_notices();
|
|
|
|
|
|
|
|
|
|
// Throw error with enhanced message.
|
|
|
|
|
throw new UserError( $error_message );
|
2022-02-04 15:00:44 -05:00
|
|
|
}//end try
|
2019-07-09 00:49:11 -04:00
|
|
|
};
|
|
|
|
|
}
|
2026-03-18 17:51:27 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format WC notices for GraphQL response.
|
|
|
|
|
*
|
|
|
|
|
* @param array $notices WC notices array.
|
|
|
|
|
* @return array Formatted notices for GraphQL
|
|
|
|
|
*/
|
|
|
|
|
private static function format_notices_for_response( $notices ) {
|
|
|
|
|
$formatted_notices = [];
|
|
|
|
|
|
|
|
|
|
// Include non-error notices (success, notice).
|
|
|
|
|
foreach ( [ 'success', 'notice' ] as $type ) {
|
|
|
|
|
if ( ! empty( $notices[ $type ] ) ) {
|
|
|
|
|
foreach ( $notices[ $type ] as $notice ) {
|
|
|
|
|
$formatted_notices[] = [
|
|
|
|
|
'type' => $type,
|
|
|
|
|
'message' => $notice['notice'] ?? $notice,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $formatted_notices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format WC notices for error reporting
|
|
|
|
|
*
|
|
|
|
|
* @param array $notices WC notices array
|
|
|
|
|
* @return string Formatted error message
|
|
|
|
|
*/
|
|
|
|
|
private static function format_notices_for_error( $notices ) {
|
|
|
|
|
$error_messages = [];
|
|
|
|
|
|
|
|
|
|
// Prioritize error notices.
|
|
|
|
|
if ( ! empty( $notices['error'] ) ) {
|
|
|
|
|
foreach ( $notices['error'] as $notice ) {
|
|
|
|
|
$error_messages[] = $notice['notice'] ?? $notice;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Include other notice types if no errors.
|
|
|
|
|
if ( empty( $error_messages ) ) {
|
|
|
|
|
foreach ( [ 'notice', 'success' ] as $type ) {
|
|
|
|
|
if ( ! empty( $notices[ $type ] ) ) {
|
|
|
|
|
foreach ( $notices[ $type ] as $notice ) {
|
|
|
|
|
$error_messages[] = $notice['notice'] ?? $notice;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implode( ' ', $error_messages );
|
|
|
|
|
}
|
2019-07-09 00:49:11 -04:00
|
|
|
}
|