|
| 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