/** * Process payment and notify user if IPN is received. */ public function actionIpn() { $ipn = new PPIpnAction($this, "ipn"); /* * Process payment * * See PPPhpTransaction for validation details, important values: * - PPPhpTransaction::currency = Valid currency (default: USD) * - PPPhpTransaction::amount = Minumum payment amount (default: 5.00) * * I recommend using an active record for storage / validation. */ $ipn->onRequest = array($this, "ipnRequest"); // Ignoring failures $ipn->onFailure = array($this, "ipnFailure"); // Send confirmation mail to customer $ipn->onSuccess = array($this, "ipnSuccess"); $ipn->run(); }
/** * Process payment and notify user if IPN is received. */ public function actionIpn() { $ipn = new PPIpnAction($this, "ipn"); /* * Process payment * * See PPPhpTransaction for validation details, important values: * - PPPhpTransaction::currency = Valid currency (default: USD) * - PPPhpTransaction::amount = Minumum payment amount (default: 5.00) * * I recommend using an active record for storage / validation. */ $ipn->onRequest = function ($event) { // Check if this is a transaction if (!isset($event->details["txn_id"])) { $event->msg = "Missing txn_id"; Yii::log($event->msg, "warning", "payPal.controllers.DefaultController"); $event->sender->onFailure($event); return; } Yii::log("Begin Paypal Transaction", "error", "payPal.controllers.DefaultController"); // Put payment details into a transaction model // $transaction = new PPPhpTransaction; // $transaction->paymentStatus = $event->details["payment_status"]; // $transaction->mcCurrency = $event->details["mc_currency"]; // $transaction->mcGross = $event->details["mc_gross"]; // $transaction->receiverEmail = $event->details["receiver_email"]; // $transaction->txnId = $event->details["txn_id"]; // Put payment details into a transaction model $transaction = new UserPayment(); $transaction->scenario = 'PaypalIPN'; $transaction->paypal_payment_status = $event->details["payment_status"]; $transaction->paypal_mc_currency = $event->details["mc_currency"]; $transaction->paypal_mc_gross = $event->details["mc_gross"]; $transaction->paypal_receiver_email = $event->details["receiver_email"]; $transaction->paypal_txn_id = $event->details["txn_id"]; $transaction->payment_value = $event->details["mc_gross"]; $transaction->payment_type = 'PAYPAL'; $transaction->payment_date = new CDbExpression('NOW()'); $transaction->user_id = (int) $event->details["custom"]; $transaction->payment_note = 'PayPal Payment of: ' . $event->details["mc_gross"]; // Successfully processed payment: Log and invoke success event if ($transaction->save()) { $event->msg = "Successfully processed payment"; Yii::log("{$event->msg}\nTransaction ID: {$event->details["txn_id"]}", "error", "payPal.controllers.DefaultController"); $event->sender->onSuccess($event); } else { $event->msg = "Could not process payment"; Yii::log("{$event->msg}\nTransaction ID: {$event->details["txn_id"]}", "error", "payPal.controllers.DefaultController"); $errorStr = print_r($transaction->getErrors(), true); Yii::log($errorStr, "error", "payPal.controllers.DefaultController"); $event->sender->onFailure($event); } }; // Ignoring failures $ipn->onFailure = function ($event) { // Could e.g. send a notification mail on certain events $respstr = print_r($event->details, true); Yii::log("IPN Failure" . $respstr, "error", "payPal.controllers.ipn.PPIpnAction"); }; // Send confirmation mail to customer $ipn->onSuccess = function ($event) { Yii::log("IPN Success", "error", "payPal.controllers.ipn.PPIpnAction"); // $to = $event->details["payer_email"]; // $from = $event->details["receiver_email"]; // $subject = "Payment received"; // $body = "Your payment has been processed.\n" . // "Receiver: $from\n" . // "Amount: {$event->details["mc_gross"]} {$event->details["mc_amount"]}\n"; // $headers="From: $from\r\nReply-To: $from"; $respstr = print_r($event->details, true); Yii::log("Success:" . $respstr, "error", "payPal.controllers.ipn.PPIpnAction"); //mail($to,$subject,$body,$headers); }; $ipn->run(); }