Пример #1
0
	/**
	 * Check if $this Something is in a Basket which is in Pending payment state, but which is not $notBasketId
	 *
	 * @param  int  $notBasketId
	 * @return boolean
	 */
	public function hasPendingPayment( $notBasketId ) {
		$paymentBasket		=	new cbpaidPaymentBasket( $this->_db );
		$basketLoaded		=	$paymentBasket->loadLatestBasketOfUserPlanSubscription( $this->user_id, $this->plan_id, $this->id, 'Pending', $notBasketId );
		return $basketLoaded;
	}
	/**
	 * Get the most recent payment basket, checking for time-out, and deleting if timed out !
	 *
	 * if returned cbpaidPaymentBasket has ->id != null then it's existing !
	 * @static
	 *
	 * @param  int|null $userid             User Id
	 * @param  boolean  $generateNewBasket  Delete expired basket and Generate new basket systematically
	 * @param  boolean  $generateWarning    true: sets error message with warning "A payment invoice exists already: Please check below if it is correct. If not correct, click on the cancel link below, and select your choice again."
	 * @return cbpaidPaymentBasket          Payment basket
	 */
	public static function & getInstanceBasketOfUser( $userid, $generateNewBasket, $generateWarning = true ) {
		global $_CB_framework, $_CB_database;

		$paymentBasket						=	new cbpaidPaymentBasket( $_CB_database );
		if ( $paymentBasket->loadLatestBasketOfUserPlanSubscription( $userid ) ) {

			// auto-expire basket of more than 30 minutes:
			$cbpaidTimes					=&	cbpaidTimes::getInstance();
			$initiatedAt					=	$cbpaidTimes->strToTime( $paymentBasket->time_initiated );

			if ( $generateNewBasket || ( $initiatedAt < ( $_CB_framework->now() - 1800 ) ) ) {
				// auto-expire basket of more than 30 minutes:
				$paymentBasket->delete();
				$paymentBasket				=	new cbpaidPaymentBasket( $_CB_database );
			} else {
				// otherwise return existing basket
				if ( $generateWarning ) {
					cbpaidApp::getBaseClass()->_setErrorMSG( CBPTXT::T("A payment invoice exists already: Please check below if it is correct. If not correct, click on the cancel link below, and select your choice again.") );
				}
			}
		}
		return $paymentBasket;
	}
	/** *** From PAY and onDuringLogin For Now !
	 * Shows a payment form corresponding to the latest payment basket (otpionally of a given subscription) and gives its status
	 *
	 * @param  UserTable             $user
	 * @param  cbpaidProduct[]|null  $chosenPlans      array of cbpaidProduct : Chosen plans to pay
	 * @param  string                $introText
	 * @param  array                 $subscriptionIds  array of int: Subscription ids to pay
	 * @param  string                $paymentStatus    returns one of following: 'noBasketFound', and all cpayPaymentBasket statuses: treated here: null (payment not made), 'Pending', 'Completed'
	 * @return string|null
	 */
	public static function showPaymentForm( &$user, $chosenPlans, $introText, $subscriptionIds, &$paymentStatus ) {
		// get the most recent payment basket for that user and plan, and with that subscription if $subscriptionId != null:
		$paymentBasket		=	new cbpaidPaymentBasket();
		if ( is_array( $chosenPlans ) ) {
			/** @var $lastPlan cbpaidProduct|boolean */
			$lastPlan		=	end( $chosenPlans );
			reset( $chosenPlans );
			if ( $lastPlan === false ) {
				$lastPlanId	=	null;
			} else {
				$lastPlanId	=	$lastPlan->get( 'id' );
			}
		} else {
			$lastPlanId		=	(int) $chosenPlans;
			if ( ! $lastPlanId ) {
				$lastPlanId	=	null;
			}
		}
		if ( is_array( $subscriptionIds ) && ( count( $subscriptionIds ) > 0 ) ) {
			$lastPlanAndSubId	=	end( $subscriptionIds );
			reset( $subscriptionIds );
			if ( count( $lastPlanAndSubId ) == 2 ) {
				list( $lastPlanId, $lastSubId )	=	$lastPlanAndSubId;
			} else {
				$lastSubId	=	null;
			}
		} else {
			$lastSubId		=	null;
		}
		$basketLoaded		=	$paymentBasket->loadLatestBasketOfUserPlanSubscription( $user->id, $lastPlanId, $lastSubId );
		if ( $basketLoaded ) {
			$paymentStatus	=	$paymentBasket->payment_status;
			// display basket and payment buttons or redirect for payment depending if multiple payment choices or intro text present:
			$result			=	self::showBasketForPayment( $user, $paymentBasket, $introText );
		} else {
			$basketLoaded			=	$paymentBasket->loadLatestBasketOfUserPlanSubscription( $user->id, $lastPlanId, $lastSubId, 'Pending' );
			if ( ! $basketLoaded ) {
				// This is an error condition, subscription has been created, and is called for payment but no basket is found in database, so create a new one:
				// $paymentStatus = 'noBasketFound';
				// cbpaidApp::getBaseClass()->_setErrorMSG("No payment basket found, creating new one.");
				// $result = null;
				cbpaidApp::getBaseClass()->_setErrorMSG(CBPTXT::T("No payment basket found, creating new one."));
			}
			if ( $chosenPlans && ( count( $chosenPlans ) > 0 ) ) {
				$paymentBasket		=	cbpaidControllerOrder::createSubscriptionsAndPayment( $user, $chosenPlans, array(), null, $subscriptionIds, null, null );
				if ( is_object( $paymentBasket ) ) {
					if ( $basketLoaded ) {
						cbpaidApp::getBaseClass()->_setErrorMSG( CBPTXT::T("A payment basket is pending in progress for payment for this. Are you sure that you didn't already pay for this ?. Here you can pay again:") );
					}
					$paymentStatus	=	$paymentBasket->payment_status;
					$result			=	self::showBasketForPayment( $user, $paymentBasket, $introText );
				} else {
					$result			=	$paymentBasket;		// display messages as nothing has to be paid.
				}
			} else {
				// can be called with chosenPlans null to try loading a valid payment basket from the user:
				// trigger_error( 'cbpaid:_showPaymentForm: no chosen plans.', E_USER_NOTICE );
				$result				=	null;
			}
		}
		return $result;
	}