| @@ -2,541 +2,435 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Payplug\PayplugWoocommerce; |
| 4 | 4 | |
| 5 | 5 | // Exit if accessed directly |
| 6 | -use Automattic\WooCommerce\Utilities\OrderUtil; | |
| 6 | +use Payplug\PayplugWoocommerce\Controller\WC_Payplug_Intent_Controller as PayplugIntent; | |
| 7 | 7 | use Payplug\PayplugWoocommerce\Gateway\PayplugAddressData; |
| 8 | 8 | use Payplug\PayplugWoocommerce\Gateway\PayplugGateway; |
| 9 | -use Payplug\PayplugWoocommerce\Traits\ServiceGetter; | |
| 9 | +use WC_Payment_Tokens; | |
| 10 | +use WP_REST_Request; | |
| 11 | +use Automattic\WooCommerce\Utilities\OrderUtil; | |
| 10 | 12 | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit; | |
| 13 | 15 | } |
| 14 | 16 | |
| 15 | 17 | /** |
| 16 | 18 | * Class PayplugWoocommerceRequest |
| 19 | + * @package Payplug\PayplugWoocommerce | |
| 17 | 20 | */ |
| 18 | -class PayplugWoocommerceRequest | |
| 19 | -{ | |
| 20 | - use ServiceGetter; | |
| 21 | +class PayplugWoocommerceRequest { | |
| 21 | 22 | |
| 22 | - /** | |
| 23 | - * Gateway settings. | |
| 24 | - * | |
| 25 | - * @var array | |
| 26 | - */ | |
| 27 | - protected $settings; | |
| 23 | + /** | |
| 24 | + * Gateway settings. | |
| 25 | + * | |
| 26 | + * @var array | |
| 27 | + */ | |
| 28 | + protected $settings; | |
| 28 | 29 | |
| 29 | - /** | |
| 30 | - * @var PayplugGateway | |
| 31 | - */ | |
| 32 | - protected $gateway; | |
| 30 | + /** | |
| 31 | + * @var PayplugGateway | |
| 32 | + */ | |
| 33 | + protected $gateway; | |
| 33 | 34 | |
| 34 | - /** | |
| 35 | - * PayplugWoocommerceRequest constructor. | |
| 36 | - */ | |
| 37 | - public function __construct() | |
| 38 | - { | |
| 39 | - $this->settings = $this->get_configuration()->get_options(); | |
| 35 | + /** | |
| 36 | + * PayplugWoocommerceRequest constructor. | |
| 37 | + */ | |
| 38 | + public function __construct() { | |
| 39 | + $this->settings = get_option( 'woocommerce_payplug_settings', [] ); | |
| 40 | + if ( empty( $this->settings ) || ! isset( $this->settings['enabled'] ) || 'yes' !== $this->settings['enabled'] ) { | |
| 41 | + return; | |
| 42 | + } | |
| 40 | 43 | |
| 41 | - if (empty($this->settings) || !isset($this->settings['enabled']) || !(bool) $this->settings['enabled']) { | |
| 42 | - return; | |
| 43 | - } | |
| 44 | + // Don't load for change payment method page. | |
| 45 | + if ( isset( $_GET['change_payment_method'] ) ) { | |
| 46 | + return; | |
| 47 | + } | |
| 44 | 48 | |
| 45 | - // Don't load for change payment method page. | |
| 46 | - if (isset($_GET['change_payment_method'])) { | |
| 47 | - return; | |
| 48 | - } | |
| 49 | + add_action( 'template_redirect', [ $this, 'set_session' ] ); | |
| 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']); | |
| 49 | 56 | |
| 50 | - add_action('template_redirect', [$this, 'set_session']); | |
| 51 | - add_action('wc_ajax_payplug_create_order', [$this, 'ajax_create_order']); | |
| 52 | - add_action('wc_ajax_applepay_update_payment', [$this, 'applepay_update_payment']); | |
| 53 | - add_action('wc_ajax_applepay_get_order_totals', [$this, 'applepay_get_order_totals']); | |
| 54 | - add_action('wc_ajax_payplug_order_review_url', [$this, 'ajax_create_payment']); | |
| 55 | - add_action('wc_ajax_payplug_check_payment', [$this, 'check_payment']); | |
| 56 | - add_action('wc_ajax_payplug_create_intent', [$this, 'create_payment_intent']); | |
| 57 | - } | |
| 57 | + } | |
| 58 | 58 | |
| 59 | - /** | |
| 60 | - * Sets the WC customer session if one is not set. | |
| 61 | - * This is needed so nonces can be verified by AJAX Request. | |
| 62 | - */ | |
| 63 | - public function set_session() | |
| 64 | - { | |
| 65 | - if (!is_product() || (isset(WC()->session) && WC()->session->has_session())) { | |
| 66 | - return; | |
| 67 | - } | |
| 59 | + /** | |
| 60 | + * Sets the WC customer session if one is not set. | |
| 61 | + * This is needed so nonces can be verified by AJAX Request. | |
| 62 | + */ | |
| 63 | + public function set_session() { | |
| 64 | + if ( ! is_product() || ( isset( WC()->session ) && WC()->session->has_session() ) ) { | |
| 65 | + return; | |
| 66 | + } | |
| 68 | 67 | |
| 69 | - $session_class = apply_filters('woocommerce_session_handler', 'WC_Session_Handler'); | |
| 70 | - $wc_session = new $session_class(); | |
| 68 | + $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' ); | |
| 69 | + $wc_session = new $session_class(); | |
| 71 | 70 | |
| 72 | - if (version_compare(WC_VERSION, '3.3', '>=')) { | |
| 73 | - $wc_session->init(); | |
| 74 | - } | |
| 71 | + if ( version_compare( WC_VERSION, '3.3', '>=' ) ) { | |
| 72 | + $wc_session->init(); | |
| 73 | + } | |
| 75 | 74 | |
| 76 | - $wc_session->set_customer_session_cookie(true); | |
| 77 | - } | |
| 75 | + $wc_session->set_customer_session_cookie( true ); | |
| 76 | + } | |
| 78 | 77 | |
| 79 | - /** | |
| 80 | - * Create the woocommerce order in the BO | |
| 81 | - */ | |
| 82 | - public function ajax_create_order() | |
| 83 | - { | |
| 84 | - if (WC()->cart->is_empty()) { | |
| 85 | - wp_send_json_error(__('Empty cart', 'payplug')); | |
| 86 | - } | |
| 78 | + /** | |
| 79 | + * Create the woocommerce order in the BO | |
| 80 | + * | |
| 81 | + */ | |
| 82 | + public function ajax_create_order() { | |
| 83 | + if ( WC()->cart->is_empty() ) { | |
| 84 | + wp_send_json_error( __( 'Empty cart', 'payplug' ) ); | |
| 85 | + } | |
| 87 | 86 | |
| 88 | - if (!defined('WOOCOMMERCE_CHECKOUT')) { | |
| 89 | - define('WOOCOMMERCE_CHECKOUT', true); | |
| 90 | - } | |
| 87 | + if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { | |
| 88 | + define( 'WOOCOMMERCE_CHECKOUT', true ); | |
| 89 | + } | |
| 91 | 90 | |
| 92 | - WC()->checkout()->process_checkout(); | |
| 91 | + WC()->checkout()->process_checkout(); | |
| 93 | 92 | |
| 94 | - die(0); | |
| 95 | - } | |
| 93 | + die( 0 ); | |
| 94 | + } | |
| 96 | 95 | |
| 97 | - /** | |
| 98 | - * Create the woocommerce order in the BO | |
| 99 | - */ | |
| 100 | - public function ajax_create_payment() | |
| 101 | - { | |
| 102 | - global $wp; | |
| 96 | + /** | |
| 97 | + * Create the woocommerce order in the BO | |
| 98 | + * | |
| 99 | + */ | |
| 100 | + public function ajax_create_payment() { | |
| 103 | 101 | |
| 104 | - if (WC()->cart->is_empty()) { | |
| 105 | - wp_send_json_error(__('Empty cart', 'payplug')); | |
| 106 | - } | |
| 102 | + if ( WC()->cart->is_empty() ) { | |
| 103 | + wp_send_json_error( __( 'Empty cart', 'payplug' ) ); | |
| 104 | + } | |
| 107 | 105 | |
| 108 | - if (!defined('WOOCOMMERCE_CHECKOUT')) { | |
| 109 | - define('WOOCOMMERCE_CHECKOUT', true); | |
| 110 | - } | |
| 106 | + if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { | |
| 107 | + define( 'WOOCOMMERCE_CHECKOUT', true ); | |
| 108 | + } | |
| 111 | 109 | |
| 112 | - $payment_method = $_POST['payment_method']; | |
| 110 | + $payment_method = $_POST['payment_method']; | |
| 113 | 111 | |
| 114 | - if ($payment_method === 'payplug' || $payment_method === 'american_express') { | |
| 115 | - $method = $this->get_configuration()->get_option('payment_methods.configuration.payplug.embedded_mode'); | |
| 116 | - if ('integrated' != $method && 'popup' != $method) { | |
| 117 | - $this->ajax_create_order(); | |
| 118 | - } | |
| 119 | - } else { | |
| 120 | - $this->ajax_create_order(); | |
| 121 | - } | |
| 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']; | |
| 122 | 116 | |
| 123 | - $https_referer = $_POST['_wp_http_referer']; | |
| 124 | - $path = parse_url($https_referer); | |
| 125 | - wp_parse_str($path['query'], $output); | |
| 117 | + if($method !== "integrated" && $method !== "popup" ){ | |
| 118 | + $this->ajax_create_order(); | |
| 119 | + } | |
| 126 | 120 | |
| 127 | - if (isset($output['order-pay'])) { | |
| 128 | - $order_id = $output['order-pay']; | |
| 129 | - } else { | |
| 130 | - preg_match('/(?<=order-pay\/)\d*/', $path['path'], $matches); | |
| 131 | - $order_id = $matches[0]; | |
| 132 | - } | |
| 121 | + }else{ | |
| 122 | + $this->ajax_create_order(); | |
| 123 | + } | |
| 133 | 124 | |
| 134 | - $this->process_order_payment($order_id, $payment_method); | |
| 135 | - } | |
| 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']; | |
| 136 | 129 | |
| 137 | - /** | |
| 138 | - * wordpress class-wc-checkout.php | |
| 139 | - * 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 | |
| 140 | - * We can+t use WP method because it's protected | |
| 141 | - * Process an order that does require payment. | |
| 142 | - * | |
| 143 | - * @since 3.0.0 | |
| 144 | - * | |
| 145 | - * @param int $order_id Order ID. | |
| 146 | - * @param string $payment_method Payment method. | |
| 147 | - */ | |
| 148 | - protected function process_order_payment($order_id, $payment_method) | |
| 149 | - { | |
| 150 | - $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); | |
| 130 | + if(is_null($order_id)){ | |
| 131 | + preg_match("/(?<=order-pay\/)\d*/", $path['path'], $matches); | |
| 132 | + $order_id = $matches[0]; | |
| 133 | + } | |
| 151 | 134 | |
| 152 | - if (!isset($available_gateways[$payment_method])) { | |
| 153 | - return; | |
| 154 | - } | |
| 135 | + $this->process_order_payment($order_id, $payment_method); | |
| 155 | 136 | |
| 156 | - // Store Order ID in session so it can be re-used after payment failure. | |
| 157 | - WC()->session->set('order_awaiting_payment', $order_id); | |
| 137 | + } | |
| 158 | 138 | |
| 159 | - // Process Payment. | |
| 160 | - $result = $available_gateways[$payment_method]->process_payment($order_id); | |
| 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(); | |
| 161 | 151 | |
| 162 | - // Redirect to success/confirmation/payment page. | |
| 163 | - if (isset($result['result']) && 'success' === $result['result']) { | |
| 164 | - $result['order_id'] = $order_id; | |
| 152 | + if ( ! isset( $available_gateways[ $payment_method ] ) ) { | |
| 153 | + return; | |
| 154 | + } | |
| 165 | 155 | |
| 166 | - $result = apply_filters('woocommerce_payment_successful_result', $result, $order_id); | |
| 156 | + // Store Order ID in session so it can be re-used after payment failure. | |
| 157 | + WC()->session->set( 'order_awaiting_payment', $order_id ); | |
| 167 | 158 | |
| 168 | - if (!wp_doing_ajax()) { | |
| 169 | - // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect | |
| 170 | - wp_redirect($result['redirect']); | |
| 171 | - exit; | |
| 172 | - } | |
| 159 | + // Process Payment. | |
| 160 | + $result = $available_gateways[ $payment_method ]->process_payment( $order_id ); | |
| 173 | 161 | |
| 174 | - wp_send_json($result); | |
| 175 | - } | |
| 176 | - } | |
| 162 | + // Redirect to success/confirmation/payment page. | |
| 163 | + if ( isset( $result['result'] ) && 'success' === $result['result'] ) { | |
| 164 | + $result['order_id'] = $order_id; | |
| 177 | 165 | |
| 178 | - /** | |
| 179 | - * Update Payplug API Payment for Apple Pay | |
| 180 | - */ | |
| 181 | - public function applepay_update_payment() | |
| 182 | - { | |
| 183 | - $payment_id = $_POST['payment_id']; | |
| 184 | - $apiService = new \Payplug\PayplugWoocommerce\Service\Api(); | |
| 185 | - $mode = PayplugWoocommerceHelper::check_mode() ? 'live' : 'test'; | |
| 186 | - $bearer_token = $apiService->get_bearer_token($mode); | |
| 187 | - try { | |
| 188 | - \Payplug\Payplug::init([ | |
| 189 | - 'secretKey' => $bearer_token, | |
| 190 | - 'apiVersion' => '2019-08-06', | |
| 191 | - ]); | |
| 192 | - } catch (\Exception $e) { | |
| 193 | - $is_401 = (method_exists($e, 'getCode') && $e->getCode() == 401) | |
| 194 | - || strpos($e->getMessage(), '401') !== false; | |
| 195 | - if ($is_401 && $this->try_refresh_jwt($apiService, $mode)) { | |
| 196 | - try { | |
| 197 | - \Payplug\Payplug::init([ | |
| 198 | - 'secretKey' => $apiService->get_bearer_token($mode), | |
| 199 | - 'apiVersion' => '2019-08-06', | |
| 200 | - ]); | |
| 201 | - } catch (\Exception $e) { | |
| 202 | - PayplugWoocommerceHelper::payplug_logout(); | |
| 203 | - wp_send_json_error(__('You have been disconnected from the Payplug module (error 401)', 'payplug')); | |
| 166 | + $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id ); | |
| 204 | 167 | |
| 205 | - return; | |
| 206 | - } | |
| 207 | - } else { | |
| 208 | - if ($is_401) { | |
| 209 | - PayplugWoocommerceHelper::payplug_logout(); | |
| 210 | - wp_send_json_error(__('You have been disconnected from the Payplug module (error 401)', 'payplug')); | |
| 211 | - } else { | |
| 212 | - wp_send_json_error($e->getMessage()); | |
| 213 | - } | |
| 168 | + if ( ! wp_doing_ajax() ) { | |
| 169 | + // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect | |
| 170 | + wp_redirect( $result['redirect'] ); | |
| 171 | + exit; | |
| 172 | + } | |
| 214 | 173 | |
| 215 | - return; | |
| 216 | - } | |
| 217 | - } | |
| 218 | - $apple_pay = []; | |
| 219 | - $apple_pay['payment_token'] = $_POST['payment_token']; | |
| 220 | - $payment = \Payplug\Payment::retrieve($payment_id); | |
| 221 | - $data = ['apple_pay' => $apple_pay]; | |
| 222 | - $update = $payment->update($data); | |
| 223 | - wp_send_json_success(['result' => $update->is_paid]); | |
| 224 | - } | |
| 174 | + wp_send_json( $result ); | |
| 175 | + } | |
| 176 | + } | |
| 225 | 177 | |
| 226 | - public function applepay_get_order_totals() | |
| 227 | - { | |
| 228 | - try { | |
| 229 | - wp_send_json_success(WC()->cart->total); | |
| 230 | - } catch (\Exception $e) { | |
| 231 | - PayplugGateway::log($e->getMessage()); | |
| 232 | - wp_send_json_error($e->getMessage()); | |
| 233 | - } | |
| 234 | - } | |
| 235 | 178 | |
| 236 | - /** | |
| 237 | - * Empty cart for Apple Pay on product page | |
| 238 | - */ | |
| 239 | - public function applepay_empty_cart() | |
| 240 | - { | |
| 241 | - try { | |
| 242 | - WC()->cart->empty_cart(); | |
| 243 | - wp_send_json_success(); | |
| 244 | - } catch (\Exception $e) { | |
| 245 | - wp_send_json_error([ | |
| 246 | - 'message' => __('Your order was cancelled.', 'woocommerce'), | |
| 247 | - ]); | |
| 248 | - } | |
| 249 | - } | |
| 250 | 179 | |
| 251 | - /** | |
| 252 | - * Add the product on the current page to the cart for Apple Pay on product page | |
| 253 | - */ | |
| 254 | - public function applepay_add_to_cart() | |
| 255 | - { | |
| 256 | - try { | |
| 257 | - if (!empty($_POST['product_id'])) { | |
| 258 | - $product_id = $_POST['product_id']; | |
| 259 | - } else { | |
| 260 | - $product_id = $_POST['product_variation_id']; | |
| 261 | - } | |
| 180 | + /** | |
| 181 | + * Update Payplug API Payment for Apple Pay | |
| 182 | + */ | |
| 183 | + public function applepay_update_payment() { | |
| 262 | 184 | |
| 263 | - $product_quantity = !empty($_POST['product_quantity']) ? $_POST['product_quantity'] : 1; | |
| 185 | + $options = get_option('woocommerce_payplug_settings', []); | |
| 186 | + $order_id = $_POST['order_id']; | |
| 187 | + $payment_id = $_POST['payment_id']; | |
| 264 | 188 | |
| 265 | - WC()->cart->add_to_cart($product_id, $product_quantity); | |
| 266 | - wp_send_json_success([ | |
| 267 | - 'total' => WC()->cart->total - WC()->cart->shipping_total, | |
| 268 | - ]); | |
| 269 | - } catch (\Exception $e) { | |
| 270 | - wp_send_json_error([ | |
| 271 | - 'message' => __('Your order was cancelled.', 'woocommerce'), | |
| 272 | - ]); | |
| 273 | - } | |
| 274 | - } | |
| 189 | + try{ | |
| 190 | + \Payplug\Payplug::init(array( | |
| 191 | + 'secretKey' => @$options['payplug_live_key'], | |
| 192 | + 'apiVersion' => "2019-08-06", | |
| 193 | + )); | |
| 275 | 194 | |
| 276 | - /** | |
| 277 | - * Limit string length. | |
| 278 | - * | |
| 279 | - * @param string $value | |
| 280 | - * @param int $maxlength | |
| 281 | - * | |
| 282 | - * @return string | |
| 283 | - */ | |
| 284 | - public function limit_length($value, $maxlength = 100) | |
| 285 | - { | |
| 286 | - return (strlen($value) > $maxlength) ? substr($value, 0, $maxlength) : $value; | |
| 287 | - } | |
| 195 | + $apple_pay = array(); | |
| 196 | + $apple_pay['payment_token'] = $_POST['payment_token']; | |
| 197 | + $payment = \Payplug\Payment::retrieve($payment_id); | |
| 288 | 198 | |
| 289 | - public function check_payment() | |
| 290 | - { | |
| 291 | - global $wpdb; | |
| 292 | - $payment_id = $_POST['payment_id']; | |
| 293 | - if (empty($payment_id)) { | |
| 294 | - wp_send_json_error(__('Invalid request.', 'payplug')); | |
| 295 | - } | |
| 296 | - $apiService = new \Payplug\PayplugWoocommerce\Service\Api(); | |
| 297 | - $mode = PayplugWoocommerceHelper::check_mode() ? 'live' : 'test'; | |
| 298 | - $bearer_token = $apiService->get_bearer_token($mode); | |
| 299 | - try { | |
| 300 | - \Payplug\Payplug::init([ | |
| 301 | - 'secretKey' => $bearer_token, | |
| 302 | - 'apiVersion' => '2019-08-06', | |
| 303 | - ]); | |
| 304 | - $payment = \Payplug\Payment::retrieve($payment_id); | |
| 305 | - } catch (\Exception $e) { | |
| 306 | - $is_401 = (method_exists($e, 'getCode') && $e->getCode() == 401) | |
| 307 | - || strpos($e->getMessage(), '401') !== false; | |
| 308 | - if ($is_401) { | |
| 309 | - if ($this->try_refresh_jwt($apiService, $mode)) { | |
| 310 | - try { | |
| 311 | - \Payplug\Payplug::init([ | |
| 312 | - 'secretKey' => $apiService->get_bearer_token($mode), | |
| 313 | - 'apiVersion' => '2019-08-06', | |
| 314 | - ]); | |
| 315 | - $payment = \Payplug\Payment::retrieve($payment_id); | |
| 316 | - } catch (\Exception $e) { | |
| 317 | - PayplugWoocommerceHelper::payplug_logout(); | |
| 318 | - wp_send_json_error(__('You have been disconnected from the Payplug module (error 401)', 'payplug')); | |
| 199 | + $data = array( 'apple_pay' => $apple_pay ); | |
| 200 | + $update = $payment->update($data); | |
| 319 | 201 | |
| 320 | - return; | |
| 321 | - } | |
| 322 | - } else { | |
| 323 | - PayplugWoocommerceHelper::payplug_logout(); | |
| 324 | - wp_send_json_error(__('You have been disconnected from the Payplug module (error 401)', 'payplug')); | |
| 202 | + wp_send_json_success([ "result" => $update->is_paid ]); | |
| 325 | 203 | |
| 326 | - return; | |
| 327 | - } | |
| 328 | - } else { | |
| 329 | - $order_id = $this->getOrderFromPaymentId($payment_id); | |
| 330 | - $order = wc_get_order($order_id); | |
| 331 | - PayplugGateway::log( | |
| 332 | - sprintf( | |
| 333 | - 'Order #%s : An error occurred while retrieving the payment data with the message : %s', | |
| 334 | - $order_id, | |
| 335 | - $e->getMessage() | |
| 336 | - ) | |
| 337 | - ); | |
| 338 | - wp_send_json_error($e->getMessage()); | |
| 339 | - } | |
| 340 | - } | |
| 341 | - $order_id = $this->getOrderFromPaymentId($payment_id); | |
| 342 | - $order = wc_get_order($order_id); | |
| 343 | - $return_url = esc_url_raw($order->get_checkout_order_received_url()); | |
| 344 | - if ((isset($payment->failure)) && (!empty($payment->failure)) || ($payment->is_paid === false && is_null($payment->paid_at))) { | |
| 345 | - $order->update_status('failed', __('Order cancelled by customer.', 'woocommerce')); | |
| 346 | - wp_send_json_error( | |
| 347 | - [ | |
| 348 | - 'code' => isset($payment->failure->code) ? $payment->failure->code : 500, | |
| 349 | - 'message' => !empty($payment->failure->message) ? $payment->failure->message : __('payplug_integrated_payment_error', 'payplug'), | |
| 350 | - 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()), | |
| 351 | - ] | |
| 352 | - ); | |
| 353 | - } | |
| 204 | + }catch (\Exception $e){ | |
| 205 | + wp_send_json_error($e->getMessage()); | |
| 206 | + } | |
| 207 | + } | |
| 354 | 208 | |
| 355 | - wp_send_json_success([ | |
| 356 | - 'payment_id' => $payment->id, | |
| 357 | - 'result' => 'success', | |
| 358 | - 'redirect' => !empty($payment->hosted_payment->payment_url) ? $payment->hosted_payment->payment_url : $return_url, | |
| 359 | - 'cancel' => !empty($payment->hosted_payment->cancel_url) ? $payment->hosted_payment->cancel_url : null, | |
| 360 | - ]); | |
| 361 | - } | |
| 209 | + public function applepay_get_order_totals() | |
| 210 | + { | |
| 211 | + try { | |
| 212 | + wp_send_json_success(WC()->cart->total); | |
| 362 | 213 | |
| 363 | - /** | |
| 364 | - * Attempt to regenerate the JWT for the given mode and persist the new token. | |
| 365 | - * | |
| 366 | - * @param \Payplug\PayplugWoocommerce\Service\Api $apiService | |
| 367 | - * @param string $mode | |
| 368 | - * | |
| 369 | - * @return bool True if a new JWT was successfully obtained and stored. | |
| 370 | - */ | |
| 371 | - private function try_refresh_jwt($apiService, $mode) | |
| 372 | - { | |
| 373 | - $configuration = $this->get_configuration(); | |
| 374 | - $options = $configuration->get_options(); | |
| 375 | - $oauth_client_data = isset($options['oauth_client_data']) ? json_decode($options['oauth_client_data'], true) : []; | |
| 376 | - $client_data = isset($oauth_client_data[$mode]) ? $oauth_client_data[$mode] : []; | |
| 377 | - if (empty($client_data)) { | |
| 378 | - return false; | |
| 379 | - } | |
| 380 | - $new_jwt = $apiService->generate_jwt( | |
| 381 | - isset($client_data['client_id']) ? $client_data['client_id'] : '', | |
| 382 | - isset($client_data['client_secret']) ? $client_data['client_secret'] : '' | |
| 383 | - ); | |
| 384 | - if (empty($new_jwt) || !isset($new_jwt['access_token'])) { | |
| 385 | - return false; | |
| 386 | - } | |
| 387 | - $jwt = isset($options['jwt']) ? json_decode($options['jwt'], true) : []; | |
| 388 | - $jwt[$mode] = $new_jwt; | |
| 389 | - $api_keys = isset($options['api_key']) ? json_decode($options['api_key'], true) : []; | |
| 390 | - $api_keys[$mode] = $new_jwt['access_token']; | |
| 391 | - $configuration->update_option('jwt', json_encode($jwt)); | |
| 392 | - $configuration->update_option('api_key', json_encode($api_keys)); | |
| 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 | + } | |
| 393 | 231 | |
| 394 | - return true; | |
| 395 | - } | |
| 232 | + public function check_payment() { | |
| 396 | 233 | |
| 397 | - /** | |
| 398 | - * @param $payment_id | |
| 399 | - * | |
| 400 | - * @return int|string|null | |
| 401 | - */ | |
| 402 | - private function getOrderFromPaymentId($payment_id) | |
| 403 | - { | |
| 404 | - global $wpdb; | |
| 234 | + global $wpdb; | |
| 405 | 235 | |
| 406 | - if (OrderUtil::custom_orders_table_usage_is_enabled()) { | |
| 407 | - $orders = wc_get_orders( | |
| 408 | - [ | |
| 409 | - 'field_query' => [ | |
| 410 | - [ | |
| 411 | - 'key' => 'transaction_id', | |
| 412 | - 'comparison' => $payment_id, | |
| 413 | - ], | |
| 414 | - ], | |
| 415 | - ] | |
| 416 | - ); | |
| 417 | - $order = $orders[0]; | |
| 418 | - $order_id = $order->get_id(); | |
| 419 | - } else { | |
| 420 | - $sql = 'SELECT post_id | |
| 421 | - FROM $wpdb->postmeta | |
| 422 | - WHERE meta_key = "_transaction_id" AND meta_value = %s'; | |
| 423 | - $order_id = $wpdb->get_var( | |
| 424 | - $wpdb->prepare( | |
| 425 | - $sql, | |
| 426 | - $payment_id | |
| 427 | - ) | |
| 428 | - ); | |
| 429 | - } | |
| 236 | + $payment_id = $_POST['payment_id']; | |
| 430 | 237 | |
| 431 | - return $order_id; | |
| 432 | - } | |
| 238 | + if (PayplugWoocommerceHelper::check_mode()) { | |
| 239 | + $key = PayplugWoocommerceHelper::get_live_key(); | |
| 240 | + } else { | |
| 241 | + $key = PayplugWoocommerceHelper::get_test_key(); | |
| 242 | + } | |
| 433 | 243 | |
| 434 | - public function create_payment_intent() | |
| 435 | - { | |
| 436 | - $order_id = $_POST['order_id']; | |
| 437 | - $this->gateway = $this->get_payplug_gateway($_POST['gateway']); | |
| 438 | - $order = wc_get_order($order_id); | |
| 439 | - $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id(); | |
| 440 | - $return_url = esc_url_raw($order->get_checkout_order_received_url()); | |
| 441 | - $address_data = PayplugAddressData::from_order($order); | |
| 442 | - $amount = (int) PayplugWoocommerceHelper::get_payplug_amount($order->get_total()); | |
| 443 | - $amount = $this->gateway->validate_order_amount($amount); | |
| 244 | + try { | |
| 245 | + \Payplug\Payplug::init(array( | |
| 246 | + 'secretKey' => $key, | |
| 247 | + 'apiVersion' => "2019-08-06", | |
| 248 | + )); | |
| 444 | 249 | |
| 445 | - $payment_data = [ | |
| 446 | - 'amount' => $amount, | |
| 447 | - 'currency' => get_woocommerce_currency(), | |
| 448 | - 'allow_save_card' => $this->gateway->save_card_available() && (int) $customer_id > 0, | |
| 449 | - 'billing' => $address_data->get_billing(), | |
| 450 | - 'shipping' => $address_data->get_shipping(), | |
| 451 | - 'hosted_payment' => [ | |
| 452 | - 'return_url' => $return_url, | |
| 453 | - ], | |
| 454 | - 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')), | |
| 455 | - 'metadata' => [ | |
| 456 | - 'order_id' => $order_id, | |
| 457 | - 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest', | |
| 458 | - 'domain' => $this->limit_length(esc_url_raw(home_url()), 500), | |
| 459 | - ], | |
| 460 | - ]; | |
| 250 | + $payment =\Payplug\Payment::retrieve($payment_id); | |
| 461 | 251 | |
| 462 | - if (PayplugWoocommerceHelper::is_checkout_block() && is_checkout()) { | |
| 463 | - $payment_data['metadata']['woocommerce_block'] = 'CHECKOUT'; | |
| 464 | - } elseif (PayplugWoocommerceHelper::is_cart_block() && is_cart()) { | |
| 465 | - $payment_data['metadata']['woocommerce_block'] = 'CART'; | |
| 466 | - } | |
| 252 | + } catch ( \Exception $e ) { | |
| 467 | 253 | |
| 468 | - if ($this->gateway->id === 'apple_pay') { | |
| 469 | - unset($payment_data['allow_save_card']); | |
| 254 | + $order_id = $this->getOrderFromPaymentId($payment_id); | |
| 255 | + $order = wc_get_order($order_id); | |
| 470 | 256 | |
| 471 | - $payment_data['payment_method'] = $this->gateway->id; | |
| 472 | - $payment_data['payment_context'] = [ | |
| 473 | - 'apple_pay' => [ | |
| 474 | - 'domain_name' => $this->gateway->domain_name, | |
| 475 | - 'application_data' => base64_encode(json_encode([ | |
| 476 | - 'apple_pay_domain' => $this->gateway->domain_name, | |
| 477 | - ])), | |
| 478 | - ], | |
| 479 | - ]; | |
| 480 | - $payment_data['hosted_payment']['cancel_url'] = esc_url_raw($order->get_cancel_order_url_raw()); | |
| 481 | - $payment_data['metadata']['applepay_workflow'] = 'checkout'; | |
| 482 | - } | |
| 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 | + ); | |
| 483 | 264 | |
| 484 | - $method = $this->get_configuration()->get_option('payment_methods.configuration.payplug.embedded_mode'); | |
| 265 | + return wp_send_json_error( $e->getMessage()); | |
| 266 | + } | |
| 485 | 267 | |
| 486 | - if ('integrated' == $method && $this->gateway->id === 'payplug') { | |
| 487 | - $payment_data['initiator'] = 'PAYER'; | |
| 488 | - $payment_data['integration'] = 'INTEGRATED_PAYMENT'; | |
| 489 | - unset($payment_data['hosted_payment']['cancel_url']); | |
| 490 | - } | |
| 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()); | |
| 491 | 271 | |
| 492 | - if ('popup' == $method && $this->gateway->id === 'american_express') { | |
| 493 | - $payment_data['payment_method'] = $this->gateway->id; | |
| 494 | - } | |
| 272 | + if ((isset($payment->failure)) && (!empty($payment->failure)) || ($payment->is_paid === false && is_null($payment->paid_at))) { | |
| 495 | 273 | |
| 496 | - /** | |
| 497 | - * Filter the payment data before it's used | |
| 498 | - * | |
| 499 | - * @param array $payment_data | |
| 500 | - * @param int $order_id | |
| 501 | - * @param array $customer_details | |
| 502 | - * @param PayplugAddressData $address_data | |
| 503 | - */ | |
| 504 | - $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data); | |
| 274 | + $order->update_status( 'failed', __( 'Order cancelled by customer.', 'woocommerce' ) ); | |
| 505 | 275 | |
| 506 | - $payment = $this->gateway->payplug_api->payment_create($payment_data); | |
| 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 | + } | |
| 507 | 284 | |
| 508 | - // Save transaction id on the order | |
| 509 | - PayplugWoocommerceHelper::is_pre_30() ? update_post_meta($order_id, '_transaction_id', $payment->id) : $order->set_transaction_id($payment->id); | |
| 510 | 285 | |
| 511 | - if (is_callable([$order, 'save'])) { | |
| 512 | - $order->save(); | |
| 513 | - } | |
| 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 | + )); | |
| 514 | 292 | |
| 515 | - $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment); | |
| 516 | - PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata); | |
| 293 | + } | |
| 517 | 294 | |
| 518 | - wp_send_json_success([ | |
| 519 | - 'payment_id' => $payment->id, | |
| 520 | - 'merchant_session' => isset($payment->payment_method['merchant_session']) ? $payment->payment_method['merchant_session'] : null, | |
| 521 | - 'redirect' => !empty($payment->hosted_payment->payment_url) ? $payment->hosted_payment->payment_url : $return_url, | |
| 522 | - 'cancel' => esc_url_raw($order->get_cancel_order_url_raw()), | |
| 523 | - ]); | |
| 524 | - } | |
| 295 | + /** | |
| 296 | + * @param $payment_id | |
| 297 | + * @return int|string|null | |
| 298 | + */ | |
| 299 | + private function getOrderFromPaymentId( $payment_id){ | |
| 300 | + global $wpdb; | |
| 525 | 301 | |
| 526 | - /** | |
| 527 | - * Returns an instantiated gateway. | |
| 528 | - * | |
| 529 | - * @return PayplugGateway | |
| 530 | - */ | |
| 531 | - protected function get_payplug_gateway($id) | |
| 532 | - { | |
| 533 | - if (!isset($this->gateway)) { | |
| 534 | - $gateways = WC()->payment_gateways()->payment_gateways(); | |
| 535 | - foreach ($gateways as $gateway) { | |
| 536 | - if ($gateway->id === $id) { | |
| 537 | - return $gateway; | |
| 538 | - } | |
| 539 | - } | |
| 540 | - } | |
| 541 | - } | |
| 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 | + | |
| 542 | 436 | } |