/** * @param array $attributes * @return PaymentLog * @throws CException */ public static function create(array $attributes) { $model = new PaymentLog(); $model->attributes = $attributes; $model->createdAt = date('Y-m-d H:i:s'); if (!$model->save()) { throw new CException('Failed to save payment log.'); } return $model; }
public function actionIndex() { //error_reporting(E_ALL ^ E_NOTICE); // Read the post from PayPal and add 'cmd' $req = 'cmd=_notify-validate'; if (function_exists('get_magic_quotes_gpc')) { $get_magic_quotes_exists = true; } $txt = ''; foreach ($_REQUEST as $key => $value) { $txt .= $key . " = " . $value . "\r\n"; } Utils::logToPaymentFile('paypal_ipn', $txt); // save also to DB $pLog = new PaymentLog(); $pLog->date_created = date("Y-m-d h:i:s"); $pLog->log = $txt; $pLog->save(); foreach ($_REQUEST as $key => $value) { if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { $value = urlencode(stripslashes($value)); } else { $value = urlencode($value); } $req .= "&{$key}={$value}"; } // Post back to PayPal to validate $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; if (Yii::app()->params['paymentsTestMode']) { $fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); } else { $fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30); } // Process validation from PayPal // TODO: This sample does not test the HTTP response code. All // HTTP response codes must be handles or you should use an HTTP // library, such as cUrl if (!$fp) { // HTTP ERROR Utils::logToPaymentFile('paypal_ipn', $_REQUEST['subscr_id'] . " - HTTP ERROR!"); } else { // NO HTTP ERROR fputs($fp, $header . $req); while (!feof($fp)) { $res = fgets($fp, 1024); if (strcmp($res, "VERIFIED") == 0) { Utils::logToPaymentFile('paypal_ipn', $_REQUEST['subscr_id'] . " - RESULT: {$res}\n"); // TODO: // Check the payment_status is Completed // Check that txn_id has not been previously processed // Check that receiver_email is your Primary PayPal email // Check that payment_amount/payment_currency are correct // Process payment // If 'VERIFIED', send an email of IPN variables and values to the // specified email address try { Utils::logToPaymentFile('paypal_ipn', $_REQUEST['subscr_id'] . ' - Processing Payment'); $this->processPayment(); Utils::logToPaymentFile('paypal_ipn', $_REQUEST['subscr_id'] . ' - Processed OK'); } catch (Exception $e) { Utils::logToPaymentFile('paypal_ipn', $_REQUEST['subscr_id'] . ' - Processing Exception: ' . $e->getMessage()); } } else { if (strcmp($res, "INVALID") == 0) { Utils::logToPaymentFile('paypal_ipn', $_REQUEST['subscr_id'] . " - RESULT: {$res}\n"); } } } fclose($fp); } exit; }