/** * Managing authorize.net silent post * @author Dmitry Semenov <*****@*****.**> */ public function run() { $postData = $_POST; Yii::log(CJSON::encode($postData), 'trace'); if (empty($postData) || empty($postData['x_trans_id']) || empty($postData['x_amount']) || empty($postData['x_response_code']) || empty($postData['x_response_reason_text'])) { Yii::log("Incorrect POST provided", 'error'); exit; } try { if (Yii::app()->params['payment']['testMode'] == true) { $hash = md5(Yii::app()->params['payment']['testData']['md5Word'] . $postData['x_trans_id'] . $postData['x_amount']); } else { $hash = md5(Yii::app()->params['payment']['liveData']['md5Word'] . $postData['x_trans_id'] . $postData['x_amount']); } } catch (Exception $e) { Yii::log($e->getMessage(), 'error'); Yii::app()->end(); } try { if (strtolower($hash) == strtolower($postData['x_MD5_Hash'])) { $payment = PaymentModel::model()->findByAttributes(array('subscriptionID' => $postData['x_subscription_id'])); if (empty($payment)) { Yii::log("No subscription #" . $postData["x_subscription_id"] . " in DB\n", 'error'); Yii::app()->end(); } else { $valid = true; } } else { Yii::log("Incorrect hash provided.", 'error'); Yii::app()->end(); } } catch (Exception $e) { Yii::log($e->getMessage(), 'error'); exit; } // If it is an ARB transaction, do something with it if ($valid) { try { if ($postData['x_response_code'] == 1) { // --- Approved transactions management --- // if ($postData['x_subscription_paynum'] == 1) { Yii::log("Subscription OK", 'trace'); Yii::app()->end(); } $membershipPlan = MembershipPlanModel::model()->findByPk($payment->relatedUser->membership->membershipPlanID); $paymentData = array('status' => PaymentModel::STATUS_COMPLETED, 'message' => $postData['x_response_reason_text'], 'initID' => $payment->id); PaymentModel::createPayment($payment->relatedUser, PaymentModel::TYPE_OEM_RECURRING, $membershipPlan, $payment->relatedUser->membership, null, $paymentData); UserMembershipModel::extendMembership($payment->userMembershipID); Yii::app()->membership->prolong($plan->id, $plan->intervalLength, $plan->intervalUnit, $userId); } else { // Denied transactions //--- oem_recurring (failed) ---// $membershipPlan = MembershipPlanModel::model()->findByPk($payment->relatedUser->membership->membershipPlanID); $paymentData = array('status' => PaymentModel::STATUS_REFUSED, 'message' => $postData['x_response_reason_text'], 'initID' => $payment->id); PaymentModel::createPayment($payment->relatedUser, PaymentModel::TYPE_OEM_RECURRING, $membershipPlan, $payment->relatedUser->membership, null, $paymentData); Yii::app()->membership->prolong($plan->id, $plan->intervalLength, $plan->intervalUnit, $userId); } } catch (Exception $e) { Yii::log($e->getMessage(), 'error'); Yii::app()->end(); } } Yii::app()->end(); }
/** * Calculate discount amount for Order * * @author Dmitry Semenov <*****@*****.**> * * @param float * @param array * * @return float */ public function getDiscount($code, $plan = null) { $message = ''; $discount = 0; if (empty($code)) { return array('discount' => $discount, 'message' => 'No promotional code provided.'); } $plan = \MembershipPlanModel::model()->findByPk($plan); $plan = \MembershipPlanModel::recalculateSalesPrices(array($plan)); $plan = $plan[0]; if (!$plan) { return array('success' => false, 'message' => 'Billing plan not found.'); } $criteria = new \CDbCriteria(); $criteria->addColumnCondition(array('t.code' => $code)); $coupon = self::model()->find($criteria); if (!$coupon) { return array('success' => false, 'message' => 'Coupon not found.'); } if (!$coupon->canUse($plan->id)) { return array('success' => false, 'message' => 'Can`t use this coupon with selected billing plan.'); } if (!$coupon->isInTime()) { return array('success' => false, 'message' => 'This coupon is out of date.'); } switch ($coupon->discountType) { case PromoCodeType::DISCOUNT_TYPE_DOLLAR_VALUE: return array('success' => true, 'message' => "Your discount is \$" . $coupon->discountValue . " and total cost is \$" . round($coupon->discountValue < $plan->price ? $plan->price - $coupon->discountValue : 0, 2), 'discountedPrice' => round($coupon->discountValue < $plan->price ? $plan->price - $coupon->discountValue : 0, 2)); break; case PromoCodeType::DISCOUNT_TYPE_PERCENTAGE: $discountedPrice = $plan->price - $coupon->discountValue / 100 * $plan->price; if ($coupon->discountValue >= 100) { $discountedPrice = 0; } return array('success' => true, 'message' => "Your discount is " . $coupon->discountValue . "% and total cost is \$" . round($discountedPrice, 2), 'discountedPrice' => round($discountedPrice, 2)); break; } }