Plugin Directory

Changeset 1983253


Ignore:
Timestamp:
11/30/2018 01:28:05 PM (7 years ago)
Author:
mychoice2pay
Message:

Added support for subscriptions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • woo-gateway-mc2p/trunk/wc_gateway_mc2p.php

    r1731562 r1983253  
    66 * Author: MyChoice2Pay
    77 * Author URI: https://www.mychoice2pay.com/
    8  * Version: 1.0.0
     8 * Version: 1.2.0
    99 * Text Domain: wc_mc2p_payment_gateway
    1010 * Domain Path: /i18n/languages/
     
    6262 * @class       WC_Gateway_MC2P
    6363 * @extends     WC_Payment_Gateway
    64  * @version     1.0.0
     64 * @version     1.2.0
    6565 * @package     WooCommerce/Classes/Payment
    6666 * @author      MyChoice2Pay
     
    7878function wc_mc2p_gateway_init() {
    7979    class WC_Gateway_MC2P extends WC_Payment_Gateway {
     80
     81        public $supports = array(
     82            'subscriptions'
     83        );
     84
    8085        /**
    8186         * Constructor for the gateway.
     
    226231            }
    227232
     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
    228265            // Create transaction
    229266            $transaction = $mc2p->Transaction(
     
    231268                    "order_id" => $order_id,
    232269                    "currency" => get_woocommerce_currency(),
    233                     "return_url"  => $this->get_return_url($order),
     270                    "return_url"  => $this->get_return_url( $order ),
    234271                    "cancel_url" => $order->get_cancel_order_url(),
    235272                    "notify_url" => $this->notify_url,
     
    248285            $transaction->save();
    249286
    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 levels
    254             $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 
    262287            return array(
    263288                'result'    => 'success',
    264289                '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()
    265355            );
    266356        }
Note: See TracChangeset for help on using the changeset viewer.