| 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\PayplugWoocommerceHelper; |
| 13 |
use Payplug\Resource\IVerifiableAPIResource; |
| 14 |
use WC_Payment_Token_CC; |
| 15 |
|
| 16 |
class PayplugIpnResponse { |
| 17 |
|
| 18 |
private $gateway; |
| 19 |
|
| 20 |
/** |
| 21 |
* PayplugIpnResponse constructor. |
| 22 |
* |
| 23 |
* @param PayplugGateway $gateway |
| 24 |
*/ |
| 25 |
public function __construct( $gateway ) { |
| 26 |
$this->gateway = $gateway; |
| 27 |
add_action( 'woocommerce_api_paypluggateway', [ $this, 'handle_ipn_response' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
public function handle_ipn_response() { |
| 31 |
$input = file_get_contents( 'php://input' ); |
| 32 |
|
| 33 |
try { |
| 34 |
$resource = Notification::treat( $input ); |
| 35 |
} catch ( UnknownAPIResourceException $e ) { |
| 36 |
PayplugGateway::log( sprintf( 'Error while parsing IPN payload : %s', $e->getMessage() ), 'error' ); |
| 37 |
exit; |
| 38 |
} |
| 39 |
|
| 40 |
if ( ! $this->validate_ipn( $resource ) ) { |
| 41 |
PayplugGateway::log( sprintf( 'Resource %s is not supported (Transaction %s).', wc_clean( $resource->object ), wc_clean( $resource->id ) ), 'error' ); |
| 42 |
exit; |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! method_exists( $this, sprintf( 'process_%s_resource', wc_clean( $resource->object ) ) ) ) { |
| 46 |
PayplugGateway::log( sprintf( 'No method found to process resource %s (Transaction %s).', wc_clean( $resource->object ), wc_clean( $resource->id ) ), 'error' ); |
| 47 |
exit; |
| 48 |
} |
| 49 |
|
| 50 |
call_user_func( [ $this, sprintf( 'process_%s_resource', $resource->object ) ], $resource ); |
| 51 |
exit; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Validate IPN notification. |
| 56 |
* |
| 57 |
* @param IVerifiableAPIResource $resource |
| 58 |
* |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
public function validate_ipn( $resource ) { |
| 62 |
return isset( $resource->object ) && in_array( $resource->object, [ 'payment', 'refund' ] ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Process payment notification. |
| 67 |
* |
| 68 |
* @param $resource |
| 69 |
* |
| 70 |
* @return void |
| 71 |
* @throws \WC_Data_Exception |
| 72 |
*/ |
| 73 |
public function process_payment_resource( $resource ) { |
| 74 |
$this->gateway->response->process_payment( $resource ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Process refund notification. |
| 79 |
* |
| 80 |
* @param $resource |
| 81 |
*/ |
| 82 |
public function process_refund_resource( $resource ) { |
| 83 |
$this->gateway->response->process_refund( $resource ); |
| 84 |
} |
| 85 |
} |