PluginProbe
PayPlug for WooCommerce (Official) / 2.8.2
PayPlug for WooCommerce (Official) v2.8.2
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.8.2, at src/PayplugWoocommerceRequest.php

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