PluginProbe
PayPlug for WooCommerce (Official) / 2.6.0
PayPlug for WooCommerce (Official) v2.6.0
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 / PayplugGenericGateway.php

PayplugGenericGateway.php in PayPlug for WooCommerce (Official) 2.6.0, at src/Controller/PayplugGenericGateway.php

354 lines 10.8 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\PayplugWoocommerce\Gateway\PayplugAddressData;
6 use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
7 use Payplug\PayplugWoocommerce\Interfaces\PayplugGatewayBuilder;
8 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
9
10
11 use Payplug\Authentication;
12 use Payplug\Exception\ConfigurationException;
13 use Payplug\Exception\HttpException;
14 use Payplug\Exception\ForbiddenException;
15 use Payplug\Payplug;
16
17 class PayplugGenericGateway extends PayplugGateway implements PayplugGatewayBuilder
18 {
19
20 public function __construct()
21 {
22 parent::__construct();
23
24 //this is only for PPRo payments
25 add_action('woocommerce_after_order_itemmeta', [$this, 'hide_wc_refund_button']);
26 //TODO:: add requirements here
27
28 }
29
30 /**
31 * Generic code to fetch payment gateway specific image
32 * @return string
33 */
34 public function get_icon()
35 {
36
37 //get object $image
38 $icons = apply_filters('payplug_payment_icons', [
39 'payplug' => sprintf('<img src="%s" alt="%s" class="payplug-payment-icon" />', esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/' . $this->image), $this->id . " Icon"),
40 ]);
41
42 $icons_str = '';
43 foreach ($icons as $icon) {
44 $icons_str .= $icon;
45 }
46
47 return $icons_str;
48 }
49
50 /**
51 * @return void
52 */
53 public function display_notice()
54 {
55 $error_message = 'payplug_' . $this->id . '_unauthorized_message';
56 ?>
57 <div class="notice notice-error is-dismissible">
58 <p><?php echo __( $error_message, 'payplug' ); ?></p>
59 </div>
60 <?php
61 }
62
63 public function checkGateway()
64 {
65
66 //TODO:: MISSING saved configurations
67 $account = PayplugWoocommerceHelper::generic_get_account_data_from_options( $this->id );
68
69 //account doesnt have permissions
70 if ( ( isset( $account["payment_methods"] ) ) && ( empty( $account["payment_methods"][ $this->id ] ) ) && ( ! $account["payment_methods"][ $this->id ]['enabled'] ) ) {
71 return false;
72 }
73
74 //check if it's activated on the BO
75 if ( ! isset( $account['permissions'][ $this->id ] ) || ( isset( $account['permissions'][ $this->id ] ) && ! $account['permissions'][ $this->id ] ) ) {
76 return false;
77 }
78
79 if (is_checkout()) {
80 if ( empty( WC()->cart ) ) {
81 return false;
82 }
83
84 //for backend orders
85 if ( ! empty( get_query_var( 'order-pay' ) ) ) {
86 $order = wc_get_order( get_query_var( 'order-pay' ) );
87 $items = $order->get_items();
88
89 $country_code_shipping = $order->get_shipping_country();
90 $country_code_billing = $order->get_billing_country();
91
92 $this->order_items_to_cart( WC()->cart, $items );
93 }
94
95 $order_amount = $this->get_order_total();
96
97 $this->allowed_country_codes = $account["payment_methods"][ $this->id ]['allowed_countries'];
98 $this->get_thresholds_values( $account );
99
100
101 //threshold validations
102 if ( ( ! empty( $this->min_thresholds ) && $order_amount < $this->min_thresholds ) || ( ! empty( $this->max_thresholds ) && $order_amount > $this->max_thresholds ) ) {
103 $this->description = '<div class="payment_method_oney_x3_with_fees_disabled">' . __( $this->id . '_threshold.', 'payplug' ) . '</div>';
104
105 return false;
106 }
107
108 if ( empty( $country_code_shipping ) || empty( $country_code_shipping ) ) {
109 $country_code_shipping = WC()->customer->get_shipping_country();
110 $country_code_billing = WC()->customer->get_billing_country();
111 }
112
113 if ( $this->allowed_country_codes === "ALL" || empty( $this->allowed_country_codes ) ) {
114 return true;
115 }
116
117 //check if country is allowed
118 if ( in_array( $country_code_billing, $this->allowed_country_codes ) ) {
119
120 //billing and shipping should be the same country
121 if ( ! $this->validate_shipping_billing_country( $country_code_shipping, $country_code_billing ) ) {
122 $this->description = '<div class="payment_method_oney_x3_with_fees_disabled">' . __( 'Unavailable for the specified country.', 'payplug' ) . '</div>';
123
124 return false;
125 }
126
127 return true;
128
129 } else {
130 return false;
131 }
132 } else {
133 //check if it's activated on the BO
134 if ( ! isset( $account['permissions'][ $this->id ] ) || ( isset( $account['permissions'][ $this->id ] ) && ! $account['permissions'][ $this->id ] ) ) {
135 return false;
136 } else {
137 return true;
138 }
139 }
140 }
141
142 public function process_standard_payment($order, $amount, $customer_id)
143 {
144 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
145 try {
146
147 //if there's no auth to process payment
148 if (!$this->checkGateway()) {
149 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
150 }
151
152 $address_data = PayplugAddressData::from_order($order);
153
154 $return_url = esc_url_raw($order->get_checkout_order_received_url());
155
156 if (!(substr( $return_url, 0, 4 ) === "http")) {
157 $return_url = get_site_url().$return_url;
158 }
159
160 $payment_data = [
161 'amount' => $amount,
162 'currency' => get_woocommerce_currency(),
163 'payment_method' => $this->id,
164 'billing' => $address_data->get_billing(),
165 'shipping' => $address_data->get_shipping(),
166 'hosted_payment' => [
167 'return_url' => $return_url,
168 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
169 ],
170 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
171 'metadata' => [
172 'order_id' => $order_id,
173 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
174 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
175 ],
176 "save_card"=> false,
177 "force_3ds"=> false
178 ];
179
180 /**
181 * Filter the payment data before it's used
182 *
183 * @param array $payment_data
184 * @param int $order_id
185 * @param array $customer_details
186 * @param PayplugAddressData $address_data
187 */
188 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
189 $payment = $this->api->payment_create($payment_data);
190
191 // Save transaction id for the order
192 PayplugWoocommerceHelper::is_pre_30()
193 ? update_post_meta($order_id, '_transaction_id', $payment->id)
194 : $order->set_transaction_id($payment->id);
195
196 if (is_callable([$order, 'save'])) {
197 $order->save();
198 }
199
200 /**
201 * Fires once a payment has been created.
202 *
203 * @param int $order_id Order ID
204 * @param PaymentResource $payment Payment resource
205 */
206 \do_action('payplug_gateway_payment_created', $order_id, $payment);
207
208 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
209 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
210
211 PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id));
212
213 return [
214 'result' => 'success',
215 'redirect' => $payment->hosted_payment->payment_url,
216 'cancel' => $payment->hosted_payment->cancel_url,
217 ];
218
219 } catch (HttpException $e) {
220 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
221 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
222 } catch (\Exception $e) {
223 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
224 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
225 }
226
227 }
228
229 /**
230 * Process refund for an order paid with PayPlug gateway.
231 *
232 * @param int $order_id
233 * @param null $amount
234 * @param string $reason
235 *
236 * @return bool|\WP_Error
237 */
238 public function process_refund($order_id, $amount = null, $reason = ''){
239
240 PayplugGateway::log(sprintf('Processing refund for order #%s', $order_id));
241
242 if( !$this->user_logged_in()){
243 PayplugGateway::log(__('You must be logged in with your PayPlug account.', 'payplug'), 'error');
244 return new \WP_Error('process_refund_error', __('You must be logged in with your PayPlug account.', 'payplug'));
245 }
246
247 $order = wc_get_order($order_id);
248 if (!$order instanceof \WC_Order) {
249 PayplugGateway::log(sprintf('The order #%s does not exist.', $order_id), 'error');
250
251 return new \WP_Error('process_refund_error', sprintf(__('The order %s does not exist.', 'payplug'), $order_id));
252 }
253
254 if ($order->get_status() === "cancelled") {
255 PayplugGateway::log(sprintf('The order #%s cannot be refund.', $order_id), 'error');
256
257 return new \WP_Error('process_refund_error', sprintf(__('The order %s cannot be refund.', 'payplug'), $order_id));
258 }
259
260 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
261 if (empty($transaction_id)) {
262 PayplugGateway::log(sprintf('The order #%s does not have PayPlug transaction ID associated with it.', $order_id), 'error');
263
264 return new \WP_Error('process_refund_error', __('No PayPlug transaction was found for this order. The refund could not be processed.', 'payplug'));
265 }
266
267 add_action('admin_head', [$this, 'hide_wc_refund_button'] );
268
269 //TODO:: add the refund process, for now we don't have any scenario where we allow refund for the new gateways
270 PayplugGateway::log(__('payplug_refund_disabled_error', 'payplug'), 'error');
271 return new \WP_Error('process_refund_error', __('payplug_refund_disabled_error', 'payplug'));
272
273 }
274
275
276 public function hide_wc_refund_button(){
277 global $post;
278 $payment_methods = ['giropay', 'satispay', 'sofort', 'ideal', 'mybank'];
279 $order = new \WC_Order($post->ID);
280 if (in_array($order->get_payment_method(), $payment_methods)) {
281 ?>
282 <script>
283 jQuery(function () {
284 jQuery('.refund-items').attr("disabled", true);
285 });
286 </script>
287 <?php
288 }
289 }
290
291 /**
292 * refund not possible for PPRO payments
293 *
294 * @return void
295 */
296 public function refund_not_available($order)
297 {
298 if ($this->id === $order->get_payment_method() ) {
299 echo "<p style='color: red;'>" . __('payplug_refund_disabled_error', 'payplug') . "</p>";
300 }
301 }
302
303 /**
304 *
305 * Billing and shipping addresses should have the same country and allowed by Oney
306 * https://payplug-prod.atlassian.net/browse/WOOC-227
307 *
308 * @param $order
309 * @return bool
310 *
311 */
312 private function validate_shipping_billing_country($shipping_country, $billing_country)
313 {
314 if($billing_country === $shipping_country)
315 return true;
316
317 return false;
318 }
319
320 /**
321 * Empty the cart and add all order_items
322 *
323 * @param $cart
324 * @param $items
325 * @return void
326 */
327 private function order_items_to_cart($cart, $items){
328 $cart->empty_cart();
329 foreach ($items as $item){
330 $cart->add_to_cart($item->get_product_id(), $item->get_quantity());
331 }
332 }
333
334 /**
335 * get threshold values for the current payment method
336 * @param $account
337 * @return void
338 */
339 private function get_thresholds_values($account){
340
341 if(!empty($account["payment_methods"][$this->id]['min_amounts']['EUR'])){
342 $this->min_thresholds = floatval($account["payment_methods"][$this->id]['min_amounts']['EUR']/100);
343
344 }
345
346 if(!empty($account["payment_methods"][$this->id]['max_amounts']['EUR'])){
347 $this->max_thresholds = floatval($account["payment_methods"][$this->id]['max_amounts']['EUR']/100);
348
349 }
350
351 }
352
353 }
354