| 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 |
$reference = false; |
| 157 |
|
| 158 |
if ( !empty( $data['error'] ) ) { |
| 159 |
|
| 160 |
wc_add_notice( __( 'Payment failed', 'wc_makecommerce_domain' ), 'error' ); |
| 161 |
|
| 162 |
return $returnUrl; |
| 163 |
} |
| 164 |
|
| 165 |
if ( !empty( $data['message_type'] ) ) { |
| 166 |
|
| 167 |
if ( $data['message_type'] == 'payment_return' ) { |
| 168 |
|
| 169 |
$transactionId = $data['transaction']; |
| 170 |
$reference = $data['reference']; |
| 171 |
$paymentStatus = $data['status']; |
| 172 |
} |
| 173 |
|
| 174 |
if ( $data['message_type'] == 'token_return' ) { |
| 175 |
if ( !empty( $data['transaction']['reference'] ) ) { |
| 176 |
$reference = $data['transaction']['reference']; |
| 177 |
} |
| 178 |
|
| 179 |
$paymentStatus = $data['transaction']['status']; |
| 180 |
$transactionId = $data['transaction']['id']; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved |
| 186 |
if ( !$reference ) { |
| 187 |
return $returnUrl; |
| 188 |
} |
| 189 |
|
| 190 |
$order = new \WC_Order( $reference ); |
| 191 |
|
| 192 |
// if we din't find the Order, we send user to shop landing-page. TODO: could be improved |
| 193 |
if ( !$order->get_id() ) { |
| 194 |
return $returnUrl; |
| 195 |
} |
| 196 |
|
| 197 |
$returnUrl = $order->get_checkout_order_received_url(); |
| 198 |
|
| 199 |
if ( $paymentStatus == 'EXPIRED' || $paymentStatus == 'CANCELLED' || $paymentStatus == 'COMPLETED' ) { |
| 200 |
|
| 201 |
//get transaction to update the payment method. |
| 202 |
$transaction = $api->getTransaction( $transactionId ); |
| 203 |
|
| 204 |
//make sure the payment method is the same for simplecheckout and normal version. |
| 205 |
$paymentMethod = ""; |
| 206 |
if ( isset( $transaction->type ) && isset( $transaction->method ) ) { |
| 207 |
$paymentMethod = $transaction->channel; |
| 208 |
} |
| 209 |
|
| 210 |
//update _makecommerce_preselected_method |
| 211 |
update_post_meta( $order->get_id(), '_makecommerce_preselected_method', $paymentMethod ); |
| 212 |
} |
| 213 |
|
| 214 |
switch( $paymentStatus ) { |
| 215 |
|
| 216 |
case 'CANCELLED': |
| 217 |
$order->update_status( 'cancelled' ); |
| 218 |
wc_add_notice( __( 'Payment transaction cancelled', 'wc_makecommerce_domain' ), 'error'); |
| 219 |
$returnUrl = wc_get_cart_url(); |
| 220 |
|
| 221 |
break; |
| 222 |
case 'EXPIRED': |
| 223 |
//only update if order status is pending payment |
| 224 |
if ( $order->get_status() == "pending" ) { |
| 225 |
|
| 226 |
$order->update_status( 'cancelled' ); |
| 227 |
wc_add_notice( __( 'Payment transaction expired', 'wc_makecommerce_domain' ), 'error'); |
| 228 |
$returnUrl = wc_get_cart_url(); |
| 229 |
} |
| 230 |
|
| 231 |
break; |
| 232 |
case 'COMPLETED': |
| 233 |
$orderNote = array(); |
| 234 |
$orderNote[] = __( 'Transaction ID', 'wc_makecommerce_domain' ) . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>'; |
| 235 |
$orderNote[] = __( 'Payment option', 'wc_makecommerce_domain' ) . ': ' . get_post_meta( $order->get_id(), '_makecommerce_preselected_method', true ); |
| 236 |
$order->add_order_note( implode( "\r\n", $orderNote ) ); |
| 237 |
|
| 238 |
if ( !empty( $data['token'] ) && !empty( $data['token']['multiuse'] ) ) { |
| 239 |
|
| 240 |
update_post_meta( $order->get_id(), '_makecommerce_payment_token', $data['token']['id'] ); |
| 241 |
update_post_meta( $order->get_id(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until'] ); |
| 242 |
} |
| 243 |
|
| 244 |
if ( isset( $_GET['lang1'] ) ) { |
| 245 |
$order->update_meta_data( 'wpml_language', $_GET["lang1"] ); |
| 246 |
} |
| 247 |
|
| 248 |
$order->payment_complete( $transactionId ); |
| 249 |
$woocommerce->cart->empty_cart(); |
| 250 |
|
| 251 |
break; |
| 252 |
} |
| 253 |
|
| 254 |
return $returnUrl; |
| 255 |
} |
| 256 |
} |