• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-woocommerce / 23801006185

31 Mar 2026 01:53PM UTC coverage: 91.773% (+0.02%) from 91.753%
23801006185

Pull #906

github

web-flow
Merge 1ba796da6 into 858887fa7
Pull Request #906: feat: Implements `refund` mutations

147 of 156 new or added lines in 4 files covered. (94.23%)

18483 of 20140 relevant lines covered (91.77%)

148.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.83
/includes/mutation/class-refund-delete.php
1
<?php
2
/**
3
 * Mutation - deleteRefund
4
 *
5
 * Registers mutation for deleting a refund on an order.
6
 *
7
 * @package WPGraphQL\WooCommerce\Mutation
8
 * @since TBD
9
 */
10

11
namespace WPGraphQL\WooCommerce\Mutation;
12

13
use GraphQL\Error\UserError;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use WPGraphQL\AppContext;
16
use WPGraphQL\WooCommerce\Model\Order;
17

18
/**
19
 * Class Refund_Delete
20
 */
21
class Refund_Delete {
22
        /**
23
         * Registers mutation
24
         *
25
         * @return void
26
         */
27
        public static function register_mutation() {
28
                register_graphql_mutation(
289✔
29
                        'deleteRefund',
289✔
30
                        [
289✔
31
                                'inputFields'         => self::get_input_fields(),
289✔
32
                                'outputFields'        => self::get_output_fields(),
289✔
33
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
289✔
34
                        ]
289✔
35
                );
289✔
36
        }
37

38
        /**
39
         * Defines the mutation input field configuration.
40
         *
41
         * @return array
42
         */
43
        public static function get_input_fields() {
44
                return [
289✔
45
                        'id'    => [
289✔
46
                                'type'        => [ 'non_null' => 'ID' ],
289✔
47
                                'description' => static function () {
289✔
48
                                        return __( 'The ID of the refund to delete.', 'wp-graphql-woocommerce' );
2✔
49
                                },
289✔
50
                        ],
289✔
51
                        'force' => [
289✔
52
                                'type'        => 'Boolean',
289✔
53
                                'description' => static function () {
289✔
54
                                        return __( 'Force delete the refund. Defaults to true.', 'wp-graphql-woocommerce' );
2✔
55
                                },
289✔
56
                        ],
289✔
57
                ];
289✔
58
        }
59

60
        /**
61
         * Defines the mutation output field configuration.
62
         *
63
         * @return array
64
         */
65
        public static function get_output_fields() {
66
                return [
289✔
67
                        'refund' => [
289✔
68
                                'type'    => 'Refund',
289✔
69
                                'resolve' => static function ( $payload ) {
289✔
70
                                        return ! empty( $payload['refund'] ) ? $payload['refund'] : null;
1✔
71
                                },
289✔
72
                        ],
289✔
73
                        'order'  => [
289✔
74
                                'type'    => 'Order',
289✔
75
                                'resolve' => static function ( $payload ) {
289✔
76
                                        return ! empty( $payload['order_id'] ) ? new Order( $payload['order_id'] ) : null;
1✔
77
                                },
289✔
78
                        ],
289✔
79
                ];
289✔
80
        }
81

82
        /**
83
         * Defines the mutation data modification closure.
84
         *
85
         * @return callable
86
         */
87
        public static function mutate_and_get_payload() {
88
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
289✔
89
                        $refund_id = \WPGraphQL\Utils\Utils::get_database_id_from_id( $input['id'] );
1✔
90

91
                        if ( empty( $refund_id ) ) {
1✔
NEW
92
                                throw new UserError( __( 'Invalid refund ID.', 'wp-graphql-woocommerce' ) );
×
93
                        }
94

95
                        /** @var \WC_Order_Refund|false $refund */
96
                        $refund = \wc_get_order( $refund_id );
1✔
97
                        if ( ! $refund || 'shop_order_refund' !== $refund->get_type() ) {
1✔
NEW
98
                                throw new UserError( __( 'Invalid refund ID.', 'wp-graphql-woocommerce' ) );
×
99
                        }
100

101
                        $order_id = $refund->get_parent_id();
1✔
102
                        if ( ! \wc_rest_check_post_permissions( 'shop_order', 'delete', $order_id ) ) {
1✔
103
                                throw new UserError( __( 'You do not have permission to delete this refund.', 'wp-graphql-woocommerce' ) );
1✔
104
                        }
105

106
                        // Capture refund data before deletion for the response.
107
                        $refund_model = new Order( $refund_id );
1✔
108

109
                        /**
110
                         * Action called before a refund is deleted.
111
                         *
112
                         * @param int                                  $refund_id Refund ID.
113
                         * @param int                                  $order_id  Order ID.
114
                         * @param array                                $input     Input data.
115
                         * @param \WPGraphQL\AppContext                $context   AppContext instance.
116
                         * @param \GraphQL\Type\Definition\ResolveInfo $info      ResolveInfo instance.
117
                         */
118
                        do_action( 'graphql_woocommerce_before_refund_delete', $refund_id, $order_id, $input, $context, $info );
1✔
119

120
                        $force  = isset( $input['force'] ) ? $input['force'] : true;
1✔
121
                        $result = $refund->delete( $force );
1✔
122

123
                        if ( ! $result ) {
1✔
NEW
124
                                throw new UserError( __( 'Could not delete refund.', 'wp-graphql-woocommerce' ) );
×
125
                        }
126

127
                        /**
128
                         * Action called after a refund is deleted.
129
                         *
130
                         * @param int                                  $refund_id Refund ID.
131
                         * @param int                                  $order_id  Order ID.
132
                         * @param array                                $input     Input data.
133
                         * @param \WPGraphQL\AppContext                $context   AppContext instance.
134
                         * @param \GraphQL\Type\Definition\ResolveInfo $info      ResolveInfo instance.
135
                         */
136
                        do_action( 'graphql_woocommerce_after_refund_delete', $refund_id, $order_id, $input, $context, $info );
1✔
137

138
                        return [
1✔
139
                                'refund'   => $refund_model,
1✔
140
                                'order_id' => $order_id,
1✔
141
                        ];
1✔
142
                };
289✔
143
        }
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc