PluginProbe
PayPlug for WooCommerce (Official) / 2.6.3
PayPlug for WooCommerce (Official) v2.6.3
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 / PayplugWoocommerceRequest.php

PayplugWoocommerceRequest.php in PayPlug for WooCommerce (Official) 2.6.3, at src/PayplugWoocommerceRequest.php

277 lines 6.8 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;
4
5 // Exit if accessed directly
6 use Payplug\PayplugWoocommerce\Gateway\PayplugAddressData;
7 use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
8 use WC_Payment_Tokens;
9 use WP_REST_Request;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Class PayplugWoocommerceRequest
17 * @package Payplug\PayplugWoocommerce
18 */
19 class PayplugWoocommerceRequest {
20
21 /**
22 * Gateway settings.
23 *
24 * @var array
25 */
26 protected $settings;
27
28 /**
29 * PayplugWoocommerceRequest constructor.
30 */
31 public function __construct() {
32 $this->settings = get_option( 'woocommerce_payplug_settings', [] );
33 if ( empty( $this->settings ) || ! isset( $this->settings['enabled'] ) || 'yes' !== $this->settings['enabled'] ) {
34 return;
35 }
36
37 // Don't load for change payment method page.
38 if ( isset( $_GET['change_payment_method'] ) ) {
39 return;
40 }
41
42 add_action( 'template_redirect', [ $this, 'set_session' ] );
43 add_action( 'wc_ajax_payplug_create_order', [ $this, 'ajax_create_order' ] );
44 add_action( 'wc_ajax_applepay_update_payment', [ $this, 'applepay_update_payment' ] );
45 add_action( 'wc_ajax_applepay_get_order_totals', [ $this, 'applepay_get_order_totals' ] );
46 add_action( 'wc_ajax_payplug_order_review_url', [ $this, 'ajax_create_payment' ] );
47 add_action( 'wc_ajax_payplug_check_payment', [$this, 'check_payment']);
48
49 }
50
51 /**
52 * Sets the WC customer session if one is not set.
53 * This is needed so nonces can be verified by AJAX Request.
54 */
55 public function set_session() {
56 if ( ! is_product() || ( isset( WC()->session ) && WC()->session->has_session() ) ) {
57 return;
58 }
59
60 $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
61 $wc_session = new $session_class();
62
63 if ( version_compare( WC_VERSION, '3.3', '>=' ) ) {
64 $wc_session->init();
65 }
66
67 $wc_session->set_customer_session_cookie( true );
68 }
69
70 /**
71 * Create the woocommerce order in the BO
72 *
73 */
74 public function ajax_create_order() {
75 if ( WC()->cart->is_empty() ) {
76 wp_send_json_error( __( 'Empty cart', 'payplug' ) );
77 }
78
79 if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
80 define( 'WOOCOMMERCE_CHECKOUT', true );
81 }
82
83 WC()->checkout()->process_checkout();
84
85 die( 0 );
86 }
87
88 /**
89 * Create the woocommerce order in the BO
90 *
91 */
92 public function ajax_create_payment() {
93
94 if ( WC()->cart->is_empty() ) {
95 wp_send_json_error( __( 'Empty cart', 'payplug' ) );
96 }
97
98 if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
99 define( 'WOOCOMMERCE_CHECKOUT', true );
100 }
101
102 $payment_method = $_POST['payment_method'];
103
104 //TODO:: check if integrated or embedded is activated, if not go to ajax_create_order as well
105 if($payment_method === 'payplug'){
106 $settings = get_option('woocommerce_payplug_settings', []);
107 $method = $settings['payment_method'];
108
109 if($method !== "integrated" && $method !== "popup" ){
110 $this->ajax_create_order();
111 }
112
113 }else{
114 $this->ajax_create_order();
115 }
116
117 $https_referer = $_POST['_wp_http_referer'];
118 $path = parse_url($https_referer);
119 wp_parse_str($path['query'], $output);
120 $order_id = $output['order-pay'];
121
122 $this->process_order_payment($order_id, $payment_method);
123
124 }
125
126 /**
127 * wordpress class-wc-checkout.php
128 * We don't need all the other verifications, at this point customer already had the checkout and it's all valid, the order already exists
129 * We can+t use WP method because it's protected
130 * Process an order that does require payment.
131 *
132 * @since 3.0.0
133 * @param int $order_id Order ID.
134 * @param string $payment_method Payment method.
135 */
136 protected function process_order_payment( $order_id, $payment_method ) {
137 $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
138
139 if ( ! isset( $available_gateways[ $payment_method ] ) ) {
140 return;
141 }
142
143 // Store Order ID in session so it can be re-used after payment failure.
144 WC()->session->set( 'order_awaiting_payment', $order_id );
145
146 // Process Payment.
147 $result = $available_gateways[ $payment_method ]->process_payment( $order_id );
148
149 // Redirect to success/confirmation/payment page.
150 if ( isset( $result['result'] ) && 'success' === $result['result'] ) {
151 $result['order_id'] = $order_id;
152
153 $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id );
154
155 if ( ! wp_doing_ajax() ) {
156 // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
157 wp_redirect( $result['redirect'] );
158 exit;
159 }
160
161 wp_send_json( $result );
162 }
163 }
164
165
166
167 /**
168 * Update Payplug API Payment for Apple Pay
169 */
170 public function applepay_update_payment() {
171
172 $options = get_option('woocommerce_payplug_settings', []);
173 $order_id = $_POST['order_id'];
174 $payment_id = $_POST['payment_id'];
175
176 try{
177 \Payplug\Payplug::init(array(
178 'secretKey' => @$options['payplug_live_key'],
179 'apiVersion' => "2019-08-06",
180 ));
181
182 $apple_pay = array();
183 $apple_pay['payment_token'] = $_POST['payment_token'];
184 $payment = \Payplug\Payment::retrieve($payment_id);
185
186 $data = array( 'apple_pay' => $apple_pay );
187 $update = $payment->update($data);
188
189 wp_send_json_success([ "result" => $update->is_paid ]);
190
191 }catch (\Exception $e){
192 wp_send_json_error($e->getMessage());
193 }
194 }
195
196 public function applepay_get_order_totals()
197 {
198 try {
199 wp_send_json_success(WC()->cart->total);
200
201 } catch (\Exception $e) {
202 PayplugGateway::log($e->getMessage());
203 wp_send_json_error($e->getMessage());
204 }
205 }
206 /**
207 * Limit string length.
208 *
209 * @param string $value
210 * @param int $maxlength
211 *
212 * @return string
213 */
214 public function limit_length($value, $maxlength = 100)
215 {
216 return (strlen($value) > $maxlength) ? substr($value, 0, $maxlength) : $value;
217 }
218
219 public function check_payment() {
220
221 global $wpdb;
222
223 $payment_id = $_POST['payment_id'];
224
225 if (PayplugWoocommerceHelper::check_mode()) {
226 $key = PayplugWoocommerceHelper::get_live_key();
227 } else {
228 $key = PayplugWoocommerceHelper::get_test_key();
229 }
230
231 try {
232 \Payplug\Payplug::init(array(
233 'secretKey' => $key,
234 'apiVersion' => "2019-08-06",
235 ));
236
237 $payment =\Payplug\Payment::retrieve($payment_id);
238
239 } catch ( \Exception $e ) {
240
241 $order_id = $wpdb->get_var(
242 $wpdb->prepare(
243 "
244 SELECT post_id
245 FROM $wpdb->postmeta
246 WHERE meta_key = '_transaction_id'
247 AND meta_value = %s
248 ",
249 $payment_id
250 )
251 );
252
253 PayplugGateway::log(
254 sprintf(
255 'Order #%s : An error occurred while retrieving the payment data with the message : %s',
256 $order_id,
257 $e->getMessage()
258 )
259 );
260
261 return wp_send_json_error( $e->getMessage());
262 }
263
264 if ((isset($payment->failure)) && (!empty($payment->failure)) || ($payment->is_paid === false && is_null($payment->paid_at))) {
265 return wp_send_json_error(
266 [
267 'code' => $payment->failure->code,
268 'message' => !empty($payment->failure->message) ? $payment->failure->message :__("payplug_integrated_payment_error", "payplug")
269 ]
270 );
271 }
272 return wp_send_json_success($payment);
273
274 }
275
276 }
277