PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / api / payment.php

payment.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/api/payment.php

253 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\API;
4
5 use StoreEngine\Classes\Exceptions\StoreEngineException;
6 use StoreEngine\Classes\Order;
7 use StoreEngine\Classes\OrderCollection;
8 use StoreEngine\Classes\OrderStatus\OrderStatus;
9 use StoreEngine\Utils\Helper;
10 use WP_Error;
11 use WP_REST_Request;
12 use WP_REST_Server;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 class Payment extends AbstractRestApiController {
19 public static function init() {
20 $self = new self();
21 $self->namespace = STOREENGINE_PLUGIN_SLUG . '/v1';
22 $self->rest_base = 'payment';
23
24 add_action( 'rest_api_init', [ $self, 'register_routes' ] );
25 }
26
27
28 public function register_routes() {
29 register_rest_route( $this->namespace, '/' . $this->rest_base, [
30 [
31 'methods' => WP_REST_Server::READABLE,
32 'callback' => [ $this, 'get_payment' ],
33 'permission_callback' => [ $this, 'check_payment_permissions' ],
34 'args' => $this->get_item_schema(),
35 ],
36 ] );
37 register_rest_route( $this->namespace, '/' . $this->rest_base, [
38 [
39 'methods' => WP_REST_Server::CREATABLE,
40 'callback' => [ $this, 'create_payment' ],
41 'permission_callback' => [ $this, 'check_payment_permissions' ],
42 'args' => $this->get_item_schema(),
43 ],
44 ] );
45 register_rest_route( $this->namespace, '/' . $this->rest_base . '/all', [
46 [
47 'methods' => WP_REST_Server::READABLE,
48 'callback' => [ $this, 'get_payments' ],
49 'permission_callback' => [ $this, 'check_admin_permissions' ],
50 'args' => $this->get_collection_params(),
51 ],
52 ] );
53
54 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [
55 'args' => [
56 'id' => [
57 'description' => __( 'Unique identifier for the object.', 'storeengine' ),
58 'type' => 'integer',
59 ],
60 ],
61 [
62 'methods' => WP_REST_Server::READABLE,
63 'callback' => [ $this, 'get_item' ],
64 'permission_callback' => [ $this, 'permissions_check' ],
65 'args' => [
66 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
67 ],
68 ],
69 'schema' => [ $this, 'get_item_schema' ],
70 ] );
71
72 // @TODO implement update method.
73 // @TODO implement delete method.
74 }
75
76 public function check_admin_permissions() {
77 return Helper::check_rest_user_cap( 'manage_options' );
78 }
79
80 public function check_payment_permissions( WP_REST_Request $request ): bool {
81 $order_id = $request->get_param( 'order_id' );
82 if ( $order_id ) {
83 $order = Helper::get_order( $order_id );
84
85 return $order instanceof Order;
86 }
87
88 return false;
89 }
90
91 public function permissions_check() {
92 return Helper::check_rest_user_cap( 'manage_options' );
93 }
94
95 public function create_payment(): WP_Error {
96 return new WP_Error(
97 'endpoint_is_deprecated',
98 __( 'Please use checkout workflow. this endpoint is deprecated.', 'storeengine' ),
99 [ 'status' => 400 ]
100 );
101 }
102
103 public function get_payment( $request ) {
104 $id = 0;
105
106 foreach ( [ 'id', 'order', 'order_id', 'payment', 'payment_id' ] as $key ) {
107 if ( $request->has_param( $key ) && $request->get_param( $key ) ) {
108 $id = absint( $request->get_param( $key ) );
109 break;
110 }
111 }
112
113 if ( ! $id ) {
114 return new WP_Error( 'invalid-request', __( 'Invalid request', 'storeengine' ), [ 'status' => 400 ] );
115 }
116
117 $order = Helper::get_order( $id );
118
119 if ( ! is_wp_error( $order ) ) {
120 return $order;
121 }
122
123 return rest_ensure_response( $this->prepare_item_for_response( $order, $request ) );
124 }
125
126 public function collect_payment(): WP_Error {
127 return new WP_Error( 'not-implemented', __( 'Not implemented.', 'storeengine' ), [ 'status' => 501 ] );
128 }
129
130 public function get_payments( $request ) {
131 $page = max( 1, absint( $request->get_param( 'page' ) ) );
132 $status = $request->get_param( 'status' );
133 $search = $request->get_param( 'search' );
134 $customer = $request->get_param( 'customer' );
135 $where = [
136 'relation' => 'AND',
137 [
138 'key' => 'type',
139 'value' => 'order',
140 ],
141 [
142 'key' => 'total_amount',
143 'value' => 0,
144 'compare' => '>',
145 'type' => 'NUMERIC',
146 ],
147 ];
148
149 if ( $status && ! in_array( $status, [ 'all', 'any', 'draft', 'auto-draft' ], true ) ) {
150 // An order with no stored status is surfaced under the default status
151 // in the view layer (AbstractOrder::get_status() → the
152 // `storeengine/default_order_status` filter, i.e. pending_payment), so
153 // the table lists those empty-status rows under that tab. The filter
154 // query hits the raw `status` column, so it must match the empty rows
155 // too for that status — otherwise the tab shows nothing while the rows
156 // are visible under "All".
157 $default_status = apply_filters( 'storeengine/default_order_status', OrderStatus::PAYMENT_PENDING );
158
159 if ( $status === $default_status ) {
160 $where[] = [
161 'key' => 'status',
162 'value' => [ $status, '' ],
163 'compare' => 'IN',
164 ];
165 } else {
166 $where[] = [
167 'key' => 'status',
168 'value' => $status,
169 ];
170 }
171 } else {
172 $where[] = [
173 'key' => 'status',
174 'value' => [ 'draft', 'auto-draft' ],
175 'compare' => 'NOT IN',
176 ];
177 }
178
179 if ( $customer ) {
180 $where[] = [
181 'key' => 'customer_id',
182 'value' => $customer,
183 'compare' => '=',
184 'type' => 'NUMERIC',
185 ];
186
187 if ( $search ) {
188 $where[] = [
189 'key' => 'payment_method_title',
190 'value' => "%$search%",
191 'compare' => 'LIKE',
192 ];
193 }
194 } else {
195 if ( $search ) {
196 $where[] = [
197 'relation' => 'OR',
198 [
199 'key' => 'billing_email',
200 'value' => "%$search%",
201 'compare' => 'LIKE',
202 ],
203 [
204 'key' => 'payment_method_title',
205 'value' => "%$search%",
206 'compare' => 'LIKE',
207 ],
208 ];
209 }
210 }
211
212 try {
213 $query = new OrderCollection( [
214 'per_page' => absint( $request->get_param( 'per_page' ) ),
215 'page' => $page,
216 'where' => $where,
217 ] );
218
219 $payments = [];
220
221 foreach ( $query->get_results() as $order ) {
222 $response = rest_ensure_response( $this->prepare_item_for_response( $order, $request ) );
223 $response->add_links( $this->prepare_links( $order, $request ) );
224 $payments[] = $this->prepare_response_for_collection( $response );
225 }
226
227 return $this->prepare_query_response( $payments, $query, $request );
228 } catch ( StoreEngineException $exception ) {
229 return rest_ensure_response( $exception->toWpError() );
230 }
231 }
232
233 public function get_item( $request ) {
234 $order = Helper::get_order( $request['id'] );
235
236 if ( ! is_wp_error( $order ) ) {
237 return $order;
238 }
239
240 return rest_ensure_response( $this->prepare_item_for_response( $order, $request ) );
241 }
242
243 /**
244 * @param Order $item object.
245 * @param WP_REST_Request $request Requests.
246 *
247 * @return array
248 */
249 public function prepare_item_for_response( $item, $request ): array {
250 return Helper::get_payment_data( $item );
251 }
252 }
253