| 1 |
<?php |
| 2 |
/** |
| 3 |
* Webhook: Create endpoint on Stripe Connect connection |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.4.2 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core\Webhook; |
| 13 |
|
| 14 |
use Exception; |
| 15 |
use SimplePay\Core\EventManagement\SubscriberInterface; |
| 16 |
use SimplePay\Core\License\LicenseAwareInterface; |
| 17 |
use SimplePay\Core\License\LicenseAwareTrait; |
| 18 |
|
| 19 |
/** |
| 20 |
* StripeConnectSync class. |
| 21 |
* |
| 22 |
* @since 4.4.2 |
| 23 |
*/ |
| 24 |
class StripeConnectSync implements SubscriberInterface, LicenseAwareInterface { |
| 25 |
|
| 26 |
use LicenseAwareTrait; |
| 27 |
|
| 28 |
/** |
| 29 |
* Webhook endpoint manager. |
| 30 |
* |
| 31 |
* @since 4.4.2 |
| 32 |
* @var \SimplePay\Core\Webhook\WebhookEndpointManager $endpoint |
| 33 |
*/ |
| 34 |
private $endpoint; |
| 35 |
|
| 36 |
/** |
| 37 |
* StripeConnectSync. |
| 38 |
* |
| 39 |
* @since 4.4.2 |
| 40 |
* |
| 41 |
* @param \SimplePay\Core\Webhook\WebhookEndpointManager $endpoint Webhook endpoint manager. |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function __construct( WebhookEndpointManager $endpoint ) { |
| 45 |
$this->endpoint = $endpoint; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* {@inheritdoc} |
| 50 |
*/ |
| 51 |
public function get_subscribed_events() { |
| 52 |
if ( true === $this->license->is_lite() ) { |
| 53 |
return array(); |
| 54 |
} |
| 55 |
|
| 56 |
return array( |
| 57 |
'simpay_stripe_account_connected' => 'add_update_endpoint', |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Adds or updates a webhook endpoint on Stripe Connect connection. |
| 63 |
* |
| 64 |
* @since 4.4.2 |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function add_update_endpoint() { |
| 69 |
try { |
| 70 |
$endpoint = $this->endpoint->exists(); |
| 71 |
|
| 72 |
// Endpoint does not exist, create one. |
| 73 |
if ( ! $endpoint instanceof \SimplePay\Vendor\Stripe\WebhookEndpoint ) { |
| 74 |
$this->endpoint->create(); |
| 75 |
|
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// Endpoint exists but it is no longer valid, update it. |
| 80 |
if ( false === $this->endpoint->is_valid( $endpoint ) ) { |
| 81 |
$this->endpoint->update( $endpoint ); |
| 82 |
|
| 83 |
return; |
| 84 |
} |
| 85 |
} catch ( Exception $e ) { |
| 86 |
// Fail silently. |
| 87 |
// @todo Show a separate notice? |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|