PluginProbe
MakeCommerce for WooCommerce / 3.0.6
MakeCommerce for WooCommerce v3.0.6
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.6, at payment/payment.php

295 lines 7.4 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 wc_add_notice( __( 'Payment failed', 'wc_makecommerce_domain' ), 'error' );
162
163 return $returnUrl;
164 }
165
166 if ( !empty( $data['message_type'] ) ) {
167
168 if ( $data['message_type'] == 'payment_return' ) {
169
170 $transactionId = $data['transaction'];
171 $reference = $data['reference'];
172 $paymentStatus = $data['status'];
173 }
174
175 if ( $data['message_type'] == 'token_return' ) {
176 if ( !empty( $data['transaction']['reference'] ) ) {
177 $reference = $data['transaction']['reference'];
178 }
179
180 $paymentStatus = $data['transaction']['status'];
181 $transactionId = $data['transaction']['id'];
182 }
183 }
184 }
185
186 // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved
187 if ( !$transactionId ) {
188 return $returnUrl;
189 }
190
191 //get the post id using transactionid
192 $orderId = self::get_postid_using_metakey( '_makecommerce_transaction_id', $transactionId );
193
194 //still nothing. Lets try matching _order_number (SCO doesnt seem to have transaction id, transaction is created via SCO instead)
195 if ( $orderId == null ) {
196 $orderId = self::get_postid_using_metakey( '_order_number', $reference );
197 }
198
199 //try using reference as orderId
200 if ( !$orderId ) {
201 $orderId = $reference;
202 }
203
204 $order = new \WC_Order( $orderId );
205
206 // if we didn't find the Order, we send user to shop landing-page. TODO: could be improved
207 if ( !$order->get_id() ) {
208 return $returnUrl;
209 }
210
211 $returnUrl = $order->get_checkout_order_received_url();
212
213 if ( $paymentStatus == 'EXPIRED' || $paymentStatus == 'CANCELLED' || $paymentStatus == 'COMPLETED' ) {
214
215 //get transaction to update the payment method.
216 $transaction = $api->getTransaction( $transactionId );
217
218 //make sure the payment method is the same for simplecheckout and normal version.
219 $paymentMethod = "";
220 if ( isset( $transaction->type ) && isset( $transaction->method ) ) {
221 $paymentMethod = $transaction->channel;
222 }
223
224 //update _makecommerce_preselected_method
225 update_post_meta( $order->get_id(), '_makecommerce_preselected_method', $paymentMethod );
226 }
227
228 switch( $paymentStatus ) {
229
230 case 'CANCELLED':
231 $order->update_status( 'cancelled' );
232 wc_add_notice( __( 'Payment transaction cancelled', 'wc_makecommerce_domain' ), 'error');
233 $returnUrl = wc_get_cart_url();
234
235 break;
236 case 'EXPIRED':
237 //only update if order status is pending payment
238 if ( $order->get_status() == "pending" ) {
239
240 $order->update_status( 'cancelled' );
241 wc_add_notice( __( 'Payment transaction expired', 'wc_makecommerce_domain' ), 'error');
242 $returnUrl = wc_get_cart_url();
243 }
244
245 break;
246 case 'COMPLETED':
247 $orderNote = array();
248 $transactionIdText = __( 'Transaction ID', 'wc_makecommerce_domain' );
249 $paymentOptionText = __( 'Payment option', 'wc_makecommerce_domain' );
250 $transactionIdText = \MakeCommerce\i18n::get_string_from_mo( 'Transaction ID', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
251 $paymentOptionText = \MakeCommerce\i18n::get_string_from_mo( 'Payment option', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
252
253 $orderNote[] = $transactionIdText . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>';
254 $orderNote[] = $paymentOptionText . ': ' . get_post_meta( $order->get_id(), '_makecommerce_preselected_method', true );
255 $order->add_order_note( implode( "\r\n", $orderNote ) );
256
257 if ( !empty( $data['token'] ) && !empty( $data['token']['multiuse'] ) ) {
258
259 update_post_meta( $order->get_id(), '_makecommerce_payment_token', $data['token']['id'] );
260 update_post_meta( $order->get_id(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until'] );
261 }
262
263 if ( isset( $_GET['lang1'] ) ) {
264 $order->update_meta_data( 'wpml_language', $_GET["lang1"] );
265 }
266
267 $order->payment_complete( $transactionId );
268 $woocommerce->cart->empty_cart();
269
270 break;
271 }
272
273 return $returnUrl;
274 }
275
276 /**
277 * Returns post_id using transaction id or false if not found
278 *
279 * @since 3.0.4
280 */
281 public static function get_postid_using_metakey( $meta_key, $transactionId ) {
282
283 global $wpdb;
284
285 $wpdb->query("SELECT `post_id` FROM $wpdb->postmeta WHERE `meta_key` = '". $meta_key ."' AND `meta_value` = '" . $transactionId . "'");
286
287 foreach ( $wpdb->last_result as $row ) {
288 if ( isset( $row->post_id ) ) {
289 return $row->post_id;
290 }
291 }
292
293 return false;
294 }
295 }