Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace api\modules\v1\events;
- use common\models\BusinessProfile;
- use Yii;
- /**
- *
- *
- */
- class TransactionEvents
- {
- /**
- * Holds the instance of the transaction
- *
- * @var mixed
- */
- private static $_transaction;
- /**
- * Hold the subscription instance
- *
- * @var mixed
- */
- private static $_subscription;
- /**
- * Holds the message object
- *
- * @var mixed
- */
- private static $_messages;
- /**
- * @var mixed
- */
- private static $_order;
- /**
- * Handles the after Transaction processes like email notifications
- * for success and failure
- *
- * @param \api\modules\v1\models\events\AnetTransaction $event Anettransaction event object
- *
- * @return null
- */
- public function handleAfterTransaction($event)
- {
- $anetTransaction = $event->getAnetTransaction();
- self::$_transaction = $anetTransaction->getTransaction();
- self::$_messages = $anetTransaction->getMessages();
- self::$_subscription = self::$_transaction->getSubscription();
- self::$_order = self::$_transaction->getOrder();
- if (self::$_messages->getResultCode() == 'Ok') {
- $model = self::getBusinessProfile();
- $notificationType = 'success';
- } else {
- $notificationType = 'fail';
- }
- self::_sendNotificationEmail($notificationType);
- }
- /**
- * Send Email notifications
- *
- * @param string $notificationType typ of notification to be sent
- *
- * @return null
- */
- private static function _sendNotificationEmail($notificationType)
- {
- $profile = self::getBusinessProfile();
- $user = $profile->user;
- $subscriptionId = self::$_subscription->getId();
- $transaction = self::$_transaction;
- $order = self::$_order;
- $billTo = $transaction->getBillTo();
- if ($notificationType == 'success') {
- $subject = "Transaction against the Subscription : $subscriptionId completed successfully";
- } else {
- $subject = "Transaction against the Subscription : $subscriptionId failed";
- }
- $data = [
- 'transaction_id' => $transaction->getTransId(),
- 'invoice_number' => $order->getInvoiceNumber(),
- 'invoice_description' => $order->getDescription(),
- 'amount' => $transaction->getAuthAmount(),
- 'submit_time_utc' => $transaction->getSubmitTimeUTC(),
- 'submit_time_local' => $transaction->getSubmitTimeLocal(),
- 'status' => $transaction->getTransactionStatus(),
- 'comments' => $transaction->getResponseReasonDescription(),
- 'billing_info' => [
- 'full_name' => $billTo->getFirstName() . ' ' . $billTo->getLastName(),
- 'address' => $billTo->getAddress(),
- 'city' => $billTo->getCity(),
- 'state' => $billTo->getState(),
- 'zip' => $billTo->getZip(),
- ],
- 'user' => [
- 'email' => $user->email,
- ],
- ];
- //send email to user
- $user->sendEmail($subject, 'transaction-completed', $data);
- $data['user']['email'] = Yii::$app->params['adminEmail'];
- //send email to admin
- $user->sendEmail($subject, 'transaction-completed', $data, Yii::$app->params['adminEmail']);
- }
- /**
- * Loads the BusinessProfile for the given subscription ID
- *
- * @return common\models\BusinessProfile
- */
- protected static function getBusinessProfile()
- {
- $model = BusinessProfile::find()->where(
- ['=', 'subscription_id', self::$_subscription->getId()]
- )->one();
- return $model;
- }
- }
Add Comment
Please, Sign In to add comment