| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\Background; |
| 4 |
|
| 5 |
use SureCart\Models\IncomingWebhook; |
| 6 |
|
| 7 |
/** |
| 8 |
* SureCart Queue |
| 9 |
* |
| 10 |
* A job queue using WordPress actions. |
| 11 |
*/ |
| 12 |
class AsyncWebhookService extends AsyncRequest { |
| 13 |
/** |
| 14 |
* Map object names to their models. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $models = [ |
| 19 |
'charge' => \SureCart\Models\Charge::class, |
| 20 |
'coupon' => \SureCart\Models\Coupon::class, |
| 21 |
'customer' => \SureCart\Models\Customer::class, |
| 22 |
'purchase' => \SureCart\Models\Purchase::class, |
| 23 |
'price' => \SureCart\Models\Price::class, |
| 24 |
'product' => \SureCart\Models\Product::class, |
| 25 |
'period' => \SureCart\Models\Period::class, |
| 26 |
'order' => \SureCart\Models\Order::class, |
| 27 |
'refund' => \SureCart\Models\Refund::class, |
| 28 |
'subscription' => \SureCart\Models\Subscription::class, |
| 29 |
'invoice' => \SureCart\Models\Invoice::class, |
| 30 |
'account' => \SureCart\Models\Account::class, |
| 31 |
'webhook_endpoint' => \SureCart\Models\Webhook::class, |
| 32 |
]; |
| 33 |
|
| 34 |
/** |
| 35 |
* Enqueue an action to run one time, as soon as possible |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $prefix = 'surecart'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Action for ajax hooks. |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
protected $action = 'async_webhook'; |
| 47 |
|
| 48 |
/** |
| 49 |
* Handle a dispatched request. |
| 50 |
* |
| 51 |
* @param integer $id The webhook process id. |
| 52 |
* @return void |
| 53 |
* @throws \Exception If no id is specified. |
| 54 |
*/ |
| 55 |
public function handle( $id = 0 ) { |
| 56 |
// get the webhook. |
| 57 |
$id = $id ? $id : $_POST['id']; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 58 |
|
| 59 |
// get the event name. |
| 60 |
if ( empty( $id ) ) { |
| 61 |
error_log( 'No id specified for webhook' ); |
| 62 |
throw new \Exception( 'No id specified for webhook' ); |
| 63 |
} |
| 64 |
|
| 65 |
// find the webhook. |
| 66 |
$webhook = IncomingWebhook::find( $id ); |
| 67 |
|
| 68 |
// get WP error and throw exception. |
| 69 |
if ( is_wp_error( $webhook ) ) { |
| 70 |
error_log( 'SureCart Webhook Processing Error (' . esc_html( $id ) . ') ' . $webhook->get_error_message() ); |
| 71 |
throw new \Exception( $webhook->get_error_message() ); |
| 72 |
} |
| 73 |
|
| 74 |
// get the event name. |
| 75 |
if ( empty( $webhook->data->type ) ) { |
| 76 |
error_log( 'No event specified for webhook' ); |
| 77 |
throw new \Exception( 'No event specified for webhook' ); |
| 78 |
} |
| 79 |
|
| 80 |
$object_name = $webhook->data->data->object->object ?? ''; |
| 81 |
|
| 82 |
// get model. |
| 83 |
$class = $this->models[ $object_name ] ?? null; |
| 84 |
|
| 85 |
// We don't have a model. That's okay since we only subscribe to specific items. |
| 86 |
if ( empty( $class ) ) { |
| 87 |
$webhook->update( [ 'processed_at' => current_time( 'mysql' ) ] ); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// do the action. |
| 92 |
do_action( $this->createEventName( $webhook->data->type ), new $class( $webhook->data->data->object ), $webhook->data ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 93 |
|
| 94 |
// update as processed. |
| 95 |
$webhook->update( [ 'processed_at' => current_time( 'mysql' ) ] ); |
| 96 |
|
| 97 |
// update. |
| 98 |
return $webhook; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Replace our dot notation webhook with underscore. |
| 103 |
* |
| 104 |
* @param string $type The event type. |
| 105 |
* @return string |
| 106 |
*/ |
| 107 |
public function createEventName( $type = '' ) { |
| 108 |
$type = str_replace( '.', '_', $type ); |
| 109 |
return "surecart/$type"; |
| 110 |
} |
| 111 |
} |
| 112 |
|