You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.7 KiB

  1. <?php
  2. namespace App\Models;
  3. use App\Models\Model;
  4. use Illuminate\Support\Arr;
  5. use App\Utilities\Zarinpal\Laravel\Facade\Zarinpal;
  6. class Transaction extends Model
  7. {
  8. protected $table = 'transactions';
  9. protected $fillable = [
  10. 'user_id', 'business_id', 'amount', 'succeeded', 'options'
  11. ];
  12. protected $casts = [
  13. 'options' => 'array',
  14. 'succeeded' => 'boolean',
  15. ];
  16. protected $fillable_relations = [
  17. 'user', 'business'
  18. ];
  19. protected $reportable = [
  20. 'user_id', 'business_id', 'amount', 'succeeded', 'options', // fields
  21. ];
  22. public $perPage = 12;
  23. public function getValueOf(?string $key)
  24. {
  25. $values = [
  26. 'business_id' => $this->business_id,
  27. 'project_id' => null,
  28. 'sprint_id' => null,
  29. 'system_id' => null,
  30. 'user_id' => $this->user_id,
  31. 'workflow_id' => null,
  32. 'status_id' => null,
  33. 'task_id' => null,
  34. 'subject_id' => $this->id,
  35. ];
  36. if ($key && isset($values, $key)) {
  37. return $values[$key];
  38. }
  39. return $values;
  40. }
  41. public function rules(): array
  42. {
  43. return [
  44. 'user_id' => 'required|',
  45. 'business_id' => 'required',
  46. 'amount' => 'required|integer|min:1',
  47. ];
  48. }
  49. public function updateRelations()
  50. {
  51. // user relations
  52. if (!empty($this->filled_relations['user'])) {
  53. $this->dirties['user'] = $this->user_id;
  54. }
  55. // business relations
  56. if (!empty($this->filled_relations['business'])) {
  57. $this->dirties['business'] = $this->business_id;
  58. }
  59. }
  60. public function reportActivity()
  61. {
  62. }
  63. public function user()
  64. {
  65. return $this->belongsTo(User::class, 'user_id','id',__FUNCTION__);
  66. }
  67. public function business()
  68. {
  69. return $this->belongsTo(Business::class, 'business_id','id',__FUNCTION__);
  70. }
  71. /**
  72. * Receive the authority key from the payment gateway
  73. */
  74. public function prepare(): Transaction
  75. {
  76. $results = Zarinpal::request(
  77. config('services.zarinpal.callback-url'),
  78. $this->amount,
  79. config('services.zarinpal.description')
  80. );
  81. $this->options = $results;
  82. $this->save();
  83. return $this;
  84. }
  85. /**
  86. * Redirect to the payment gateway
  87. */
  88. public function redirect()
  89. {
  90. return Zarinpal::redirect();
  91. }
  92. public function verify(): Transaction
  93. {
  94. $results = Zarinpal::verify($this->amount, $this->options['Authority']);
  95. if ($results['Status'] == 'verified_before') {
  96. throw new \Exception("تراکنش قبلا تایید شده است.");
  97. }
  98. if ($results['Status'] == 'success') {
  99. $this->succeeded = true;
  100. } else {
  101. $this->succeeded = false;
  102. }
  103. $options = array_merge($this->options, $results);
  104. $this->options = $options;
  105. $this->save();
  106. return $this;
  107. }
  108. /**
  109. * Find a transaction via the authoriry key that psp provides us
  110. *
  111. * @throw ModelNotFound
  112. */
  113. public static function findByAuthority(string $authority): Transaction
  114. {
  115. return static::where('options->Authority','=',$authority)->firstOrFail();
  116. }
  117. public function isWentToPaymentGateway(): bool
  118. {
  119. return !empty($this->options);
  120. }
  121. public function hasBeenAppliedToWallet(): bool
  122. {
  123. return Arr::get($this->options,"applied", false);
  124. }
  125. public function amountWasAppliedToWallet()
  126. {
  127. $options = $this->options;
  128. $options['applied'] = true;
  129. $this->options = $options;
  130. $this->save();
  131. }
  132. }