mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
* chore: setup PHPStan * chore: General code cleanup for PHPStan * chore: PHPStan to level 2 * chore: PHPStan to level 2 * fix: Syntax error fixed. * devops: PHPStan level 2 reached. * devops: PHPStan level 3 passed * devops: PHPStan level 4 passed * chore: fix bad typehints from jwt-auth [phpstan] * chore: simplify conditional logic in WC_Terms::register_connections() * chore: add stubs for WP type properties. * fix: check for valid WC_Order_Item before caching order. * chore: lint * devops: PHPStan level 6 passed. * chore: Linter compliance met. * chore: simplify logic and check for valid term. * fix: ensure valid types (partial) * chore: [phpcs] allow inline type annotations * devops: PHPStan level 8 reached. * devops: More stability fixes. * fix: general bugfixes and improvements * devops: Code Quality GA script added. * devops: Linter compliance met & 2 "phpstan-ignore" statements set. * devops: PHPStan config and CI script fixed. * fix: "cartItem" query resolution refactored * devops: GA scripts updated. * chore: Simple copy changes --------- Co-authored-by: Geoff Taylor <geoff@axistaylor.com>
101 lines
2.4 KiB
PHP
101 lines
2.4 KiB
PHP
<?php
|
|
/**
|
|
* Mutation - deletePaymentMethod
|
|
*
|
|
* Registers mutation for deleting a stored payment method.
|
|
*
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
|
* @since 0.12.4
|
|
*/
|
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
|
|
|
use GraphQL\Error\UserError;
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
use GraphQLRelay\Relay;
|
|
use WPGraphQL\AppContext;
|
|
use WPGraphQL\Data\DataSource;
|
|
use WPGraphQL\Model\Comment;
|
|
use WPGraphQL\Mutation\CommentCreate;
|
|
use WC_Payment_Tokens;
|
|
|
|
/**
|
|
* Class Payment_Method_Delete
|
|
*/
|
|
class Payment_Method_Delete {
|
|
|
|
/**
|
|
* Registers mutation
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function register_mutation() {
|
|
register_graphql_mutation(
|
|
'deletePaymentMethod',
|
|
[
|
|
'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' ],
|
|
'description' => __( 'Token ID of the payment token being deleted.', 'wp-graphql-woocommerce' ),
|
|
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Defines the mutation output field configuration
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function get_output_fields() {
|
|
return [
|
|
'status' => [
|
|
'type' => 'String',
|
|
'description' => __( 'Status of the request', 'wp-graphql-woocommerce' ),
|
|
'resolve' => function ( $payload ) {
|
|
return ! empty( $payload['status'] ) ? $payload['status'] : 'FAILED';
|
|
},
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Defines the mutation data modification closure.
|
|
*
|
|
* @return callable
|
|
*/
|
|
public static function mutate_and_get_payload() {
|
|
return function( $input, AppContext $context, ResolveInfo $info ) {
|
|
global $wp;
|
|
if ( ! is_user_logged_in() ) {
|
|
throw new UserError( __( 'Must be authenticated to set a default payment method', 'wp-graphql-woocommerce' ) );
|
|
}
|
|
|
|
$token_id = $input['tokenId'];
|
|
$token = WC_Payment_Tokens::get( $token_id );
|
|
|
|
if ( is_null( $token ) || get_current_user_id() !== $token->get_user_id() ) {
|
|
throw new UserError( __( 'Invalid payment method.', 'wp-graphql-woocommerce' ) );
|
|
}
|
|
|
|
WC_Payment_Tokens::delete( $token_id );
|
|
wc_add_notice( __( 'Payment method deleted.', 'wp-graphql-woocommerce' ) );
|
|
|
|
return [ 'status' => 'SUCCESS' ];
|
|
};
|
|
}
|
|
}
|