PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.4.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.4.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Controllers / Rest / IncomingWebhooksController.php
IncomingWebhooksController.php
134 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Controllers\Rest;
4
5 use SureCart\Models\IncomingWebhook;
6
7 /**
8 * Handle Price requests through the REST API
9 */
10 class IncomingWebhooksController {
11 /**
12 * Create a product integration.
13 *
14 * @param \WP_REST_Request $request Rest Request.
15 *
16 * @return \WP_REST_Response|\WP_Error
17 */
18 public function create( \WP_REST_Request $request ) {
19 return IncomingWebhook::create( $request->get_params() );
20 }
21
22 /**
23 * Retry the incoming webhook.
24 *
25 * @param \WP_REST_Request $request Rest Request.
26 *
27 * @return void
28 */
29 public function retry( \WP_REST_Request $request ) {
30 try {
31 $webhook = \SureCart::async()->handle( $request['id'] );
32 } catch ( \Exception $e ) {
33 return new \WP_Error( 'webhook_retry_failed', $e->getMessage() );
34 }
35
36 if ( is_wp_error( $webhook ) ) {
37 return $webhook;
38 }
39
40 if ( empty( $webhook ) ) {
41 return new \WP_Error( 'webhook_retry_failed', 'Webhook retry failed.' );
42 }
43
44 return $webhook->toArray();
45 }
46
47 /**
48 * List all product integrations.
49 *
50 * @param \WP_REST_Request $request Rest Request.
51 *
52 * @return \WP_REST_Response|\WP_Error
53 */
54 public function index( \WP_REST_Request $request ) {
55 $webhook = new IncomingWebhook();
56
57 // processed is false.
58 if ( false === $request->get_param( 'processed' ) ) {
59 $webhook = $webhook->whereNull( 'processed_at' );
60 } elseif ( ! empty( $request->get_param( 'processed' ) ) ) {
61 $webhook = $webhook->whereNotNull( 'processed_at' );
62 }
63
64 // webhook ids.
65 if ( $request->get_param( 'webhook_ids' ) ) {
66 $webhook = $webhook->whereIn( 'webhook_ids', $request->get_param( 'webhook_ids' ) );
67 }
68
69 $total = $webhook->count();
70 $page = $request->get_param( 'page' ) ? $request->get_param( 'page' ) : 1;
71 $per_page = $request->get_param( 'per_page' ) ? $request->get_param( 'per_page' ) : 10;
72
73 // handle pagination.
74 $items = $webhook->paginate(
75 [
76 'page' => $page,
77 'per_page' => $per_page,
78 ]
79 );
80
81 $response = rest_ensure_response( $items );
82 $response->header( 'X-WP-Total', (int) $total );
83 $response->header( 'X-WP-TotalPages', (int) ceil( $total / (int) $per_page ) );
84
85 return $response;
86 }
87
88 /**
89 * Find a specific product integration.
90 *
91 * @param \WP_REST_Request $request Rest Request.
92 *
93 * @return \WP_REST_Response|\WP_Error
94 */
95 public function find( \WP_REST_Request $request ) {
96 $webhook = new IncomingWebhook();
97
98 // processed is false.
99 if ( false === $request->get_param( 'processed' ) ) {
100 $webhook = $webhook->whereNull( 'processed' );
101 } elseif ( ! empty( $request->get_param( 'processed' ) ) ) {
102 $webhook = $webhook->whereNotNull( 'processed' );
103 }
104
105 if ( $request->get_param( 'webhook_id' ) ) {
106 $webhook = $webhook->where( 'webhook_id', $request->get_param( 'webhook_id' ) );
107 }
108
109 return $webhook->find( $request['id'] );
110 }
111
112 /**
113 * Edit model.
114 *
115 * @param \WP_REST_Request $request Rest Request.
116 *
117 * @return \WP_REST_Response|\WP_Error
118 */
119 public function edit( \WP_REST_Request $request ) {
120 return IncomingWebhook::where( $request->get_query_params() )->update( array_diff_assoc( $request->get_params(), $request->get_query_params() ) );
121 }
122
123 /**
124 * Delete model.
125 *
126 * @param \WP_REST_Request $request Rest Request.
127 *
128 * @return \WP_REST_Response|\WP_Error
129 */
130 public function delete( \WP_REST_Request $request ) {
131 return IncomingWebhook::delete( $request['id'] );
132 }
133 }
134