Skip to content

Commit 27d9297

Browse files
authored
feat: payment method mutations and fields implemented. (#735)
* feat: payment method mutation implemented. * devops: Unit test updated. * chore: WPCS compliance met.
1 parent 41caf45 commit 27d9297

13 files changed

+839
-19
lines changed

docs/toc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [Harmonizing with WordPress](harmonizing-with-wordpress.md)
1717
- [Using Checkout Mutation + Order Mutations](using-checkout-mutation-and-order-mutations.md)
1818
- [Using Order Data](using-order-data.md)
19-
- [Using Customer Data + Mutations](using-customer-data-mutations.md)
19+
- [Using Customer Data + Mutations](using-customer-data-and-mutations.md)
2020

2121
## WooGraphQL Pro
2222

includes/class-core-schema-filters.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ public static function add_filters() {
129129
'graphql_wp_connection_type_config',
130130
[ '\WPGraphQL\WooCommerce\Connection\Products', 'set_connection_config' ]
131131
);
132+
133+
add_filter(
134+
'woographql_cart_connection_definitions',
135+
[ __CLASS__, 'skip_cart_item_connection' ],
136+
);
132137
}
133138

134139
/**
@@ -401,4 +406,32 @@ public static function inject_type_resolver( $type, $value ) {
401406

402407
return $type;
403408
}
409+
410+
/**
411+
* Return true if WooGraphQL Pro is handling cart item connections.
412+
*
413+
* @return boolean
414+
*/
415+
private static function should_skip_cart_item_connection() {
416+
if ( ! class_exists( 'WPGraphQL\WooCommerce\Pro\WooGraphQL_Pro' ) ) {
417+
return false;
418+
}
419+
420+
return Pro\WooGraphQL_Pro::is_composite_products_enabled()
421+
&& Pro\WooGraphQL_Pro::is_composite_products_active();
422+
}
423+
424+
/**
425+
* Skip core cart item connection definitions if WooGraphQL Pro is handling it.
426+
*
427+
* @param array $connections Cart connection defintions.
428+
* @return array
429+
*/
430+
public static function skip_cart_item_connection( $connections ) {
431+
if ( self::should_skip_cart_item_connection() ) {
432+
unset( $connections['contents'] );
433+
}
434+
435+
return $connections;
436+
}
404437
}

includes/class-type-registry.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
6363
Type\WPInterface\Attribute::register_interface( $type_registry );
6464
Type\WPInterface\Product_Attribute::register_interface( $type_registry );
6565
Type\WPInterface\Cart_Error::register_interface( $type_registry );
66+
Type\WPInterface\Payment_Token::register_interface( $type_registry );
6667

6768
// Objects.
6869
Type\WPObject\Meta_Data_Type::register();
@@ -86,6 +87,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
8687
Type\WPObject\Shipping_Package_Type::register();
8788
Type\WPObject\Shipping_Rate_Type::register();
8889
Type\WPObject\Cart_Error_Types::register();
90+
Type\WPObject\Payment_Token_Types::register();
8991

9092
// Object fields.
9193
Type\WPObject\Product_Category_Type::register_fields();
@@ -130,5 +132,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
130132
Mutation\Coupon_Create::register_mutation();
131133
Mutation\Coupon_Update::register_mutation();
132134
Mutation\Coupon_Delete::register_mutation();
135+
Mutation\Payment_Method_Delete::register_mutation();
136+
Mutation\Payment_Method_Set_Default::register_mutation();
133137
}
134138
}

includes/class-wp-graphql-woocommerce.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ private function includes() {
231231
require $include_directory_path . 'type/interface/class-cart-error.php';
232232
require $include_directory_path . 'type/interface/class-product-attribute.php';
233233
require $include_directory_path . 'type/interface/class-product.php';
234+
require $include_directory_path . 'type/interface/class-payment-token.php';
234235

235236
// Include object type class files.
236237
require $include_directory_path . 'type/object/class-cart-error-types.php';
@@ -256,6 +257,7 @@ private function includes() {
256257
require $include_directory_path . 'type/object/class-simple-attribute-type.php';
257258
require $include_directory_path . 'type/object/class-tax-rate-type.php';
258259
require $include_directory_path . 'type/object/class-variation-attribute-type.php';
260+
require $include_directory_path . 'type/object/class-payment-token-types.php';
259261

260262
// Include input type class files.
261263
require $include_directory_path . 'type/input/class-cart-item-input.php';
@@ -297,6 +299,8 @@ private function includes() {
297299
require $include_directory_path . 'mutation/class-review-write.php';
298300
require $include_directory_path . 'mutation/class-review-delete-restore.php';
299301
require $include_directory_path . 'mutation/class-review-update.php';
302+
require $include_directory_path . 'mutation/class-payment-method-delete.php';
303+
require $include_directory_path . 'mutation/class-payment-method-set-default.php';
300304

301305
// Include connection class/function files.
302306
require $include_directory_path . 'connection/wc-cpt-connection-args.php';
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Mutation - deletePaymentMethod
4+
*
5+
* Registers mutation for deleting a stored payment method.
6+
*
7+
* @package WPGraphQL\WooCommerce\Mutation
8+
* @since 0.12.4
9+
*/
10+
11+
namespace WPGraphQL\WooCommerce\Mutation;
12+
13+
use GraphQL\Error\UserError;
14+
use GraphQL\Type\Definition\ResolveInfo;
15+
use GraphQLRelay\Relay;
16+
use WPGraphQL\AppContext;
17+
use WPGraphQL\Data\DataSource;
18+
use WPGraphQL\Model\Comment;
19+
use WPGraphQL\Mutation\CommentCreate;
20+
use WC_Payment_Tokens;
21+
22+
/**
23+
* Class Payment_Method_Delete
24+
*/
25+
class Payment_Method_Delete {
26+
27+
/**
28+
* Registers mutation
29+
*/
30+
public static function register_mutation() {
31+
register_graphql_mutation(
32+
'deletePaymentMethod',
33+
[
34+
'inputFields' => self::get_input_fields(),
35+
'outputFields' => self::get_output_fields(),
36+
'mutateAndGetPayload' => self::mutate_and_get_payload(),
37+
]
38+
);
39+
}
40+
41+
/**
42+
* Defines the mutation input field configuration
43+
*
44+
* @return array
45+
*/
46+
public static function get_input_fields() {
47+
return [
48+
'tokenId' => [
49+
'type' => [ 'non_null' => 'Integer' ],
50+
'description' => __( 'Token ID of the payment token being deleted.', 'wp-graphql-woocommerce' ),
51+
52+
],
53+
];
54+
}
55+
56+
/**
57+
* Defines the mutation output field configuration
58+
*
59+
* @return array
60+
*/
61+
public static function get_output_fields() {
62+
return [
63+
'status' => [
64+
'type' => 'String',
65+
'description' => __( 'Status of the request', 'wp-graphql-woocommerce' ),
66+
'resolve' => function ( $payload ) {
67+
return ! empty( $payload['status'] ) ? $payload['status'] : 'FAILED';
68+
},
69+
],
70+
];
71+
}
72+
73+
/**
74+
* Defines the mutation data modification closure.
75+
*
76+
* @return callable
77+
*/
78+
public static function mutate_and_get_payload() {
79+
return function( $input, AppContext $context, ResolveInfo $info ) {
80+
global $wp;
81+
if ( ! is_user_logged_in() ) {
82+
throw new UserError( __( 'Must be authenticated to set a default payment method', 'wp-graphql-woocommerce' ) );
83+
}
84+
85+
$token_id = $input['tokenId'];
86+
$token = WC_Payment_Tokens::get( $token_id );
87+
88+
if ( is_null( $token ) || get_current_user_id() !== $token->get_user_id() ) {
89+
throw new UserError( __( 'Invalid payment method.', 'wp-graphql-woocommerce' ) );
90+
}
91+
92+
WC_Payment_Tokens::delete( $token_id );
93+
wc_add_notice( __( 'Payment method deleted.', 'wp-graphql-woocommerce' ) );
94+
95+
return [ 'status' => 'SUCCESS' ];
96+
};
97+
}
98+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Mutation - setDefaultPaymentMethod
4+
*
5+
* Registers mutation for changing user's preferred payment method.
6+
*
7+
* @package WPGraphQL\WooCommerce\Mutation
8+
* @since 0.12.4
9+
*/
10+
11+
namespace WPGraphQL\WooCommerce\Mutation;
12+
13+
use GraphQL\Error\UserError;
14+
use GraphQL\Type\Definition\ResolveInfo;
15+
use GraphQLRelay\Relay;
16+
use WPGraphQL\AppContext;
17+
use WPGraphQL\Data\DataSource;
18+
use WPGraphQL\Model\Comment;
19+
use WPGraphQL\Mutation\CommentCreate;
20+
use WC_Payment_Tokens;
21+
22+
/**
23+
* Class Payment_Method_Set_Default
24+
*/
25+
class Payment_Method_Set_Default {
26+
27+
/**
28+
* Registers mutation
29+
*/
30+
public static function register_mutation() {
31+
register_graphql_mutation(
32+
'setDefaultPaymentMethod',
33+
[
34+
'inputFields' => self::get_input_fields(),
35+
'outputFields' => self::get_output_fields(),
36+
'mutateAndGetPayload' => self::mutate_and_get_payload(),
37+
]
38+
);
39+
}
40+
41+
/**
42+
* Defines the mutation input field configuration
43+
*
44+
* @return array
45+
*/
46+
public static function get_input_fields() {
47+
return [
48+
'tokenId' => [
49+
'type' => [ 'non_null' => 'Integer' ],
50+
'description' => __( 'Token ID of the payment token being deleted.', 'wp-graphql-woocommerce' ),
51+
],
52+
];
53+
}
54+
55+
/**
56+
* Defines the mutation output field configuration
57+
*
58+
* @return array
59+
*/
60+
public static function get_output_fields() {
61+
return [
62+
'status' => [
63+
'type' => 'String',
64+
'description' => __( 'Status of the request', 'wp-graphql-woocommerce' ),
65+
'resolve' => function ( $payload ) {
66+
return ! empty( $payload['status'] ) ? $payload['status'] : 'FAILED';
67+
},
68+
],
69+
'token' => [
70+
'type' => 'PaymentToken',
71+
'description' => __( 'Preferred payment method token', 'wp-graphql-woocommerce' ),
72+
'resolve' => function ( $payload ) {
73+
return ! empty( $payload['token'] ) ? $payload['token'] : null;
74+
},
75+
],
76+
];
77+
}
78+
79+
/**
80+
* Defines the mutation data modification closure.
81+
*
82+
* @return callable
83+
*/
84+
public static function mutate_and_get_payload() {
85+
return function( $input, AppContext $context, ResolveInfo $info ) {
86+
global $wp;
87+
if ( ! is_user_logged_in() ) {
88+
throw new UserError( __( 'Must be authenticated to set a default payment method', 'wp-graphql-woocommerce' ) );
89+
}
90+
91+
$token_id = $input['tokenId'];
92+
$token = WC_Payment_Tokens::get( $token_id );
93+
94+
if ( is_null( $token ) || get_current_user_id() !== $token->get_user_id() ) {
95+
throw new UserError( __( 'Invalid payment method.', 'wp-graphql-woocommerce' ) );
96+
}
97+
98+
WC_Payment_Tokens::set_users_default( $token->get_user_id(), intval( $token_id ) );
99+
wc_add_notice( __( 'This payment method was successfully set as your default.', 'wp-graphql-woocommerce' ) );
100+
101+
return [
102+
'status' => 'SUCCESS',
103+
'token' => WC_Payment_Tokens::get( $token_id ),
104+
];
105+
};
106+
}
107+
}

0 commit comments

Comments
 (0)