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.

69 lines
1.9 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Transaction;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Auth;
  6. use Spatie\QueryBuilder\QueryBuilder;
  7. use Spatie\QueryBuilder\AllowedFilter;
  8. use App\Http\Resources\TransactionResource;
  9. class CreditController extends Controller
  10. {
  11. public function payments(Request $request, int $business)
  12. {
  13. \permit('isBusinessOwner');
  14. $query = Transaction::where('business_id', $business);
  15. $builder = QueryBuilder::for($query)
  16. ->allowedSorts([
  17. 'amount',
  18. 'succeeded',
  19. 'created_at',
  20. ])
  21. ->allowedFilters([
  22. AllowedFilter::exact('user_id'),
  23. AllowedFilter::exact('succeeded'),
  24. ]);
  25. return TransactionResource::collection(
  26. $builder->paginate($request->per_page)
  27. );
  28. }
  29. public function pay(Request $request, int $business)
  30. {
  31. \permit('isBusinessOwner');
  32. return Transaction::create([
  33. 'user_id'=> Auth::id(),
  34. 'business_id'=> $business,
  35. 'amount'=> $request->amount,
  36. ]);
  37. }
  38. public function redirection($transaction)
  39. {
  40. $transaction = Transaction::findOrFail($transaction);
  41. if ($transaction->isWentToPaymentGateway()) {
  42. throw new \Exception("Siktir baba ye bar ghablan rafti.");
  43. }
  44. return $transaction->prepare()->redirect();
  45. }
  46. public function callback(Request $request)
  47. {
  48. $transaction = Transaction::findByAuthority($request->get('Authority'))->verify();
  49. if (!$transaction->hasBeenAppliedToWallet() && $transaction->succeeded) {
  50. $transaction->business->increment("wallet", $transaction->amount);
  51. $transaction->amountWasAppliedToWallet();
  52. return true;
  53. }
  54. throw new \Exception("تراکنش تایید نشد");
  55. }
  56. }