PluginProbe
MakeCommerce for WooCommerce / 3.2.0
MakeCommerce for WooCommerce v3.2.0
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.2.0, at payment/payment.php

300 lines 7.9 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 //WooCommerce
70 new Payment\Gateway\WooCommerce( true );
71 }
72
73 /**
74 * Returns price excluding taxes
75 *
76 * @since 3.0.0
77 */
78 public static function get_rate_without_taxes( $method, $rate ) {
79
80 if ( empty( $rate[$method] ) ) {
81 return 0;
82 }
83
84 $rate = $rate[$method];
85
86 return $rate->cost;
87 }
88
89 /**
90 * Returns price including taxes
91 *
92 * @since 3.0.0
93 */
94 public static function get_rate_with_taxes( $method, $rate ) {
95
96 if ( empty( $rate[$method] ) ) {
97 return 0;
98 }
99
100 $rate = $rate[$method];
101 $price = $rate->cost;
102
103 if ( empty( $rate->taxes ) ) {
104 return $price;
105 }
106
107 foreach ( $rate->taxes as $tax ) {
108 $price += $tax;
109 }
110
111 return $price;
112 }
113
114 /**
115 * Update banklinks
116 *
117 * @since 3.0.0
118 */
119 public static function update_banklinks() {
120
121 $obj = new Payment\Gateway\WooCommerce( true );
122
123 $obj->mc_banklinks_reload( true );
124
125 }
126
127 /**
128 * Check payment and process order if completed, canceled
129 *
130 * @since 3.0.0
131 */
132 public static function check_payment() {
133
134 global $woocommerce;
135
136 $api = \MakeCommerce::get_api();
137
138 //set the correct language
139 if ( isset( $_GET["lang1"] ) ) {
140 \MakeCommerce\i18n::switch_language( $_GET["lang1"] );
141 }
142
143 $returnUrl = home_url();
144
145 $request = stripslashes_deep( $_POST );
146
147 if ( $api->verifyMac( $request ) ) {
148
149 $data = $api->extractRequestData( $request );
150 $transactionId = false;
151 $reference = false;
152
153 if ( !empty( $data['error'] ) ) {
154
155 //add status and send client back to checkout
156 return add_query_arg( 'mc_payment_status', 'failed', wc_get_checkout_url() );
157 }
158
159 if ( !empty( $data['message_type'] ) ) {
160
161 if ( $data['message_type'] == 'payment_return' ) {
162
163 $transactionId = $data['transaction'];
164 $reference = $data['reference'];
165 $paymentStatus = $data['status'];
166 }
167
168 if ( $data['message_type'] == 'token_return' ) {
169 if ( !empty( $data['transaction']['reference'] ) ) {
170 $reference = $data['transaction']['reference'];
171 }
172
173 $paymentStatus = $data['transaction']['status'];
174 $transactionId = $data['transaction']['id'];
175 }
176 }
177 }
178
179 // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved
180 if ( !$transactionId ) {
181 return $returnUrl;
182 }
183
184 //get the post id using transactionid
185 $orderId = self::get_postid_using_metakey( '_makecommerce_transaction_id', $transactionId );
186
187 //still nothing. Lets try matching _order_number (SCO doesnt seem to have transaction id, transaction is created via SCO instead)
188 if ( $orderId == null ) {
189 $orderId = self::get_postid_using_metakey( '_order_number', $reference );
190 }
191
192 //try using reference as orderId
193 if ( !$orderId ) {
194 $orderId = $reference;
195 }
196
197 $order = new \WC_Order( $orderId );
198
199 // if we didn't find the Order, we send user to shop landing-page. TODO: could be improved
200 if ( !$order->get_id() ) {
201 return $returnUrl;
202 }
203
204 $returnUrl = $order->get_checkout_order_received_url();
205
206 if ( $paymentStatus == 'EXPIRED' || $paymentStatus == 'CANCELLED' || $paymentStatus == 'COMPLETED' ) {
207
208 //get transaction to update the payment method.
209 $transaction = $api->getTransaction( $transactionId );
210
211 //make sure the payment method is the same for simplecheckout and normal version.
212 $paymentMethod = "";
213 if ( isset( $transaction->type ) && isset( $transaction->method ) ) {
214 $paymentMethod = $transaction->channel;
215 }
216
217 //update _makecommerce_preselected_method
218 update_post_meta( $order->get_id(), '_makecommerce_preselected_method', $paymentMethod );
219 }
220
221 if ( $paymentStatus == 'CANCELLED' || ( $paymentStatus == 'EXPIRED' && $order->get_status() == "pending" ) ) {
222 //send client back to checkout
223 $returnUrl = wc_get_checkout_url();
224 }
225
226 //check if we already processed this status in the past.
227 if (get_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', true ) == $paymentStatus ) {
228 return $returnUrl;
229 }
230
231 switch( $paymentStatus ) {
232
233 case 'CANCELLED':
234 $order->update_status( 'cancelled' );
235 $returnUrl = add_query_arg( 'mc_payment_status', 'cancelled', $returnUrl );
236 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
237
238 break;
239 case 'EXPIRED':
240 //only update if order status is pending payment
241 if ( $order->get_status() == "pending" ) {
242
243 $order->update_status( 'cancelled' );
244 $returnUrl = add_query_arg( 'mc_payment_status', 'expired', $returnUrl );
245 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
246 }
247
248 break;
249 case 'COMPLETED':
250 $orderNote = array();
251 $transactionIdText = __( 'Transaction ID', 'wc_makecommerce_domain' );
252 $paymentOptionText = __( 'Payment option', 'wc_makecommerce_domain' );
253 $transactionIdText = \MakeCommerce\i18n::get_string_from_mo( 'Transaction ID', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
254 $paymentOptionText = \MakeCommerce\i18n::get_string_from_mo( 'Payment option', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
255
256 $orderNote[] = $transactionIdText . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>';
257 $orderNote[] = $paymentOptionText . ': ' . get_post_meta( $order->get_id(), '_makecommerce_preselected_method', true );
258 $order->add_order_note( implode( "\r\n", $orderNote ) );
259
260 if ( !empty( $data['token'] ) && !empty( $data['token']['multiuse'] ) ) {
261
262 update_post_meta( $order->get_id(), '_makecommerce_payment_token', $data['token']['id'] );
263 update_post_meta( $order->get_id(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until'] );
264 }
265
266 if ( isset( $_GET['lang1'] ) ) {
267 $order->update_meta_data( 'wpml_language', $_GET["lang1"] );
268 }
269
270 $order->payment_complete( $transactionId );
271 $woocommerce->cart->empty_cart();
272
273 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
274
275 break;
276 }
277
278 return $returnUrl;
279 }
280
281 /**
282 * Returns post_id using transaction id or false if not found
283 *
284 * @since 3.0.4
285 */
286 public static function get_postid_using_metakey( $meta_key, $transactionId ) {
287
288 global $wpdb;
289
290 $wpdb->query("SELECT `post_id` FROM $wpdb->postmeta WHERE `meta_key` = '". $meta_key ."' AND `meta_value` = '" . $transactionId . "'");
291
292 foreach ( $wpdb->last_result as $row ) {
293 if ( isset( $row->post_id ) ) {
294 return $row->post_id;
295 }
296 }
297
298 return false;
299 }
300 }