PluginProbe
MakeCommerce for WooCommerce / 3.0.4
MakeCommerce for WooCommerce v3.0.4
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.4, at payment/payment.php

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