PluginProbe
PayPlug for WooCommerce (Official) / 2.1.1
PayPlug for WooCommerce (Official) v2.1.1
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 / Controller / ApplePay.php

ApplePay.php in PayPlug for WooCommerce (Official) 2.1.1, at src/Controller/ApplePay.php

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