PluginProbe
PayPlug for WooCommerce (Official) / 2.9.0
PayPlug for WooCommerce (Official) v2.9.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Gateway / PayplugIpnResponse.php

PayplugIpnResponse.php in PayPlug for WooCommerce (Official) 2.9.0, at src/Gateway/PayplugIpnResponse.php

103 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\PayplugWoocommerce\PayplugWoocommerceHelper;
14 use Payplug\Resource\IVerifiableAPIResource;
15 use WC_Payment_Token_CC;
16
17 class PayplugIpnResponse {
18
19 private $gateway;
20
21 /**
22 * PayplugIpnResponse constructor.
23 *
24 * @param PayplugGateway $gateway
25 */
26 public function __construct( $gateway ) {
27 $this->gateway = $gateway;
28 add_action( 'woocommerce_api_paypluggateway', [ $this, 'handle_ipn_response' ] );
29 }
30
31 public function handle_ipn_response() {
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 return isset( $resource->object ) && in_array( $resource->object, [ 'payment', 'refund' ] );
64 }
65
66 /**
67 * Process payment notification.
68 *
69 * @param $resource
70 *
71 * @return void
72 * @throws \WC_Data_Exception
73 */
74 public function process_payment_resource( $resource, $save_request = true ) {
75
76 $lock_id = \Payplug\PayplugWoocommerce\Helper\Lock::handle_insert($save_request, $resource->id);
77 if(!$lock_id){
78 return;
79 }
80
81 $this->gateway->response->process_payment( $resource, false, "ipn" );
82
83 Lock::delete_lock($lock_id);
84 $waiting_requests = Lock::get_lock_by_payment_id($resource->id);
85
86 if($waiting_requests){
87 Lock::delete_lock_by_payment_id($resource->id);
88 $this->gateway->validate_payment($resource->metadata['order_id'], false, true);
89 };
90
91
92 }
93
94 /**
95 * Process refund notification.
96 *
97 * @param $resource
98 */
99 public function process_refund_resource( $resource ) {
100 $this->gateway->response->process_refund( $resource );
101 }
102 }
103