Changeset 1983253
- Timestamp:
- 11/30/2018 01:28:05 PM (7 years ago)
- File:
-
- 1 edited
-
woo-gateway-mc2p/trunk/wc_gateway_mc2p.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
woo-gateway-mc2p/trunk/wc_gateway_mc2p.php
r1731562 r1983253 6 6 * Author: MyChoice2Pay 7 7 * Author URI: https://www.mychoice2pay.com/ 8 * Version: 1. 0.08 * Version: 1.2.0 9 9 * Text Domain: wc_mc2p_payment_gateway 10 10 * Domain Path: /i18n/languages/ … … 62 62 * @class WC_Gateway_MC2P 63 63 * @extends WC_Payment_Gateway 64 * @version 1. 0.064 * @version 1.2.0 65 65 * @package WooCommerce/Classes/Payment 66 66 * @author MyChoice2Pay … … 78 78 function wc_mc2p_gateway_init() { 79 79 class WC_Gateway_MC2P extends WC_Payment_Gateway { 80 81 public $supports = array( 82 'subscriptions' 83 ); 84 80 85 /** 81 86 * Constructor for the gateway. … … 226 231 } 227 232 233 if( class_exists( 'WC_Subscriptions_Order' ) && WC_Subscriptions_Order::order_contains_subscription( $order_id ) ) { 234 $result = $this->process_subscription_payment( $mc2p, $order, $language); 235 } else { 236 $result = $this->process_regular_payment( $mc2p, $order, $language); 237 } 238 239 // Mark as on-hold (we're awaiting the payment) 240 $order->update_status( 'on-hold', __( 'Awaiting MC2P payment', 'wc-gateway-mc2p' ) ); 241 242 // Reduce stock levels 243 $order->reduce_order_stock(); 244 245 if ( version_compare( WOOCOMMERCE_VERSION, '2.0', '<' ) ) { 246 $woocommerce->cart->empty_cart(); 247 } else { 248 WC()->cart->empty_cart(); 249 } 250 251 return $result; 252 } 253 254 255 /** 256 * Process regular payment and return the result 257 * 258 * @param object $mc2p 259 * @param object $order 260 * @param string $language 261 * @return array 262 */ 263 public function process_regular_payment( $mc2p, $order, $language ) { 264 228 265 // Create transaction 229 266 $transaction = $mc2p->Transaction( … … 231 268 "order_id" => $order_id, 232 269 "currency" => get_woocommerce_currency(), 233 "return_url" => $this->get_return_url( $order),270 "return_url" => $this->get_return_url( $order ), 234 271 "cancel_url" => $order->get_cancel_order_url(), 235 272 "notify_url" => $this->notify_url, … … 248 285 $transaction->save(); 249 286 250 // Mark as on-hold (we're awaiting the payment)251 $order->update_status( 'on-hold', __( 'Awaiting MC2P payment', 'wc-gateway-mc2p' ) );252 253 // Reduce stock levels254 $order->reduce_order_stock();255 256 if ( version_compare( WOOCOMMERCE_VERSION, '2.0', '<' ) ) {257 $woocommerce->cart->empty_cart();258 } else {259 WC()->cart->empty_cart();260 }261 262 287 return array( 263 288 'result' => 'success', 264 289 'redirect' => $transaction->getPayUrl() 290 ); 291 } 292 293 294 /** 295 * Process subscription payment and return the result 296 * 297 * @param object $mc2p 298 * @param object $order 299 * @param string $language 300 * @return array 301 */ 302 public function process_subscription_payment( $mc2p, $order, $language ) { 303 304 $unconverted_periods = array( 305 'period' => WC_Subscriptions_Order::get_subscription_period( $order ), 306 'trial_period' => WC_Subscriptions_Order::get_subscription_trial_period( $order ) 307 ); 308 309 $converted_periods = array(); 310 foreach ( $unconverted_periods as $key => $period ) { 311 switch( strtolower( $period ) ) { 312 case 'day': 313 $converted_periods[$key] = 'D'; 314 break; 315 case 'week': 316 $converted_periods[$key] = 'W'; 317 break; 318 case 'year': 319 $converted_periods[$key] = 'Y'; 320 break; 321 case 'month': 322 default: 323 $converted_periods[$key] = 'M'; 324 break; 325 } 326 } 327 328 $period = $converted_periods['period']; 329 $duration = WC_Subscriptions_Order::get_subscription_interval( $order ); 330 $price = WC_Subscriptions_Order::get_total_initial_payment( $order ); 331 332 // Create transaction 333 $subscription = $mc2p->Subscription( 334 array( 335 "order_id" => $order_id, 336 "currency" => get_woocommerce_currency(), 337 "return_url" => $this->get_return_url( $order ), 338 "cancel_url" => $order->get_cancel_order_url(), 339 "notify_url" => $this->notify_url, 340 "language" => $language, 341 "plan" => array( 342 "name" => __('Subscription of order ', 'wc-gateway-mc2p').$order_id, 343 "price" => $price, 344 "duration" => $duration, 345 "unit" => $period, 346 "recurring" => True 347 ) 348 ) 349 ); 350 $subscription->save(); 351 352 return array( 353 'result' => 'success', 354 'redirect' => $subscription->getPayUrl() 265 355 ); 266 356 }
Note: See TracChangeset
for help on using the changeset viewer.