2023-04-20 17:33:58 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Mutation - setDefaultPaymentMethod
|
|
|
|
|
*
|
|
|
|
|
* Registers mutation for changing user's preferred payment method.
|
|
|
|
|
*
|
|
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
|
|
|
|
* @since 0.12.4
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
|
|
|
|
|
|
|
|
|
use GraphQL\Error\UserError;
|
|
|
|
|
use WC_Payment_Tokens;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Payment_Method_Set_Default
|
|
|
|
|
*/
|
|
|
|
|
class Payment_Method_Set_Default {
|
|
|
|
|
/**
|
|
|
|
|
* Registers mutation
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @return void
|
2023-04-20 17:33:58 -04:00
|
|
|
*/
|
|
|
|
|
public static function register_mutation() {
|
|
|
|
|
register_graphql_mutation(
|
|
|
|
|
'setDefaultPaymentMethod',
|
|
|
|
|
[
|
|
|
|
|
'inputFields' => self::get_input_fields(),
|
|
|
|
|
'outputFields' => self::get_output_fields(),
|
|
|
|
|
'mutateAndGetPayload' => self::mutate_and_get_payload(),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation input field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_input_fields() {
|
|
|
|
|
return [
|
|
|
|
|
'tokenId' => [
|
|
|
|
|
'type' => [ 'non_null' => 'Integer' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Token ID of the payment token being deleted.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2023-04-20 17:33:58 -04:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation output field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_output_fields() {
|
|
|
|
|
return [
|
|
|
|
|
'status' => [
|
|
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Status of the request', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2023-07-19 22:56:42 +03:00
|
|
|
'resolve' => static function ( $payload ) {
|
2023-04-20 17:33:58 -04:00
|
|
|
return ! empty( $payload['status'] ) ? $payload['status'] : 'FAILED';
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
'token' => [
|
2026-01-16 12:28:23 -05:00
|
|
|
'type' => 'PaymentTokenInterface',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Preferred payment method token', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2023-07-19 22:56:42 +03:00
|
|
|
'resolve' => static function ( $payload ) {
|
2023-04-20 17:33:58 -04:00
|
|
|
return ! empty( $payload['token'] ) ? $payload['token'] : null;
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation data modification closure.
|
|
|
|
|
*
|
|
|
|
|
* @return callable
|
|
|
|
|
*/
|
|
|
|
|
public static function mutate_and_get_payload() {
|
2023-11-30 15:41:32 +02:00
|
|
|
return static function ( $input ) {
|
2023-04-20 17:33:58 -04:00
|
|
|
global $wp;
|
|
|
|
|
if ( ! is_user_logged_in() ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Must be authenticated to set a default payment method', 'graphql-for-ecommerce' ) );
|
2023-04-20 17:33:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$token_id = $input['tokenId'];
|
|
|
|
|
$token = WC_Payment_Tokens::get( $token_id );
|
|
|
|
|
|
|
|
|
|
if ( is_null( $token ) || get_current_user_id() !== $token->get_user_id() ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Invalid payment method.', 'graphql-for-ecommerce' ) );
|
2023-04-20 17:33:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WC_Payment_Tokens::set_users_default( $token->get_user_id(), intval( $token_id ) );
|
2026-06-30 10:16:21 -04:00
|
|
|
wc_add_notice( __( 'This payment method was successfully set as your default.', 'graphql-for-ecommerce' ) );
|
2023-04-20 17:33:58 -04:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'status' => 'SUCCESS',
|
|
|
|
|
'token' => WC_Payment_Tokens::get( $token_id ),
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|