Guest User

Untitled

a guest
Dec 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. <?php
  2. namespace api\modules\v1\events;
  3.  
  4. use common\models\BusinessProfile;
  5. use Yii;
  6.  
  7. /**
  8. *
  9. *
  10. */
  11. class TransactionEvents
  12. {
  13.  
  14. /**
  15. * Holds the instance of the transaction
  16. *
  17. * @var mixed
  18. */
  19. private static $_transaction;
  20.  
  21. /**
  22. * Hold the subscription instance
  23. *
  24. * @var mixed
  25. */
  26. private static $_subscription;
  27.  
  28. /**
  29. * Holds the message object
  30. *
  31. * @var mixed
  32. */
  33. private static $_messages;
  34.  
  35. /**
  36. * @var mixed
  37. */
  38. private static $_order;
  39.  
  40. /**
  41. * Handles the after Transaction processes like email notifications
  42. * for success and failure
  43. *
  44. * @param \api\modules\v1\models\events\AnetTransaction $event Anettransaction event object
  45. *
  46. * @return null
  47. */
  48. public function handleAfterTransaction($event)
  49. {
  50.  
  51. $anetTransaction = $event->getAnetTransaction();
  52.  
  53. self::$_transaction = $anetTransaction->getTransaction();
  54. self::$_messages = $anetTransaction->getMessages();
  55. self::$_subscription = self::$_transaction->getSubscription();
  56. self::$_order = self::$_transaction->getOrder();
  57.  
  58. if (self::$_messages->getResultCode() == 'Ok') {
  59. $model = self::getBusinessProfile();
  60. $notificationType = 'success';
  61. } else {
  62. $notificationType = 'fail';
  63. }
  64.  
  65. self::_sendNotificationEmail($notificationType);
  66. }
  67.  
  68. /**
  69. * Send Email notifications
  70. *
  71. * @param string $notificationType typ of notification to be sent
  72. *
  73. * @return null
  74. */
  75. private static function _sendNotificationEmail($notificationType)
  76. {
  77. $profile = self::getBusinessProfile();
  78. $user = $profile->user;
  79. $subscriptionId = self::$_subscription->getId();
  80. $transaction = self::$_transaction;
  81. $order = self::$_order;
  82. $billTo = $transaction->getBillTo();
  83.  
  84. if ($notificationType == 'success') {
  85. $subject = "Transaction against the Subscription : $subscriptionId completed successfully";
  86. } else {
  87. $subject = "Transaction against the Subscription : $subscriptionId failed";
  88. }
  89.  
  90. $data = [
  91. 'transaction_id' => $transaction->getTransId(),
  92. 'invoice_number' => $order->getInvoiceNumber(),
  93. 'invoice_description' => $order->getDescription(),
  94. 'amount' => $transaction->getAuthAmount(),
  95. 'submit_time_utc' => $transaction->getSubmitTimeUTC(),
  96. 'submit_time_local' => $transaction->getSubmitTimeLocal(),
  97. 'status' => $transaction->getTransactionStatus(),
  98. 'comments' => $transaction->getResponseReasonDescription(),
  99. 'billing_info' => [
  100. 'full_name' => $billTo->getFirstName() . ' ' . $billTo->getLastName(),
  101. 'address' => $billTo->getAddress(),
  102. 'city' => $billTo->getCity(),
  103. 'state' => $billTo->getState(),
  104. 'zip' => $billTo->getZip(),
  105. ],
  106. 'user' => [
  107. 'email' => $user->email,
  108. ],
  109. ];
  110.  
  111. //send email to user
  112. $user->sendEmail($subject, 'transaction-completed', $data);
  113. $data['user']['email'] = Yii::$app->params['adminEmail'];
  114.  
  115. //send email to admin
  116. $user->sendEmail($subject, 'transaction-completed', $data, Yii::$app->params['adminEmail']);
  117. }
  118.  
  119. /**
  120. * Loads the BusinessProfile for the given subscription ID
  121. *
  122. * @return common\models\BusinessProfile
  123. */
  124. protected static function getBusinessProfile()
  125. {
  126. $model = BusinessProfile::find()->where(
  127. ['=', 'subscription_id', self::$_subscription->getId()]
  128. )->one();
  129.  
  130. return $model;
  131. }
  132.  
  133. }
Add Comment
Please, Sign In to add comment