| @@ -2,84 +2,101 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Payplug\PayplugWoocommerce\Gateway; |
| 4 | 4 | |
| 5 | 5 | // Exit if accessed directly |
| 6 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 7 | - exit; | |
| 6 | +if (!defined('ABSPATH')) { | |
| 7 | + exit; | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | use Payplug\Exception\UnknownAPIResourceException; |
| 11 | 11 | use Payplug\Notification; |
| 12 | -use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; | |
| 12 | +use Payplug\PayplugWoocommerce\Model\Lock; | |
| 13 | 13 | use Payplug\Resource\IVerifiableAPIResource; |
| 14 | -use WC_Payment_Token_CC; | |
| 15 | 14 | |
| 16 | -class PayplugIpnResponse { | |
| 15 | +class PayplugIpnResponse | |
| 16 | +{ | |
| 17 | + private $gateway; | |
| 17 | 18 | |
| 18 | - private $gateway; | |
| 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 | + } | |
| 19 | 29 | |
| 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 | - } | |
| 30 | + public function handle_ipn_response(): void | |
| 31 | + { | |
| 32 | + $input = file_get_contents('php://input'); | |
| 29 | 33 | |
| 30 | - public function handle_ipn_response() { | |
| 31 | - $input = file_get_contents( 'php://input' ); | |
| 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 | + } | |
| 32 | 40 | |
| 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 | - } | |
| 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 | + } | |
| 39 | 45 | |
| 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 | - } | |
| 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 | + } | |
| 44 | 50 | |
| 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 | - } | |
| 51 | + call_user_func([$this, sprintf('process_%s_resource', $resource->object)], $resource); | |
| 52 | + exit; | |
| 53 | + } | |
| 49 | 54 | |
| 50 | - call_user_func( [ $this, sprintf( 'process_%s_resource', $resource->object ) ], $resource ); | |
| 51 | - exit; | |
| 52 | - } | |
| 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 | + } | |
| 53 | 66 | |
| 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 | - } | |
| 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'); | |
| 64 | 83 | |
| 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 | - } | |
| 84 | + Lock::delete_lock($lock_id); | |
| 85 | + $waiting_requests = Lock::get_lock_by_payment_id($resource->id); | |
| 76 | 86 | |
| 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 | -} | |
| 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 | +} | |