| 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 |
'order' => \SureCart\Models\Order::class, |
| 25 |
'refund' => \SureCart\Models\Refund::class, |
| 26 |
'invoice' => \SureCart\Models\Invoice::class, |
| 27 |
]; |
| 28 |
|
| 29 |
/** |
| 30 |
* Remove the webhook. |
| 31 |
* |
| 32 |
* @param \SureCartCore\Requests\RequestInterface $request Request. |
| 33 |
* @return function |
| 34 |
*/ |
| 35 |
public function remove( $request ) { |
| 36 |
$deleted = Webhook::delete( $request->query( 'id' ) ); |
| 37 |
if ( is_wp_error( $deleted ) ) { |
| 38 |
wp_die( $deleted->get_error_message() ); |
| 39 |
} |
| 40 |
return ( new RedirectResponse( $request ) )->back(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Remove the webhook. |
| 45 |
* |
| 46 |
* @param \SureCartCore\Requests\RequestInterface $request Request. |
| 47 |
* @return function |
| 48 |
*/ |
| 49 |
public function ignore( $request ) { |
| 50 |
$service = new WebHooksHistoryService(); |
| 51 |
$service->deletePreviousWebhook(); |
| 52 |
return ( new RedirectResponse( $request ) )->back(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Create a webhook for this install. |
| 57 |
*/ |
| 58 |
public function create() { |
| 59 |
return Webhook::create( |
| 60 |
[ |
| 61 |
'description' => 'Main webhook for SureCart', |
| 62 |
'enabled' => true, |
| 63 |
'url' => \SureCart::routeUrl( 'webhooks.receive' ), |
| 64 |
] |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Recieve webhook |
| 70 |
*/ |
| 71 |
public function receive( $request ) { |
| 72 |
// get json if sent. |
| 73 |
if ( 'application/json' === $request->getHeaderLine( 'Content-Type' ) ) { |
| 74 |
$body = json_decode( $request->getBody(), true ); |
| 75 |
} else { |
| 76 |
$body = $request->getParsedBody(); |
| 77 |
} |
| 78 |
// perform the action. |
| 79 |
$action = $this->doAction( $body ); |
| 80 |
// handle the response. |
| 81 |
return $this->handleResponse( $action ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Handle the response back to the webhook. |
| 86 |
* |
| 87 |
* @param array|\WP_Error $data Data. |
| 88 |
* @return function |
| 89 |
*/ |
| 90 |
public function handleResponse( $data ) { |
| 91 |
// handle the response. |
| 92 |
if ( is_wp_error( $data ) ) { |
| 93 |
return \SureCart::json( [ $data->get_error_code() => $data->get_error_message() ] ) |
| 94 |
->withHeader( 'X-SURECART-WP-PLUGIN-VERSION', \SureCart::plugin()->version() ) |
| 95 |
->withStatus( 500 ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( empty( $data ) ) { |
| 99 |
return \SureCart::json( [ 'failed' => true ] ) |
| 100 |
->withHeader( 'X-SURECART-WP-PLUGIN-VERSION', \SureCart::plugin()->version() ) |
| 101 |
->withStatus( 400 ); |
| 102 |
} |
| 103 |
|
| 104 |
return \SureCart::json( |
| 105 |
[ |
| 106 |
'event_triggered' => $data['event'] ?? 'none', |
| 107 |
'data' => $data, |
| 108 |
] |
| 109 |
)->withHeader( 'X-SURECART-WP-PLUGIN-VERSION', \SureCart::plugin()->version() ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Perform the action. |
| 114 |
* |
| 115 |
* @param object $request Request. |
| 116 |
* |
| 117 |
* @return array|\WP_Error |
| 118 |
*/ |
| 119 |
public function doAction( $request ) { |
| 120 |
if ( empty( $request['type'] ) ) { |
| 121 |
return new \WP_Error( 'missing_type', 'Missing type.' ); |
| 122 |
} |
| 123 |
if ( empty( $request['data'] ) ) { |
| 124 |
return new \WP_Error( 'missing_data', 'Missing data.' ); |
| 125 |
} |
| 126 |
|
| 127 |
// create the event name. |
| 128 |
$event = $this->createEventName( $request['type'] ); |
| 129 |
$id = $this->getObjectId( $request['data'] ); |
| 130 |
$model = new $this->models[ $request['data']['object']['object'] ]( $request['data'] ); |
| 131 |
|
| 132 |
// broadcast the webhook. |
| 133 |
do_action( $event, $model, $request ); |
| 134 |
|
| 135 |
// return data. |
| 136 |
return [ |
| 137 |
'event' => $event, |
| 138 |
'id' => $id, |
| 139 |
'request' => $request, |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Replace our dot notation webhook with underscore. |
| 145 |
* |
| 146 |
* @param string $type The event type. |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
public function createEventName( $type = '' ) { |
| 150 |
$type = str_replace( '.', '_', $type ); |
| 151 |
return "surecart/$type"; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get the first object property in data. |
| 156 |
* |
| 157 |
* @param object $data Request data. |
| 158 |
* @return string |
| 159 |
*/ |
| 160 |
public function getObjectId( $data ) { |
| 161 |
return $data->object->id ?? ''; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Find the object name. |
| 166 |
* |
| 167 |
* @param object|array $data Request data. |
| 168 |
* @return string |
| 169 |
*/ |
| 170 |
public function getObjectName( $data ) { |
| 171 |
return array_key_first( (array) $data ); |
| 172 |
} |
| 173 |
} |
| 174 |
|