public function purchasePlan($planType, $advertiserId = '') { $transaction = Yii::app()->db->beginTransaction(); try { // load plan and its plan limits if ($planType == PlanType::ADVERTISER_PLAN) { $plan = Plan::model()->findByAttributes(array('plan_id' => $this->plan_id, 'wlabel_id' => Yii::app()->user->getWhiteLabelId(), 'type' => $planType)); } else { $plan = Plan::model()->findByAttributes(array('plan_id' => $this->plan_id, 'type' => $planType)); } if ($plan == null) { throw new Exception("Plan doesn't exist"); } $planLimits = PlanLimit::model()->findAllByAttributes(array('plan_id' => $plan->plan_id)); if ($planLimits == null) { // throw new Exception("Plan Limits don't exist"); } // create new purchased_plans record $purchasedPlan = new PurchasedPlan(); $purchasedPlan->plan_id = $plan->plan_id; if ($planType == PlanType::ADVERTISER_PLAN) { $purchasedPlan->wlabel_id = $plan->wlabel_id; } else { $purchasedPlan->wlabel_id = $this->wlabel_id; } $purchasedPlan->type = $plan->type; if ($planType == PlanType::ADVERTISER_PLAN) { if (Yii::app()->user->isAdvertiser()) { $purchasedPlan->advertiser_id = Yii::app()->user->getAdvertiserId(); } else { $purchasedPlan->advertiser_id = $advertiserId; } } else { } $purchasedPlan->method = 0; $purchasedPlan->price = $plan->price; $purchasedPlan->date_created = date("Y-m-d h:i:s"); if (!$purchasedPlan->validate()) { throw new Exception("Plan purchase cannot be saved: " . $this->putErrorsToString($purchasedPlan->getErrors())); } $purchasedPlan->save(); // create new purchased_plans payments record $purchasedPlanPayment = new PurchasedPlanPayment(); $purchasedPlanPayment->pplan_id = $purchasedPlan->pplan_id; $purchasedPlanPayment->date_paid = $purchasedPlan->date_created; $purchasedPlanPayment->date_expire = Date('y-m-d', strtotime("+{$plan->duration} day")); $purchasedPlanPayment->transaction_id = 'TEST_PAYMENT'; if (!$purchasedPlanPayment->validate()) { throw new Exception("Plan payment cannot be saved: " . $this->putErrorsToString($purchasedPlanPayment->getErrors())); } $purchasedPlanPayment->save(); } catch (Exception $e) { $transaction->rollBack(); throw new CHttpException(400, 'DB Exception: ' . $e->getMessage()); } $transaction->commit(); return false; }
public static function insertNewPurchasedPlanPayment($pplanId, $duration) { $sql = "SELECT MAX(date_expire) as date_expire FROM p_purchased_plans_payments" . " WHERE pplan_id=" . $pplanId; $dbCommand = Yii::app()->db->createCommand($sql); $results = $dbCommand->queryAll(); if ($results) { $dateLast = $results[0]['date_expire']; } else { $dateLast = Date('y-m-d'); } // create new purchased_plans payments record $purchasedPlanPayment = new PurchasedPlanPayment(); $purchasedPlanPayment->pplan_id = $pplanId; $purchasedPlanPayment->date_paid = date("Y-m-d h:i:s"); $purchasedPlanPayment->date_expire = Date('Y-m-d', strtotime($dateLast . "+{$duration} day")); $purchasedPlanPayment->transaction_id = 'AUTHNET_PAYMENT'; if (!$purchasedPlanPayment->validate()) { throw new Exception("Plan payment cannot be saved: " . $this->putErrorsToString($purchasedPlanPayment->getErrors())); } $purchasedPlanPayment->save(); }
public function purchasePlan($subscriptionId, $advertiserId = '') { $transaction = Yii::app()->db->beginTransaction(); try { // load plan and its plan limits $plan = Plan::model()->findByAttributes(array('plan_id' => $this->plan_id, 'wlabel_id' => $this->wlabel_id)); if ($plan == null) { throw new Exception("Plan doesn't exist"); } // create new purchased_plans record $purchasedPlan = new PurchasedPlan(); $purchasedPlan->plan_id = $plan->plan_id; $purchasedPlan->wlabel_id = $plan->wlabel_id; $purchasedPlan->type = $plan->type; $purchasedPlan->duration = $plan->duration; $purchasedPlan->method = PaymentType::TYPE_AUTHORIZENET; $purchasedPlan->subscription_id = $subscriptionId; $purchasedPlan->advertiser_id = $this->advertiser_id; $purchasedPlan->method = 0; $purchasedPlan->price = $plan->price; $purchasedPlan->date_created = date("Y-m-d h:i:s"); if (!$purchasedPlan->validate()) { throw new Exception("Plan purchase cannot be saved: " . $this->putErrorsToString($purchasedPlan->getErrors())); } $purchasedPlan->save(); // create new purchased_plans payments record $purchasedPlanPayment = new PurchasedPlanPayment(); $purchasedPlanPayment->pplan_id = $purchasedPlan->pplan_id; $purchasedPlanPayment->date_paid = $purchasedPlan->date_created; $purchasedPlanPayment->date_expire = Date('Y-m-d', strtotime("+{$plan->duration} day")); $purchasedPlanPayment->transaction_id = 'AUTHNET_INIT_PAYMENT'; if (!$purchasedPlanPayment->validate()) { throw new Exception("Plan payment cannot be saved: " . $this->putErrorsToString($purchasedPlanPayment->getErrors())); } $purchasedPlanPayment->save(); } catch (Exception $e) { $transaction->rollBack(); throw new CHttpException(400, 'DB Exception: ' . $e->getMessage()); } $transaction->commit(); return true; }
private function processPayment() { if (!isset($_REQUEST['txn_type'])) { throw new Exception("Txn type doesn't exist"); } if (isset($_REQUEST['txn_type']) && $_REQUEST['txn_type'] != 'subscr_payment') { throw new Exception("Txn type is not payment: " . $_REQUEST['txn_type']); } if (!isset($_REQUEST['payment_status'])) { throw new Exception("Payment status doesn't exist"); } if (isset($_REQUEST['payment_status']) && $_REQUEST['payment_status'] != 'Completed') { throw new Exception("Payment not completed"); } $ids = explode('_', $_REQUEST['item_number']); $planId = $ids[0]; if (isset($ids[1])) { $advertiserId = $ids[1]; } else { $advertiserId = ''; } $plan = Plan::model()->findByPk($planId); if ($plan == null) { throw new Exception("Plan not found, ID: {$planId}"); } if ($plan->type == PlanType::ADVERTISER_PLAN) { if ($advertiserId == '') { throw new Exception("Advertiser not found in item_number"); } $advertiser = Advertiser::model()->findByPk($advertiserId); if ($advertiser == null) { throw new Exception("Advertiser not found, ID: {$advertiserId}"); } if ($advertiser->wlabel_id != $plan->wlabel_id) { throw new Exception("Advertiser and Plan belong to a different accounts: " . $advertiser->wlabel_id . " <> " . $plan->wlabel_id); } } $purchasedPlan = PurchasedPlan::model()->findByAttributes(array('subscription_id' => $_REQUEST['subscr_id'], 'wlabel_id' => $plan->wlabel_id)); if ($purchasedPlan == null) { if (intval($plan->price) != intval($_REQUEST['mc_gross'])) { throw new Exception("Advertiser and Plan have different prices, possible fraud? " . intval($plan->price) . " <> " . intval($_REQUEST['amount3'])); } // insert new purchased plan $purchasedPlan = new PurchasedPlan(); $purchasedPlan->plan_id = $plan->plan_id; $purchasedPlan->wlabel_id = $plan->wlabel_id; $purchasedPlan->type = $plan->type; if ($advertiserId != '') { $purchasedPlan->advertiser_id = $advertiserId; } $purchasedPlan->method = PaymentType::TYPE_PAYPAL; $purchasedPlan->price = $plan->price; $purchasedPlan->subscription_id = $_REQUEST['subscr_id']; $purchasedPlan->date_created = date("Y-m-d h:i:s"); if (!$purchasedPlan->validate()) { throw new Exception("Plan purchase cannot be saved: " . $this->putErrorsToString($purchasedPlan->getErrors())); } $purchasedPlan->save(); } // create new purchased_plans payments record $purchasedPlanPayment = PurchasedPlanPayment::model()->findByAttributes(array('transaction_id' => $_REQUEST['txn_id'])); if ($purchasedPlanPayment != null) { throw new Exception("There already exists payment with this transaction ID: " . $_REQUEST['txn_id']); } $purchasedPlanPayment = new PurchasedPlanPayment(); $purchasedPlanPayment->pplan_id = $purchasedPlan->pplan_id; $purchasedPlanPayment->date_paid = $purchasedPlan->date_created; $purchasedPlanPayment->date_expire = Date('y-m-d', strtotime("+{$plan->duration} day")); $purchasedPlanPayment->transaction_id = $_REQUEST['txn_id']; if (!$purchasedPlanPayment->validate()) { throw new Exception("Plan payment cannot be saved: " . $this->putErrorsToString($purchasedPlanPayment->getErrors())); } $purchasedPlanPayment->save(); }