PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 1.1.4
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v1.1.4
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 / Web / WebhookController.php
WebhookController.php
175 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace SureCart\Controllers\Web;
3
4 use SureCart\Models\Webhook;
5 use SureCart\Webhooks\WebhooksHistoryService;
6 use SureCartCore\Responses\RedirectResponse;
7
8 /**
9 * Handles webhooks
10 */
11 class WebhookController {
12 /**
13 * Map object names to their models.
14 *
15 * @var array
16 */
17 protected $models = [
18 'charge' => \SureCart\Models\Charge::class,
19 'coupon' => \SureCart\Models\Coupon::class,
20 'customer' => \SureCart\Models\Customer::class,
21 'purchase' => \SureCart\Models\Purchase::class,
22 'price' => \SureCart\Models\Price::class,
23 'product' => \SureCart\Models\Product::class,
24 'period' => \SureCart\Models\Period::class,
25 'order' => \SureCart\Models\Order::class,
26 'refund' => \SureCart\Models\Refund::class,
27 'invoice' => \SureCart\Models\Invoice::class,
28 ];
29
30 /**
31 * Remove the webhook.
32 *
33 * @param \SureCartCore\Requests\RequestInterface $request Request.
34 * @return function
35 */
36 public function remove( $request ) {
37 $deleted = Webhook::delete( $request->query( 'id' ) );
38 if ( is_wp_error( $deleted ) ) {
39 wp_die( $deleted->get_error_message() );
40 }
41 return ( new RedirectResponse( $request ) )->back();
42 }
43
44 /**
45 * Remove the webhook.
46 *
47 * @param \SureCartCore\Requests\RequestInterface $request Request.
48 * @return function
49 */
50 public function ignore( $request ) {
51 $service = new WebHooksHistoryService();
52 $service->deletePreviousWebhook();
53 return ( new RedirectResponse( $request ) )->back();
54 }
55
56 /**
57 * Create a webhook for this install.
58 */
59 public function create() {
60 return Webhook::create(
61 [
62 'description' => 'Main webhook for SureCart',
63 'enabled' => true,
64 'url' => \SureCart::routeUrl( 'webhooks.receive' ),
65 ]
66 );
67 }
68
69 /**
70 * Recieve webhook
71 */
72 public function receive( $request ) {
73 // get json if sent.
74 if ( 'application/json' === $request->getHeaderLine( 'Content-Type' ) ) {
75 $body = json_decode( $request->getBody(), true );
76 } else {
77 $body = $request->getParsedBody();
78 }
79 // perform the action.
80 $action = $this->doAction( $body );
81 // handle the response.
82 return $this->handleResponse( $action );
83 }
84
85 /**
86 * Handle the response back to the webhook.
87 *
88 * @param array|\WP_Error $data Data.
89 * @return function
90 */
91 public function handleResponse( $data ) {
92 // handle the response.
93 if ( is_wp_error( $data ) ) {
94 return \SureCart::json( [ $data->get_error_code() => $data->get_error_message() ] )
95 ->withHeader( 'X-SURECART-WP-PLUGIN-VERSION', \SureCart::plugin()->version() )
96 ->withStatus( 500 );
97 }
98
99 if ( empty( $data ) ) {
100 return \SureCart::json( [ 'failed' => true ] )
101 ->withHeader( 'X-SURECART-WP-PLUGIN-VERSION', \SureCart::plugin()->version() )
102 ->withStatus( 400 );
103 }
104
105 return \SureCart::json(
106 [
107 'event_triggered' => $data['event'] ?? 'none',
108 'data' => $data,
109 ]
110 )->withHeader( 'X-SURECART-WP-PLUGIN-VERSION', \SureCart::plugin()->version() );
111 }
112
113 /**
114 * Perform the action.
115 *
116 * @param object $request Request.
117 *
118 * @return array|\WP_Error
119 */
120 public function doAction( $request ) {
121 if ( empty( $request['type'] ) ) {
122 return new \WP_Error( 'missing_type', 'Missing type.' );
123 }
124 if ( empty( $request['data'] ) ) {
125 return new \WP_Error( 'missing_data', 'Missing data.' );
126 }
127
128 // create the event name.
129 $event = $this->createEventName( $request['type'] );
130 $id = $this->getObjectId( $request['data'] );
131 $model = new $this->models[ $request['data']['object']['object'] ]( $request['data'] );
132
133 // broadcast the webhook.
134 do_action( $event, $model, $request );
135
136 // return data.
137 return [
138 'event' => $event,
139 'id' => $id,
140 'request' => $request,
141 ];
142 }
143
144 /**
145 * Replace our dot notation webhook with underscore.
146 *
147 * @param string $type The event type.
148 * @return string
149 */
150 public function createEventName( $type = '' ) {
151 $type = str_replace( '.', '_', $type );
152 return "surecart/$type";
153 }
154
155 /**
156 * Get the first object property in data.
157 *
158 * @param object $data Request data.
159 * @return string
160 */
161 public function getObjectId( $data ) {
162 return $data->object->id ?? '';
163 }
164
165 /**
166 * Find the object name.
167 *
168 * @param object|array $data Request data.
169 * @return string
170 */
171 public function getObjectName( $data ) {
172 return array_key_first( (array) $data );
173 }
174 }
175