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

wp-graphql / wp-graphql-woocommerce / 27452430870

13 Jun 2026 01:26AM UTC coverage: 91.8%. Remained the same
27452430870

Pull #1019

github

web-flow
Merge f03617ca3 into 2ce9424e1
Pull Request #1019: fix: address WordPress.org plugin review (rename + prefixing + headers)

1330 of 1587 new or added lines in 201 files covered. (83.81%)

1 existing line in 1 file now uncovered.

18528 of 20183 relevant lines covered (91.8%)

152.68 hits per line

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

93.62
/includes/mutation/class-refund-create.php
1
<?php
2
/**
3
 * Mutation - createRefund
4
 *
5
 * Registers mutation for creating a refund on an order.
6
 *
7
 * @package WPGraphQL\WooCommerce\Mutation
8
 * @since 1.0.0
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_Create
20
 */
21
class Refund_Create {
22
        /**
23
         * Registers mutation
24
         *
25
         * @return void
26
         */
27
        public static function register_mutation() {
28
                register_graphql_mutation(
298✔
29
                        'createRefund',
298✔
30
                        [
298✔
31
                                'inputFields'         => self::get_input_fields(),
298✔
32
                                'outputFields'        => self::get_output_fields(),
298✔
33
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
298✔
34
                        ]
298✔
35
                );
298✔
36
        }
37

38
        /**
39
         * Defines the mutation input field configuration.
40
         *
41
         * @return array
42
         */
43
        public static function get_input_fields() {
44
                return [
298✔
45
                        'orderId'       => [
298✔
46
                                'type'        => [ 'non_null' => 'Int' ],
298✔
47
                                'description' => static function () {
298✔
48
                                        return __( 'The ID of the order to refund.', 'graphql-for-ecommerce' );
2✔
49
                                },
298✔
50
                        ],
298✔
51
                        'amount'        => [
298✔
52
                                'type'        => [ 'non_null' => 'String' ],
298✔
53
                                'description' => static function () {
298✔
54
                                        return __( 'Refund amount.', 'graphql-for-ecommerce' );
2✔
55
                                },
298✔
56
                        ],
298✔
57
                        'reason'        => [
298✔
58
                                'type'        => 'String',
298✔
59
                                'description' => static function () {
298✔
60
                                        return __( 'Reason for refund.', 'graphql-for-ecommerce' );
2✔
61
                                },
298✔
62
                        ],
298✔
63
                        'refundPayment' => [
298✔
64
                                'type'        => 'Boolean',
298✔
65
                                'description' => static function () {
298✔
66
                                        return __( 'When true, the payment gateway API is used to generate the refund.', 'graphql-for-ecommerce' );
2✔
67
                                },
298✔
68
                        ],
298✔
69
                        'restockItems'  => [
298✔
70
                                'type'        => 'Boolean',
298✔
71
                                'description' => static function () {
298✔
72
                                        return __( 'When true, refunded items are restocked.', 'graphql-for-ecommerce' );
2✔
73
                                },
298✔
74
                        ],
298✔
75
                        'metaData'      => [
298✔
76
                                'type'        => [ 'list_of' => 'MetaDataInput' ],
298✔
77
                                'description' => static function () {
298✔
78
                                        return __( 'Meta data.', 'graphql-for-ecommerce' );
2✔
79
                                },
298✔
80
                        ],
298✔
81
                ];
298✔
82
        }
83

84
        /**
85
         * Defines the mutation output field configuration.
86
         *
87
         * @return array
88
         */
89
        public static function get_output_fields() {
90
                return [
298✔
91
                        'refund' => [
298✔
92
                                'type'    => 'Refund',
298✔
93
                                'resolve' => static function ( $payload ) {
298✔
94
                                        return new Order( $payload['id'] );
1✔
95
                                },
298✔
96
                        ],
298✔
97
                        'order'  => [
298✔
98
                                'type'    => 'Order',
298✔
99
                                'resolve' => static function ( $payload ) {
298✔
100
                                        return new Order( $payload['order_id'] );
1✔
101
                                },
298✔
102
                        ],
298✔
103
                ];
298✔
104
        }
105

106
        /**
107
         * Defines the mutation data modification closure.
108
         *
109
         * @return callable
110
         */
111
        public static function mutate_and_get_payload() {
112
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
298✔
113
                        $order_id = absint( $input['orderId'] );
1✔
114
                        $order    = \wc_get_order( $order_id );
1✔
115

116
                        if ( ! $order ) {
1✔
NEW
117
                                throw new UserError( __( 'Invalid order ID.', 'graphql-for-ecommerce' ) );
×
118
                        }
119

120
                        if ( ! \wc_rest_check_post_permissions( 'shop_order', 'edit', $order_id ) ) {
1✔
121
                                throw new UserError( __( 'You do not have permission to create refunds for this order.', 'graphql-for-ecommerce' ) );
1✔
122
                        }
123

124
                        $amount = floatval( $input['amount'] );
1✔
125
                        if ( 0 >= $amount ) {
1✔
126
                                throw new UserError( __( 'Refund amount must be greater than zero.', 'graphql-for-ecommerce' ) );
1✔
127
                        }
128

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

139
                        $refund = \wc_create_refund(
1✔
140
                                [
1✔
141
                                        'order_id'       => $order_id,
1✔
142
                                        'amount'         => $amount,
1✔
143
                                        'reason'         => ! empty( $input['reason'] ) ? $input['reason'] : null,
1✔
144
                                        'refund_payment' => ! empty( $input['refundPayment'] ) ? $input['refundPayment'] : false,
1✔
145
                                        'restock_items'  => ! empty( $input['restockItems'] ) ? $input['restockItems'] : false,
1✔
146
                                ]
1✔
147
                        );
1✔
148

149
                        if ( is_wp_error( $refund ) ) {
1✔
150
                                throw new UserError( $refund->get_error_message() );
×
151
                        }
152

153
                        if ( ! $refund ) {
1✔
NEW
154
                                throw new UserError( __( 'Could not create refund, please try again.', 'graphql-for-ecommerce' ) );
×
155
                        }
156

157
                        // Set meta data.
158
                        if ( ! empty( $input['metaData'] ) && is_array( $input['metaData'] ) ) {
1✔
159
                                foreach ( $input['metaData'] as $meta ) {
×
160
                                        $refund->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
×
161
                                }
162
                                $refund->save_meta_data();
×
163
                        }
164

165
                        /**
166
                         * Action called after a refund is created.
167
                         *
168
                         * @param \WC_Order_Refund                     $refund   Refund object.
169
                         * @param int                                  $order_id Order ID.
170
                         * @param array                                $input    Input data.
171
                         * @param \WPGraphQL\AppContext                $context  AppContext instance.
172
                         * @param \GraphQL\Type\Definition\ResolveInfo $info     ResolveInfo instance.
173
                         */
174
                        do_action( 'graphql_woocommerce_after_refund_create', $refund, $order_id, $input, $context, $info );
1✔
175

176
                        return [
1✔
177
                                'id'       => $refund->get_id(),
1✔
178
                                'order_id' => $order_id,
1✔
179
                        ];
1✔
180
                };
298✔
181
        }
182
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc