PluginProbe
PayPlug for WooCommerce (Official) / 2.6.3
PayPlug for WooCommerce (Official) v2.6.3
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 / AmericanExpress.php

AmericanExpress.php in PayPlug for WooCommerce (Official) 2.6.3, at src/Controller/AmericanExpress.php

202 lines 6.0 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 AmericanExpress extends PayplugGateway
12 {
13
14 public function __construct() {
15
16 parent::__construct();
17
18 /** @var \WC_Settings_API override $id */
19 $this->id = 'american_express';
20
21 /** @var \WC_Payment_Gateway overwrite for apple pay settings */
22 $this->method_title = __('payplug_amex_title', 'payplug');
23 $this->method_description = "";
24
25 $this->title = __('payplug_amex_title', 'payplug');
26 $this->description = '';
27 $this->has_fields = false;
28
29 if(!$this->checkAmericanExpress())
30 $this->enabled = 'no';
31
32 }
33
34 /**
35 *
36 * Check American Express Authorization
37 *
38 * @return bool
39 */
40 private function checkAmericanExpress(){
41 $account = PayplugWoocommerceHelper::get_account_data_from_options();
42
43 if (isset($account['payment_methods']['american_express']['enabled']) ) {
44
45 if( !empty($account['american_express']) && $account['american_express'] === 'yes' )
46 return $account['payment_methods']['american_express']['enabled'];
47
48 }
49
50 return false;
51 }
52
53 /**
54 * @return bool|void
55 */
56 public function process_admin_options() {
57 $data = $this->get_post_data();
58 if (isset($data['woocommerce_payplug_mode'])) {
59 if ($data['woocommerce_payplug_mode'] === '0') {
60 $options = get_option('woocommerce_payplug_settings', []);
61 $options['american_express'] = 'no';
62 update_option( 'woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options) );
63 }
64 }
65
66 if (isset($data['woocommerce_payplug_american_express'])) {
67 if (($data['woocommerce_payplug_american_express'] == 1) && (!$this->checkAmericanExpress())) {
68 $options = get_option('woocommerce_payplug_settings', []);
69 $options['american_express'] = 'no';
70 update_option( 'woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options) );
71 add_action( 'admin_notices', [$this ,"display_notice"] );
72 }
73 }
74
75 }
76
77 /**
78 * Display unauthorized error
79 *
80 * @return void
81 */
82 public static function display_notice() {
83 ?>
84 <div class="notice notice-error is-dismissible">
85 <p><?php echo __( 'payplug_amex_unauthorized_message', 'payplug' ); ?></p>
86 </div>
87 <?php
88 }
89
90 /**
91 *
92 * Get Amex payment icon
93 *
94 * @return string
95 */
96
97 public function get_icon() {
98 $available_img = 'Amex_logo_color.svg';
99 $icons = apply_filters('payplug_payment_icons', [
100 'payplug' => sprintf('<img src="%s" alt="Amex Icon" class="payplug-payment-icon" />', esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/' . $available_img)),
101 ]);
102 $icons_str = '';
103 foreach ($icons as $icon) {
104 $icons_str .= $icon;
105 }
106 return $icons_str;
107 }
108 /**
109 * @param \WC_Order $order
110 * @param int $amount
111 * @param int $customer_id
112 *
113 * @return array
114 * @throws \Exception
115 */
116 public function process_standard_payment($order, $amount, $customer_id)
117 {
118 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
119 try {
120 if (!$this->checkAmericanExpress()) {
121 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
122 }
123
124 $address_data = PayplugAddressData::from_order($order);
125
126 $return_url = esc_url_raw($order->get_checkout_order_received_url());
127
128 if (!(substr( $return_url, 0, 4 ) === "http")) {
129 $return_url = get_site_url().$return_url;
130 }
131
132 $payment_data = [
133 'amount' => $amount,
134 'currency' => get_woocommerce_currency(),
135 'payment_method' => $this->id,
136 'billing' => $address_data->get_billing(),
137 'shipping' => $address_data->get_shipping(),
138 'hosted_payment' => [
139 'return_url' => $return_url,
140 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
141 ],
142 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
143 'metadata' => [
144 'order_id' => $order_id,
145 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
146 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
147 ],
148 "save_card"=> false,
149 "force_3ds"=> false
150 ];
151
152 /**
153 * Filter the payment data before it's used
154 *
155 * @param array $payment_data
156 * @param int $order_id
157 * @param array $customer_details
158 * @param PayplugAddressData $address_data
159 */
160 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
161 $payment = $this->api->payment_create($payment_data);
162
163 // Save transaction id for the order
164 PayplugWoocommerceHelper::is_pre_30()
165 ? update_post_meta($order_id, '_transaction_id', $payment->id)
166 : $order->set_transaction_id($payment->id);
167
168 if (is_callable([$order, 'save'])) {
169 $order->save();
170 }
171
172 /**
173 * Fires once a payment has been created.
174 *
175 * @param int $order_id Order ID
176 * @param PaymentResource $payment Payment resource
177 */
178 \do_action('payplug_gateway_payment_created', $order_id, $payment);
179
180 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
181 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
182
183 PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id));
184
185 return [
186 'result' => 'success',
187 'redirect' => $payment->hosted_payment->payment_url,
188 'cancel' => $payment->hosted_payment->cancel_url,
189 ];
190
191 } catch (HttpException $e) {
192 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
193 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
194 } catch (\Exception $e) {
195 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
196 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
197 }
198
199 }
200
201 }
202