| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
use Payplug\Exception\UnknownAPIResourceException; |
| 11 |
use Payplug\Notification; |
| 12 |
use Payplug\PayplugWoocommerce\Model\Lock; |
| 13 |
use Payplug\Resource\IVerifiableAPIResource; |
| 14 |
|
| 15 |
class PayplugIpnResponse |
| 16 |
{ |
| 17 |
private $gateway; |
| 18 |
|
| 19 |
/** |
| 20 |
* PayplugIpnResponse constructor. |
| 21 |
* |
| 22 |
* @param PayplugGateway $gateway |
| 23 |
*/ |
| 24 |
public function __construct($gateway) |
| 25 |
{ |
| 26 |
$this->gateway = $gateway; |
| 27 |
add_action('woocommerce_api_paypluggateway', [$this, 'handle_ipn_response']); |
| 28 |
} |
| 29 |
|
| 30 |
public function handle_ipn_response(): void |
| 31 |
{ |
| 32 |
$input = file_get_contents('php://input'); |
| 33 |
|
| 34 |
try { |
| 35 |
$resource = Notification::treat($input); |
| 36 |
} catch (UnknownAPIResourceException $e) { |
| 37 |
PayplugGateway::log(sprintf('Error while parsing IPN payload : %s', $e->getMessage()), 'error'); |
| 38 |
exit; |
| 39 |
} |
| 40 |
|
| 41 |
if (!$this->validate_ipn($resource)) { |
| 42 |
PayplugGateway::log(sprintf('Resource %s is not supported (Transaction %s).', wc_clean($resource->object), wc_clean($resource->id)), 'error'); |
| 43 |
exit; |
| 44 |
} |
| 45 |
|
| 46 |
if (!method_exists($this, sprintf('process_%s_resource', wc_clean($resource->object)))) { |
| 47 |
PayplugGateway::log(sprintf('No method found to process resource %s (Transaction %s).', wc_clean($resource->object), wc_clean($resource->id)), 'error'); |
| 48 |
exit; |
| 49 |
} |
| 50 |
|
| 51 |
call_user_func([$this, sprintf('process_%s_resource', $resource->object)], $resource); |
| 52 |
exit; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Validate IPN notification. |
| 57 |
* |
| 58 |
* @param IVerifiableAPIResource $resource |
| 59 |
* |
| 60 |
* @return bool |
| 61 |
*/ |
| 62 |
public function validate_ipn($resource) |
| 63 |
{ |
| 64 |
return isset($resource->object) && in_array($resource->object, ['payment', 'refund']); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Process payment notification. |
| 69 |
* |
| 70 |
* @param $resource |
| 71 |
* |
| 72 |
* @throws \WC_Data_Exception |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function process_payment_resource($resource, $save_request = true): void |
| 77 |
{ |
| 78 |
$lock_id = \Payplug\PayplugWoocommerce\Helper\Lock::handle_insert($save_request, $resource->id); |
| 79 |
if (!$lock_id) { |
| 80 |
return; |
| 81 |
} |
| 82 |
$this->gateway->response->process_payment($resource, false, 'ipn'); |
| 83 |
|
| 84 |
Lock::delete_lock($lock_id); |
| 85 |
$waiting_requests = Lock::get_lock_by_payment_id($resource->id); |
| 86 |
|
| 87 |
if ($waiting_requests) { |
| 88 |
Lock::delete_lock_by_payment_id($resource->id); |
| 89 |
$this->gateway->validate_payment($resource->metadata['order_id'], false, true); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Process refund notification. |
| 95 |
* |
| 96 |
* @param $resource |
| 97 |
*/ |
| 98 |
public function process_refund_resource($resource): void |
| 99 |
{ |
| 100 |
$this->gateway->response->process_refund($resource); |
| 101 |
} |
| 102 |
} |
| 103 |
|