Пример #1
0
	/**
	 * Times out unused baskets according to general settings
	 *
	 * @param  int|null  $userId  Timeout for User id only (triggered by user), or if 0: triggered by admin
	 * @param  int       $limit   Maximum number of baskets to timeout this time
	 * @return int|null           Number of baskets that just timed out
	 */
	public function timeoutUnusedBaskets( $userId, $limit = 100 ) {
		global $_CB_database;

		$params		=	cbpaidApp::settingsParams();

		$query		=	"SELECT b.id FROM #__cbsubs_payment_baskets b"
			.	"\n WHERE b.payment_status = 'NotInitiated'"
			.	"\n AND b.payment_method IS NULL"
		;
		if ( $userId ) {
			$query	.=	"\n AND b.user_id = " . (int) $userId;
			$hours	=	$params->get( 'basket_timeout_user', 3 );
		} else {
			$hours	=	$params->get( 'basket_timeout_admin', 24 );
		}
		$query		.=	"\n AND b.time_initiated < DATE_SUB( NOW(), INTERVAL " . ( (int) $hours ) . " HOUR)";
		$_CB_database->setQuery( $query, 0, $limit );
		$ids		=	$_CB_database->loadResultArray();
		if ( is_array( $ids ) ) {
			foreach ( $ids as $basketId ) {
				$paymentBasket	=	new cbpaidPaymentBasket( $_CB_database );
				$paymentBasket->historySetMessage( 'Basket timeout' );
				$paymentBasket->delete( $basketId );
			}
			return count( $ids );
		} else {
			return null;
		}

	}
	/**
	 * 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;
	}