| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Controller; |
| 4 |
|
| 5 |
use Payplug\Exception\HttpException; |
| 6 |
use Payplug\PayplugWoocommerce\Gateway\PayplugAddressData; |
| 7 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 8 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGateway; |
| 9 |
use Payplug\Resource\Payment as PaymentResource; |
| 10 |
|
| 11 |
class ApplePay extends PayplugGateway |
| 12 |
{ |
| 13 |
|
| 14 |
protected $domain_name = ""; |
| 15 |
|
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
|
| 19 |
parent::__construct(); |
| 20 |
|
| 21 |
/** @var \WC_Settings_API override $id */ |
| 22 |
$this->id = 'apple_pay'; |
| 23 |
|
| 24 |
/** @var \WC_Payment_Gateway overwrite for apple pay settings */ |
| 25 |
$this->method_title = __('payplug_apple_pay_title', 'payplug'); |
| 26 |
$this->method_description = ""; |
| 27 |
$this->has_fields = false; |
| 28 |
|
| 29 |
$this->title = __('payplug_apple_pay_title', 'payplug'); |
| 30 |
$this->description = '<div id="apple-pay-button-wrapper"><apple-pay-button buttonstyle="black" type="pay" locale="'. get_locale() .'"></apple-pay-button></div>'; |
| 31 |
$this->domain_name = $_SERVER['HTTP_HOST']; |
| 32 |
|
| 33 |
if(($this->checkApplePay() && is_admin())){ |
| 34 |
$this->enabled = true; |
| 35 |
|
| 36 |
}else if (!($this->checkApplePay() && $this->checkDeviceComptability() && $this->isSSL())) { |
| 37 |
$this->enabled = false; |
| 38 |
|
| 39 |
} else { |
| 40 |
add_action('wp_enqueue_scripts', [$this, 'add_apple_pay_css']); |
| 41 |
add_action('wp_enqueue_scripts', [$this, 'add_apple_pay_js']); |
| 42 |
} |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @return bool|void |
| 48 |
*/ |
| 49 |
public function process_admin_options() { |
| 50 |
$data = $this->get_post_data(); |
| 51 |
if (isset($data['woocommerce_payplug_mode'])) { |
| 52 |
if ( $this->get_post_data()['woocommerce_payplug_mode'] === '0' ) { |
| 53 |
$options = get_option( 'woocommerce_payplug_settings', [] ); |
| 54 |
$options['apple_pay'] = 'no'; |
| 55 |
update_option( 'woocommerce_payplug_settings', apply_filters( 'woocommerce_settings_api_sanitized_fields_payplug', $options ) ); |
| 56 |
} |
| 57 |
} |
| 58 |
if (isset($data['woocommerce_payplug_apple_pay'])) { |
| 59 |
if (($data['woocommerce_payplug_apple_pay'] == 1) && (!$this->checkApplePay())) { |
| 60 |
add_action( 'admin_notices', [$this ,"display_notice"] ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* |
| 68 |
* Check Apple Pay Authorization |
| 69 |
* |
| 70 |
* @return bool |
| 71 |
*/ |
| 72 |
|
| 73 |
private function checkApplePay(){ |
| 74 |
$account = PayplugWoocommerceHelper::get_account_data_from_options(); |
| 75 |
|
| 76 |
if (isset($account['payment_methods']['apple_pay']['enabled']) ) { |
| 77 |
|
| 78 |
if( !empty($account['apple_pay']) && $account['apple_pay'] === 'yes' ) { |
| 79 |
$applepay = false; |
| 80 |
if ($account['payment_methods']['apple_pay']['enabled']) { |
| 81 |
if (in_array($this->domain_name, $account['payment_methods']['apple_pay']['allowed_domain_names'])) { |
| 82 |
$applepay = true; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $applepay; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Display unauthorized error |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
|
| 99 |
public static function display_notice() { |
| 100 |
?> |
| 101 |
<div class="notice notice-error is-dismissible"> |
| 102 |
<p><?php echo __( 'payplug_apple_pay_unauthorized_error', 'payplug' ); ?></p> |
| 103 |
</div> |
| 104 |
<?php |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Check User-Agent to make sure it is on Mac OS and in Safari Browser |
| 109 |
* |
| 110 |
* @return bool |
| 111 |
*/ |
| 112 |
private function checkDeviceComptability(){ |
| 113 |
$user_agent = isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
| 114 |
|
| 115 |
// Check if the Browser is Safari |
| 116 |
if (stripos( $user_agent, 'Chrome') !== false) { |
| 117 |
return false; |
| 118 |
} elseif (stripos( $user_agent, 'Safari') !== false) { |
| 119 |
// Check if the OS is Mac |
| 120 |
if(!preg_match('/macintosh|mac os x/i', $user_agent)) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Check if SSL |
| 130 |
* |
| 131 |
*/ |
| 132 |
public function isSSL() |
| 133 |
{ |
| 134 |
if( !empty( $_SERVER['HTTPS'] ) ) { |
| 135 |
if ( 'on' == strtolower($_SERVER['HTTPS']) ) |
| 136 |
return true; |
| 137 |
} elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
if( !empty( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https' ) |
| 142 |
return true; |
| 143 |
|
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Add CSS |
| 149 |
* |
| 150 |
*/ |
| 151 |
public function add_apple_pay_css() { |
| 152 |
wp_enqueue_style('payplug-apple-pay', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-apple-pay.css', [], PAYPLUG_GATEWAY_VERSION); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Add JS |
| 157 |
* |
| 158 |
*/ |
| 159 |
public function add_apple_pay_js() { |
| 160 |
wp_enqueue_script( 'apple-pay-sdk', 'https://applepay.cdn-apple.com/jsapi/v1/apple-pay-sdk.js', array(), false, true ); |
| 161 |
wp_enqueue_script('payplug-apple-pay', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-apple-pay.js', |
| 162 |
[ |
| 163 |
'jquery', |
| 164 |
'apple-pay-sdk' |
| 165 |
], PAYPLUG_GATEWAY_VERSION, true); |
| 166 |
wp_localize_script( 'payplug-apple-pay', 'apple_pay_params', |
| 167 |
array( |
| 168 |
'ajax_url_payplug_create_order' => \WC_AJAX::get_endpoint('payplug_create_order'), |
| 169 |
'ajax_url_applepay_update_payment' => \WC_AJAX::get_endpoint('applepay_update_payment'), |
| 170 |
'ajax_url_applepay_get_order_totals' => \WC_AJAX::get_endpoint('applepay_get_order_totals'), |
| 171 |
'countryCode' => WC()->customer->get_billing_country(), |
| 172 |
'currencyCode' => get_woocommerce_currency(), |
| 173 |
'total' => WC()->cart->total, |
| 174 |
'apple_pay_domain' => $this->domain_name |
| 175 |
) |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Get payment icons. |
| 181 |
* |
| 182 |
* @return string |
| 183 |
*/ |
| 184 |
public function get_icon() |
| 185 |
{ |
| 186 |
$available_img = 'apple-pay-checkout.svg'; |
| 187 |
$icons = apply_filters('payplug_payment_icons', [ |
| 188 |
'payplug' => sprintf('<img src="%s" alt="Apple Pay" class="payplug-payment-icon" />', esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/' . $available_img)), |
| 189 |
]); |
| 190 |
$icons_str = ''; |
| 191 |
foreach ($icons as $icon) { |
| 192 |
$icons_str .= $icon; |
| 193 |
} |
| 194 |
return $icons_str; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* @param \WC_Order $order |
| 200 |
* @param int $amount |
| 201 |
* @param int $customer_id |
| 202 |
* |
| 203 |
* @return array |
| 204 |
* @throws \Exception |
| 205 |
*/ |
| 206 |
public function process_standard_payment($order, $amount, $customer_id) |
| 207 |
{ |
| 208 |
$order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id(); |
| 209 |
try { |
| 210 |
if (!$this->checkApplePay()) { |
| 211 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 212 |
} |
| 213 |
|
| 214 |
$address_data = PayplugAddressData::from_order($order); |
| 215 |
|
| 216 |
$return_url = esc_url_raw($order->get_checkout_order_received_url()); |
| 217 |
|
| 218 |
if (!(substr( $return_url, 0, 4 ) === "http")) { |
| 219 |
$return_url = get_site_url().$return_url; |
| 220 |
} |
| 221 |
|
| 222 |
// delivery_type must be removed in Apple Pay |
| 223 |
$billing = $address_data->get_billing(); |
| 224 |
unset($billing['delivery_type']); |
| 225 |
$shipping = $address_data->get_shipping(); |
| 226 |
unset($shipping['delivery_type']); |
| 227 |
|
| 228 |
$payment_data = [ |
| 229 |
'amount' => $amount, |
| 230 |
'currency' => get_woocommerce_currency(), |
| 231 |
'payment_method' => $this->id, |
| 232 |
'payment_context' => array( |
| 233 |
'apple_pay' => array( |
| 234 |
'domain_name' => $this->domain_name, |
| 235 |
'application_data' => base64_encode(json_encode(array( |
| 236 |
'apple_pay_domain' => $this->domain_name, |
| 237 |
))) |
| 238 |
) |
| 239 |
), |
| 240 |
'billing' => $billing, |
| 241 |
'shipping' => $shipping, |
| 242 |
'hosted_payment' => [ |
| 243 |
'return_url' => $return_url, |
| 244 |
'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()), |
| 245 |
], |
| 246 |
'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')), |
| 247 |
'metadata' => [ |
| 248 |
'order_id' => $order_id, |
| 249 |
'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest', |
| 250 |
'domain' => $this->domain_name, |
| 251 |
] |
| 252 |
]; |
| 253 |
|
| 254 |
/** |
| 255 |
* Filter the payment data before it's used |
| 256 |
* |
| 257 |
* @param array $payment_data |
| 258 |
* @param int $order_id |
| 259 |
* @param array $customer_details |
| 260 |
* @param PayplugAddressData $address_data |
| 261 |
*/ |
| 262 |
$payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data); |
| 263 |
$payment = $this->api->payment_create($payment_data); |
| 264 |
|
| 265 |
// Save transaction id for the order |
| 266 |
PayplugWoocommerceHelper::is_pre_30() |
| 267 |
? update_post_meta($order_id, '_transaction_id', $payment->id) |
| 268 |
: $order->set_transaction_id($payment->id); |
| 269 |
|
| 270 |
if (is_callable([$order, 'save'])) { |
| 271 |
$order->save(); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Fires once a payment has been created. |
| 276 |
* |
| 277 |
* @param int $order_id Order ID |
| 278 |
* @param PaymentResource $payment Payment resource |
| 279 |
*/ |
| 280 |
\do_action('payplug_gateway_payment_created', $order_id, $payment); |
| 281 |
|
| 282 |
$metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment); |
| 283 |
PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata); |
| 284 |
|
| 285 |
PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id)); |
| 286 |
|
| 287 |
return [ |
| 288 |
'result' => 'success', |
| 289 |
'merchant_session' => $payment->payment_method["merchant_session"], |
| 290 |
'payment_id' => $payment->id, |
| 291 |
'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()), |
| 292 |
'return_url' => $return_url, |
| 293 |
]; |
| 294 |
|
| 295 |
} catch (HttpException $e) { |
| 296 |
PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error'); |
| 297 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 298 |
} catch (\Exception $e) { |
| 299 |
PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error'); |
| 300 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 301 |
} |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
} |
| 306 |
|