Using Validator in Controller

Okay, so I am trying to find out if the transaction id of a payment is unique in the DB or not. How do I do this in a controller? I know the CUniqueValidator exists but am lost on how to use it in a controller.

What is the alternative?

Here is my code so far-


	/**

	 * IPN notification action

	 */

	public function actionPaymentNotify()

	{

		// No layout.. this is just an action for payment notification

		$this->layout = false;


		switch($_GET['service'])

		{

			case Invoice::PAYMENT_PAYPAL:

				$paypal = new Paypal();


				// Log the IPN results

	//			$paypal->ipnLog = TRUE;


				// Enable test mode if debugging is on

				defined('YII_DEBUG')&&YII_DEBUG==true ? $paypal->enableTestMode() : null;


				// Check validity

				if ($paypal->validateIpn())

				{

					if ($paypal->ipnData['payment_status'] == 'Completed')

					{

						$model = Invoice::model()->findByPk($paypal->ipnData['item_number']);

						

						// Want the validation in this if

						if($paypal->ipnData['receiver_email'] == Yii::app()->params['paypalEmail']

						 	&& $paypal->ipnData['payment_amount'] == $model->amount

						 	&& $paypal->ipnData['currency_code'] == Yii::app()->params['currencyCode'])

						{

							$model->paid = 1;

							$model->transactionID = $paypal->ipnData['txn_id'];

							$model->paymentGateway = Invoice::PAYMENT_PAYPAL;

							$model->save();

						}

					}

				}


				break;

					

		}


	}

CActiveRecord::exists() should be fine.

Sorry, I didn’t mean to offend you, but this is overuse of conditional operator:




defined('YII_DEBUG')&&YII_DEBUG==true ? $paypal->enableTestMode() : null;

Isn’t it more readable:




if(YII_DEBUG)

   $paypal->enableTestMode();



Oh, thanks for the tip.

Something like this:




/**

 * IPN notification action

 */

public function actionPaymentNotify() {

    $this->layout = false;

    switch($_GET['service']) {

        case Invoice::PAYMENT_PAYPAL: paypalPayment(); break;

    }

}


private function paypalPayment() {

    $paypal = new Paypal();

//    $paypal->ipnLog = TRUE;

    if(YII_DEBUG) $paypal->enableTestMode();

    if ( ! $paypal->validateIpn()) return;

    if ( ! $paypal->ipnData['payment_status'] == 'Completed') return;

    if (Invoice::model()->exists('transactionID=:txnId',

        array(':txnId'=>$paypal->ipnData['txn_id']))) return;


    $invoice = Invoice::model()->findByPk($paypal->ipnData['item_number']);

    if ($paypal->ipnData['receiver_email'] == Yii::app()->params['paypalEmail']

     && $paypal->ipnData['payment_amount'] == $invoice->amount

     && $paypal->ipnData['currency_code']  == Yii::app()->params['currencyCode']) {

        $invoice->paid = 1;

        $invoice->transactionID = $paypal->ipnData['txn_id'];

        $invoice->paymentGateway = Invoice::PAYMENT_PAYPAL;

        $invoice->save();

    }

}



Do you have intentions to share your payment engine wrapper as an extension?

Yeah, I sure do. I am using this in an open source project which is currently being coded (the controller above). Its in trunk/protected/components/payment_gateways in the SVN.

Check it out:

Interested developers are welcome to pitch in.

Exciting idea and good choice of name! I can’t wait to try it out. :)

I haven’t browsed the views folder, but if you are ready with some of the interface, maybe you should update the project with a couple of screenshots.

I eagerly test all project management tools I possibly could.

Naah, the views aren’t done. Its the plain old Yii layout. I’ve got to find a designer who could do it for me as I suck at designing.

Would you like to help me with the coding?