PluginProbe
MakeCommerce for WooCommerce / 3.0.10
MakeCommerce for WooCommerce v3.0.10
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / payment / payment.php

payment.php in MakeCommerce for WooCommerce 3.0.10, at payment/payment.php

309 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The payment functionality of the plugin.
5 *
6 * @link https://makecommerce.net/
7 * @since 3.0.0
8 *
9 * @package Makecommerce
10 * @subpackage Makecommerce/admin
11 */
12
13 namespace MakeCommerce;
14
15 /**
16 * The payment functionality of the plugin.
17 *
18 * Defines the plugin name, version, and two examples hooks for how to
19 * enqueue the admin-specific stylesheet and JavaScript.
20 *
21 * @package Makecommerce
22 * @subpackage Makecommerce/payment
23 * @author Maksekeskus AS <support@maksekeskus.ee>
24 */
25 class Payment {
26
27 /**
28 * The ID of this plugin.
29 *
30 * @since 3.0.0
31 * @access private
32 * @var string $plugin_name The ID of this plugin.
33 */
34 private $plugin_name;
35
36 /**
37 * The version of this plugin.
38 *
39 * @since 3.0.0
40 * @access private
41 * @var string $version The current version of this plugin.
42 */
43 private $version;
44
45 private Loader $loader;
46
47 /**
48 * Initialize the class and set its properties.
49 *
50 * @since 3.0.0
51 * @param string $plugin_name The name of this plugin.
52 * @param string $version The version of this plugin.
53 */
54 public function __construct( $plugin_name, $version, Loader $loader ) {
55
56 $this->plugin_name = $plugin_name;
57 $this->version = $version;
58 $this->loader = $loader;
59
60 $this->loader->add_action( 'plugins_loaded', $this, 'initialize_payment_gateways' );
61 }
62
63 /**
64 * Initialize all payment gateways
65 *
66 * @since 3.0.0
67 */
68 public function initialize_payment_gateways() {
69
70 //Simplecheckout
71 if ( get_option( 'mk_checkout_sco', 'no' ) == "yes" ) {
72 new Payment\Gateway\Simplecheckout();
73 }
74
75 //WooCommerce
76 new Payment\Gateway\WooCommerce( true );
77 }
78
79 /**
80 * Returns price excluding taxes
81 *
82 * @since 3.0.0
83 */
84 public static function get_rate_without_taxes( $method, $rate ) {
85
86 if ( empty( $rate[$method] ) ) {
87 return 0;
88 }
89
90 $rate = $rate[$method];
91
92 return $rate->cost;
93 }
94
95 /**
96 * Returns price including taxes
97 *
98 * @since 3.0.0
99 */
100 public static function get_rate_with_taxes( $method, $rate ) {
101
102 if ( empty( $rate[$method] ) ) {
103 return 0;
104 }
105
106 $rate = $rate[$method];
107 $price = $rate->cost;
108
109 if ( empty( $rate->taxes ) ) {
110 return $price;
111 }
112
113 foreach ( $rate->taxes as $tax ) {
114 $price += $tax;
115 }
116
117 return $price;
118 }
119
120 /**
121 * Update banklinks
122 *
123 * @since 3.0.0
124 */
125 public static function update_banklinks() {
126
127 $obj = new Payment\Gateway\WooCommerce( true );
128
129 $obj->mc_banklinks_reload( true );
130
131 }
132
133 /**
134 * Check payment and process order if completed, canceled
135 *
136 * @since 3.0.0
137 */
138 public static function check_payment() {
139
140 global $woocommerce;
141
142 $api = \MakeCommerce::get_api();
143
144 //set the correct language
145 if ( isset( $_GET["lang1"] ) ) {
146 \MakeCommerce\i18n::switch_language( $_GET["lang1"] );
147 }
148
149 $returnUrl = home_url();
150
151 $request = stripslashes_deep( $_POST );
152
153 if ( $api->verifyMac( $request ) ) {
154
155 $data = $api->extractRequestData( $request );
156 $transactionId = false;
157 $reference = false;
158
159 if ( !empty( $data['error'] ) ) {
160
161 //add error and send client back to checkout
162 wc_add_notice( __( 'Payment failed', 'wc_makecommerce_domain' ), 'error' );
163 $returnUrl = wc_get_checkout_url();
164
165 return $returnUrl;
166 }
167
168 if ( !empty( $data['message_type'] ) ) {
169
170 if ( $data['message_type'] == 'payment_return' ) {
171
172 $transactionId = $data['transaction'];
173 $reference = $data['reference'];
174 $paymentStatus = $data['status'];
175 }
176
177 if ( $data['message_type'] == 'token_return' ) {
178 if ( !empty( $data['transaction']['reference'] ) ) {
179 $reference = $data['transaction']['reference'];
180 }
181
182 $paymentStatus = $data['transaction']['status'];
183 $transactionId = $data['transaction']['id'];
184 }
185 }
186 }
187
188 // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved
189 if ( !$transactionId ) {
190 return $returnUrl;
191 }
192
193 //get the post id using transactionid
194 $orderId = self::get_postid_using_metakey( '_makecommerce_transaction_id', $transactionId );
195
196 //still nothing. Lets try matching _order_number (SCO doesnt seem to have transaction id, transaction is created via SCO instead)
197 if ( $orderId == null ) {
198 $orderId = self::get_postid_using_metakey( '_order_number', $reference );
199 }
200
201 //try using reference as orderId
202 if ( !$orderId ) {
203 $orderId = $reference;
204 }
205
206 $order = new \WC_Order( $orderId );
207
208 // if we didn't find the Order, we send user to shop landing-page. TODO: could be improved
209 if ( !$order->get_id() ) {
210 return $returnUrl;
211 }
212
213 $returnUrl = $order->get_checkout_order_received_url();
214
215 if ( $paymentStatus == 'EXPIRED' || $paymentStatus == 'CANCELLED' || $paymentStatus == 'COMPLETED' ) {
216
217 //get transaction to update the payment method.
218 $transaction = $api->getTransaction( $transactionId );
219
220 //make sure the payment method is the same for simplecheckout and normal version.
221 $paymentMethod = "";
222 if ( isset( $transaction->type ) && isset( $transaction->method ) ) {
223 $paymentMethod = $transaction->channel;
224 }
225
226 //update _makecommerce_preselected_method
227 update_post_meta( $order->get_id(), '_makecommerce_preselected_method', $paymentMethod );
228 }
229
230 if ( $paymentStatus == 'CANCELLED' || ( $paymentStatus == 'EXPIRED' && $order->get_status() == "pending" ) ) {
231 //send client back to checkout
232 $returnUrl = wc_get_checkout_url();
233 }
234
235 //check if we already processed this status in the past.
236 if (get_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', true ) == $paymentStatus ) {
237 return $returnUrl;
238 }
239
240 switch( $paymentStatus ) {
241
242 case 'CANCELLED':
243 $order->update_status( 'cancelled' );
244 wc_add_notice( __( 'Payment transaction cancelled', 'wc_makecommerce_domain' ), 'error' );
245 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
246
247 break;
248 case 'EXPIRED':
249 //only update if order status is pending payment
250 if ( $order->get_status() == "pending" ) {
251
252 $order->update_status( 'cancelled' );
253 wc_add_notice( __( 'Payment transaction expired', 'wc_makecommerce_domain' ), 'error' );
254 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
255 }
256
257 break;
258 case 'COMPLETED':
259 $orderNote = array();
260 $transactionIdText = __( 'Transaction ID', 'wc_makecommerce_domain' );
261 $paymentOptionText = __( 'Payment option', 'wc_makecommerce_domain' );
262 $transactionIdText = \MakeCommerce\i18n::get_string_from_mo( 'Transaction ID', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
263 $paymentOptionText = \MakeCommerce\i18n::get_string_from_mo( 'Payment option', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
264
265 $orderNote[] = $transactionIdText . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>';
266 $orderNote[] = $paymentOptionText . ': ' . get_post_meta( $order->get_id(), '_makecommerce_preselected_method', true );
267 $order->add_order_note( implode( "\r\n", $orderNote ) );
268
269 if ( !empty( $data['token'] ) && !empty( $data['token']['multiuse'] ) ) {
270
271 update_post_meta( $order->get_id(), '_makecommerce_payment_token', $data['token']['id'] );
272 update_post_meta( $order->get_id(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until'] );
273 }
274
275 if ( isset( $_GET['lang1'] ) ) {
276 $order->update_meta_data( 'wpml_language', $_GET["lang1"] );
277 }
278
279 $order->payment_complete( $transactionId );
280 $woocommerce->cart->empty_cart();
281
282 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
283
284 break;
285 }
286
287 return $returnUrl;
288 }
289
290 /**
291 * Returns post_id using transaction id or false if not found
292 *
293 * @since 3.0.4
294 */
295 public static function get_postid_using_metakey( $meta_key, $transactionId ) {
296
297 global $wpdb;
298
299 $wpdb->query("SELECT `post_id` FROM $wpdb->postmeta WHERE `meta_key` = '". $meta_key ."' AND `meta_value` = '" . $transactionId . "'");
300
301 foreach ( $wpdb->last_result as $row ) {
302 if ( isset( $row->post_id ) ) {
303 return $row->post_id;
304 }
305 }
306
307 return false;
308 }
309 }