| @@ -2,8 +2,15 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Payplug\PayplugWoocommerce; |
| 4 | 4 | |
| 5 | 5 | // Exit if accessed directly |
| 6 | +use Payplug\PayplugWoocommerce\Controller\WC_Payplug_Intent_Controller as PayplugIntent; | |
| 7 | +use Payplug\PayplugWoocommerce\Gateway\PayplugAddressData; | |
| 8 | +use Payplug\PayplugWoocommerce\Gateway\PayplugGateway; | |
| 9 | +use WC_Payment_Tokens; | |
| 10 | +use WP_REST_Request; | |
| 11 | +use Automattic\WooCommerce\Utilities\OrderUtil; | |
| 12 | + | |
| 6 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | 14 | exit; |
| 8 | 15 | } |
| 9 | 16 | |
| @@ -20,8 +27,13 @@ | ||
| 20 | 27 | */ |
| 21 | 28 | protected $settings; |
| 22 | 29 | |
| 23 | 30 | /** |
| 31 | + * @var PayplugGateway | |
| 32 | + */ | |
| 33 | + protected $gateway; | |
| 34 | + | |
| 35 | + /** | |
| 24 | 36 | * PayplugWoocommerceRequest constructor. |
| 25 | 37 | */ |
| 26 | 38 | public function __construct() { |
| 27 | 39 | $this->settings = get_option( 'woocommerce_payplug_settings', [] ); |
| @@ -35,8 +47,14 @@ | ||
| 35 | 47 | } |
| 36 | 48 | |
| 37 | 49 | add_action( 'template_redirect', [ $this, 'set_session' ] ); |
| 38 | 50 | add_action( 'wc_ajax_payplug_create_order', [ $this, 'ajax_create_order' ] ); |
| 51 | + add_action( 'wc_ajax_applepay_update_payment', [ $this, 'applepay_update_payment' ] ); | |
| 52 | + add_action( 'wc_ajax_applepay_get_order_totals', [ $this, 'applepay_get_order_totals' ] ); | |
| 53 | + add_action( 'wc_ajax_payplug_order_review_url', [ $this, 'ajax_create_payment' ] ); | |
| 54 | + add_action( 'wc_ajax_payplug_check_payment', [$this, 'check_payment']); | |
| 55 | + add_action( 'wc_ajax_payplug_create_intent', [$this, 'create_payment_intent']); | |
| 56 | + | |
| 39 | 57 | } |
| 40 | 58 | |
| 41 | 59 | /** |
| 42 | 60 | * Sets the WC customer session if one is not set. |
| @@ -56,8 +74,12 @@ | ||
| 56 | 74 | |
| 57 | 75 | $wc_session->set_customer_session_cookie( true ); |
| 58 | 76 | } |
| 59 | 77 | |
| 78 | + /** | |
| 79 | + * Create the woocommerce order in the BO | |
| 80 | + * | |
| 81 | + */ | |
| 60 | 82 | public function ajax_create_order() { |
| 61 | 83 | if ( WC()->cart->is_empty() ) { |
| 62 | 84 | wp_send_json_error( __( 'Empty cart', 'payplug' ) ); |
| 63 | 85 | } |
| @@ -69,5 +91,346 @@ | ||
| 69 | 91 | WC()->checkout()->process_checkout(); |
| 70 | 92 | |
| 71 | 93 | die( 0 ); |
| 72 | 94 | } |
| 73 | -} | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Create the woocommerce order in the BO | |
| 98 | + * | |
| 99 | + */ | |
| 100 | + public function ajax_create_payment() { | |
| 101 | + | |
| 102 | + if ( WC()->cart->is_empty() ) { | |
| 103 | + wp_send_json_error( __( 'Empty cart', 'payplug' ) ); | |
| 104 | + } | |
| 105 | + | |
| 106 | + if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { | |
| 107 | + define( 'WOOCOMMERCE_CHECKOUT', true ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + $payment_method = $_POST['payment_method']; | |
| 111 | + | |
| 112 | + //TODO:: check if integrated or embedded is activated, if not go to ajax_create_order as well | |
| 113 | + if($payment_method === 'payplug'){ | |
| 114 | + $settings = get_option('woocommerce_payplug_settings', []); | |
| 115 | + $method = $settings['payment_method']; | |
| 116 | + | |
| 117 | + if($method !== "integrated" && $method !== "popup" ){ | |
| 118 | + $this->ajax_create_order(); | |
| 119 | + } | |
| 120 | + | |
| 121 | + }else{ | |
| 122 | + $this->ajax_create_order(); | |
| 123 | + } | |
| 124 | + | |
| 125 | + $https_referer = $_POST['_wp_http_referer']; | |
| 126 | + $path = parse_url($https_referer); | |
| 127 | + wp_parse_str($path['query'], $output); | |
| 128 | + $order_id = $output['order-pay']; | |
| 129 | + | |
| 130 | + if(is_null($order_id)){ | |
| 131 | + preg_match("/(?<=order-pay\/)\d*/", $path['path'], $matches); | |
| 132 | + $order_id = $matches[0]; | |
| 133 | + } | |
| 134 | + | |
| 135 | + $this->process_order_payment($order_id, $payment_method); | |
| 136 | + | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * wordpress class-wc-checkout.php | |
| 141 | + * 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 | |
| 142 | + * We can+t use WP method because it's protected | |
| 143 | + * Process an order that does require payment. | |
| 144 | + * | |
| 145 | + * @since 3.0.0 | |
| 146 | + * @param int $order_id Order ID. | |
| 147 | + * @param string $payment_method Payment method. | |
| 148 | + */ | |
| 149 | + protected function process_order_payment( $order_id, $payment_method ) { | |
| 150 | + $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); | |
| 151 | + | |
| 152 | + if ( ! isset( $available_gateways[ $payment_method ] ) ) { | |
| 153 | + return; | |
| 154 | + } | |
| 155 | + | |
| 156 | + // Store Order ID in session so it can be re-used after payment failure. | |
| 157 | + WC()->session->set( 'order_awaiting_payment', $order_id ); | |
| 158 | + | |
| 159 | + // Process Payment. | |
| 160 | + $result = $available_gateways[ $payment_method ]->process_payment( $order_id ); | |
| 161 | + | |
| 162 | + // Redirect to success/confirmation/payment page. | |
| 163 | + if ( isset( $result['result'] ) && 'success' === $result['result'] ) { | |
| 164 | + $result['order_id'] = $order_id; | |
| 165 | + | |
| 166 | + $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id ); | |
| 167 | + | |
| 168 | + if ( ! wp_doing_ajax() ) { | |
| 169 | + // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect | |
| 170 | + wp_redirect( $result['redirect'] ); | |
| 171 | + exit; | |
| 172 | + } | |
| 173 | + | |
| 174 | + wp_send_json( $result ); | |
| 175 | + } | |
| 176 | + } | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * Update Payplug API Payment for Apple Pay | |
| 182 | + */ | |
| 183 | + public function applepay_update_payment() { | |
| 184 | + | |
| 185 | + $options = get_option('woocommerce_payplug_settings', []); | |
| 186 | + $order_id = $_POST['order_id']; | |
| 187 | + $payment_id = $_POST['payment_id']; | |
| 188 | + | |
| 189 | + try{ | |
| 190 | + \Payplug\Payplug::init(array( | |
| 191 | + 'secretKey' => @$options['payplug_live_key'], | |
| 192 | + 'apiVersion' => "2019-08-06", | |
| 193 | + )); | |
| 194 | + | |
| 195 | + $apple_pay = array(); | |
| 196 | + $apple_pay['payment_token'] = $_POST['payment_token']; | |
| 197 | + $payment = \Payplug\Payment::retrieve($payment_id); | |
| 198 | + | |
| 199 | + $data = array( 'apple_pay' => $apple_pay ); | |
| 200 | + $update = $payment->update($data); | |
| 201 | + | |
| 202 | + wp_send_json_success([ "result" => $update->is_paid ]); | |
| 203 | + | |
| 204 | + }catch (\Exception $e){ | |
| 205 | + wp_send_json_error($e->getMessage()); | |
| 206 | + } | |
| 207 | + } | |
| 208 | + | |
| 209 | + public function applepay_get_order_totals() | |
| 210 | + { | |
| 211 | + try { | |
| 212 | + wp_send_json_success(WC()->cart->total); | |
| 213 | + | |
| 214 | + } catch (\Exception $e) { | |
| 215 | + PayplugGateway::log($e->getMessage()); | |
| 216 | + wp_send_json_error($e->getMessage()); | |
| 217 | + } | |
| 218 | + } | |
| 219 | + /** | |
| 220 | + * Limit string length. | |
| 221 | + * | |
| 222 | + * @param string $value | |
| 223 | + * @param int $maxlength | |
| 224 | + * | |
| 225 | + * @return string | |
| 226 | + */ | |
| 227 | + public function limit_length($value, $maxlength = 100) | |
| 228 | + { | |
| 229 | + return (strlen($value) > $maxlength) ? substr($value, 0, $maxlength) : $value; | |
| 230 | + } | |
| 231 | + | |
| 232 | + public function check_payment() { | |
| 233 | + | |
| 234 | + global $wpdb; | |
| 235 | + | |
| 236 | + $payment_id = $_POST['payment_id']; | |
| 237 | + | |
| 238 | + if (PayplugWoocommerceHelper::check_mode()) { | |
| 239 | + $key = PayplugWoocommerceHelper::get_live_key(); | |
| 240 | + } else { | |
| 241 | + $key = PayplugWoocommerceHelper::get_test_key(); | |
| 242 | + } | |
| 243 | + | |
| 244 | + try { | |
| 245 | + \Payplug\Payplug::init(array( | |
| 246 | + 'secretKey' => $key, | |
| 247 | + 'apiVersion' => "2019-08-06", | |
| 248 | + )); | |
| 249 | + | |
| 250 | + $payment =\Payplug\Payment::retrieve($payment_id); | |
| 251 | + | |
| 252 | + } catch ( \Exception $e ) { | |
| 253 | + | |
| 254 | + $order_id = $this->getOrderFromPaymentId($payment_id); | |
| 255 | + $order = wc_get_order($order_id); | |
| 256 | + | |
| 257 | + PayplugGateway::log( | |
| 258 | + sprintf( | |
| 259 | + 'Order #%s : An error occurred while retrieving the payment data with the message : %s', | |
| 260 | + $order_id, | |
| 261 | + $e->getMessage() | |
| 262 | + ) | |
| 263 | + ); | |
| 264 | + | |
| 265 | + return wp_send_json_error( $e->getMessage()); | |
| 266 | + } | |
| 267 | + | |
| 268 | + $order_id = $this->getOrderFromPaymentId($payment_id); | |
| 269 | + $order = wc_get_order($order_id); | |
| 270 | + $return_url = esc_url_raw($order->get_checkout_order_received_url()); | |
| 271 | + | |
| 272 | + if ((isset($payment->failure)) && (!empty($payment->failure)) || ($payment->is_paid === false && is_null($payment->paid_at))) { | |
| 273 | + | |
| 274 | + $order->update_status( 'failed', __( 'Order cancelled by customer.', 'woocommerce' ) ); | |
| 275 | + | |
| 276 | + wp_send_json_error( | |
| 277 | + [ | |
| 278 | + 'code' => isset($payment->failure->code) ? $payment->failure->code : 500, | |
| 279 | + 'message' => !empty($payment->failure->message) ? $payment->failure->message :__("payplug_integrated_payment_error", "payplug"), | |
| 280 | + 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()) | |
| 281 | + ] | |
| 282 | + ); | |
| 283 | + } | |
| 284 | + | |
| 285 | + | |
| 286 | + wp_send_json_success(array( | |
| 287 | + 'payment_id' => $payment->id, | |
| 288 | + 'result' => 'success', | |
| 289 | + 'redirect' => !empty($payment->hosted_payment->payment_url) ? $payment->hosted_payment->payment_url : $return_url, | |
| 290 | + 'cancel' => !empty($payment->hosted_payment->cancel_url) ? $payment->hosted_payment->cancel_url : null | |
| 291 | + )); | |
| 292 | + | |
| 293 | + } | |
| 294 | + | |
| 295 | + /** | |
| 296 | + * @param $payment_id | |
| 297 | + * @return int|string|null | |
| 298 | + */ | |
| 299 | + private function getOrderFromPaymentId( $payment_id){ | |
| 300 | + global $wpdb; | |
| 301 | + | |
| 302 | + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { | |
| 303 | + $orders = wc_get_orders( | |
| 304 | + array( | |
| 305 | + 'field_query' => array( | |
| 306 | + array( | |
| 307 | + 'key' => 'transaction_id', | |
| 308 | + 'comparison' => $payment_id | |
| 309 | + ), | |
| 310 | + ), | |
| 311 | + ) | |
| 312 | + ); | |
| 313 | + $order = $orders[0]; | |
| 314 | + $order_id = $order->get_id(); | |
| 315 | + | |
| 316 | + }else{ | |
| 317 | + | |
| 318 | + $sql = "SELECT post_id | |
| 319 | + FROM $wpdb->postmeta | |
| 320 | + WHERE meta_key = '_transaction_id' AND meta_value = %s"; | |
| 321 | + $order_id = $wpdb->get_var( | |
| 322 | + $wpdb->prepare( | |
| 323 | + $sql, | |
| 324 | + $payment_id | |
| 325 | + ) | |
| 326 | + ); | |
| 327 | + } | |
| 328 | + | |
| 329 | + return $order_id; | |
| 330 | + } | |
| 331 | + | |
| 332 | + public function create_payment_intent(){ | |
| 333 | + | |
| 334 | + $order_id = $_POST["order_id"]; | |
| 335 | + $this->gateway = $this->get_payplug_gateway($_POST['gateway']); | |
| 336 | + $order = wc_get_order($order_id); | |
| 337 | + $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id(); | |
| 338 | + $return_url = esc_url_raw($order->get_checkout_order_received_url()); | |
| 339 | + $address_data = PayplugAddressData::from_order($order); | |
| 340 | + $amount = (int) PayplugWoocommerceHelper::get_payplug_amount($order->get_total()); | |
| 341 | + $amount = $this->gateway->validate_order_amount($amount); | |
| 342 | + | |
| 343 | + $payment_data = [ | |
| 344 | + 'amount' => $amount, | |
| 345 | + 'currency' => get_woocommerce_currency(), | |
| 346 | + 'allow_save_card' => $this->gateway->oneclick_available() && (int) $customer_id > 0, | |
| 347 | + 'billing' => $address_data->get_billing(), | |
| 348 | + 'shipping' => $address_data->get_shipping(), | |
| 349 | + 'hosted_payment' => [ | |
| 350 | + 'return_url' => $return_url, | |
| 351 | + ], | |
| 352 | + 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')), | |
| 353 | + 'metadata' => [ | |
| 354 | + 'order_id' => $order_id, | |
| 355 | + 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest', | |
| 356 | + 'domain' => $this->limit_length(esc_url_raw(home_url()), 500), | |
| 357 | + ], | |
| 358 | + ]; | |
| 359 | + | |
| 360 | + if($this->gateway->id === "apple_pay"){ | |
| 361 | + unset($payment_data["allow_save_card"]); | |
| 362 | + | |
| 363 | + $payment_data["payment_method"] = $this->gateway->id; | |
| 364 | + $payment_data["payment_context"] = array( | |
| 365 | + 'apple_pay' => array( | |
| 366 | + 'domain_name' => $this->gateway->domain_name, | |
| 367 | + 'application_data' => base64_encode(json_encode(array( | |
| 368 | + 'apple_pay_domain' => $this->gateway->domain_name, | |
| 369 | + ))) | |
| 370 | + ) | |
| 371 | + ); | |
| 372 | + $payment_data["hosted_payment"]["cancel_url"] = esc_url_raw($order->get_cancel_order_url_raw()); | |
| 373 | + $payment_data["metadata"]["applepay_workflow"] = "checkout"; | |
| 374 | + } | |
| 375 | + | |
| 376 | + if($this->gateway->payment_method === 'integrated' && $this->gateway->id === "payplug") { | |
| 377 | + $payment_data['initiator'] = 'PAYER'; | |
| 378 | + $payment_data['integration'] = 'INTEGRATED_PAYMENT'; | |
| 379 | + unset($payment_data['hosted_payment']['cancel_url']); | |
| 380 | + } | |
| 381 | + | |
| 382 | + if($this->gateway->payment_method === 'popup' && $this->gateway->id === "american_express") { | |
| 383 | + $payment_data['payment_method'] = $this->gateway->id; | |
| 384 | + } | |
| 385 | + | |
| 386 | + /** | |
| 387 | + * Filter the payment data before it's used | |
| 388 | + * | |
| 389 | + * @param array $payment_data | |
| 390 | + * @param int $order_id | |
| 391 | + * @param array $customer_details | |
| 392 | + * @param PayplugAddressData $address_data | |
| 393 | + */ | |
| 394 | + $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data); | |
| 395 | + | |
| 396 | + /** | |
| 397 | + * | |
| 398 | + */ | |
| 399 | + $payment = $this->gateway->api->payment_create($payment_data); | |
| 400 | + | |
| 401 | + // Save transaction id on the order | |
| 402 | + PayplugWoocommerceHelper::is_pre_30() ? update_post_meta($order_id, '_transaction_id', $payment->id) : $order->set_transaction_id($payment->id); | |
| 403 | + | |
| 404 | + if (is_callable([$order, 'save'])) { | |
| 405 | + $order->save(); | |
| 406 | + } | |
| 407 | + | |
| 408 | + $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment); | |
| 409 | + PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata); | |
| 410 | + | |
| 411 | + wp_send_json_success(array( | |
| 412 | + 'payment_id' => $payment->id, | |
| 413 | + 'merchant_session' => isset($payment->payment_method["merchant_session"]) ? $payment->payment_method["merchant_session"]: null, | |
| 414 | + 'redirect' => !empty($payment->hosted_payment->payment_url) ? $payment->hosted_payment->payment_url : $return_url, | |
| 415 | + 'cancel' => esc_url_raw($order->get_cancel_order_url_raw()) | |
| 416 | + )); | |
| 417 | + } | |
| 418 | + | |
| 419 | + | |
| 420 | + /** | |
| 421 | + * Returns an instantiated gateway. | |
| 422 | + * @return PayplugGateway | |
| 423 | + */ | |
| 424 | + protected function get_payplug_gateway($id) { | |
| 425 | + if ( ! isset( $this->gateway ) ) { | |
| 426 | + $gateways = WC()->payment_gateways()->payment_gateways(); | |
| 427 | + foreach($gateways as $gateway){ | |
| 428 | + if($gateway->id === $id){ | |
| 429 | + return $gateway; | |
| 430 | + } | |
| 431 | + } | |
| 432 | + } | |
| 433 | + } | |
| 434 | + | |
| 435 | + | |
| 436 | +} | |