PaykuController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Http\Controllers\Payment;
  3. use App\Models\CombinedOrder;
  4. use App\Models\CustomerPackage;
  5. use App\Models\SellerPackage;
  6. use App\Http\Controllers\CheckoutController;
  7. use App\Http\Controllers\CustomerPackageController;
  8. use App\Http\Controllers\SellerPackageController;
  9. use App\Http\Controllers\WalletController;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Route;
  12. use SebaCarrasco93\LaravelPayku\Facades\LaravelPayku;
  13. use SebaCarrasco93\LaravelPayku\Models\PaykuTransaction;
  14. use Session;
  15. use Auth;
  16. class PaykuController
  17. {
  18. public function pay(Request $request)
  19. {
  20. if($request->session()->has('payment_type')){
  21. if($request->session()->get('payment_type') == 'cart_payment'){
  22. $combined_order = CombinedOrder::findOrFail(Session::get('combined_order_id'));
  23. $data = [
  24. 'order' => rand(0000000,11111111).date('is'),
  25. 'subject' => 'Cart Payment',
  26. 'amount' => $combined_order->grand_total,
  27. 'email' => Auth::user()->email
  28. ];
  29. }
  30. elseif ($request->session()->get('payment_type') == 'wallet_payment') {
  31. $data = [
  32. 'order' => rand(0000000,11111111).date('is'),
  33. 'subject' => 'Wallet Payment',
  34. 'amount' => $request->session()->get('payment_data')['amount'],
  35. 'email' => Auth::user()->email
  36. ];
  37. }
  38. elseif ($request->session()->get('payment_type') == 'customer_package_payment') {
  39. $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
  40. $data = [
  41. 'order' => rand(0000000,11111111).date('is'),
  42. 'subject' => 'CustomerPackage Payment',
  43. 'amount' => $customer_package->amount,
  44. 'email' => Auth::user()->email
  45. ];
  46. }
  47. elseif ($request->session()->get('payment_type') == 'seller_package_payment') {
  48. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  49. $data = [
  50. 'order' => rand(0000000,11111111).date('is'),
  51. 'subject' => 'SellerPackage Payment',
  52. 'amount' => $seller_package->amount,
  53. 'email' => Auth::user()->email
  54. ];
  55. }
  56. }
  57. return LaravelPayku::create($data['order'], $data['subject'], $data['amount'], $data['email']);
  58. }
  59. public function return($order)
  60. {
  61. $detail = LaravelPayku::return($order);
  62. return $detail;
  63. }
  64. public function notify($order)
  65. {
  66. $result = LaravelPayku::notify($order);
  67. $routeName = config('laravel-payku.route_finish_name');
  68. $routeExists = Route::has($routeName);
  69. if ($routeExists) {
  70. return redirect()->route($routeName, $result);
  71. }
  72. return view('payku::notify.missing-route', compact('result', 'routeName'));
  73. }
  74. public function callback($id){
  75. $paykuTransaction = PaykuTransaction::find($id);
  76. if($paykuTransaction->status == 'success'){
  77. $payment_type = Session::get('payment_type');
  78. if ($payment_type == 'cart_payment') {
  79. return (new CheckoutController)->checkout_done(session()->get('combined_order_id'), $paykuTransaction->toJson());
  80. }
  81. if ($payment_type == 'wallet_payment') {
  82. return (new WalletController)->wallet_payment_done(session()->get('payment_data'), $paykuTransaction->toJson());
  83. }
  84. if ($payment_type == 'customer_package_payment') {
  85. return (new CustomerPackageController)->purchase_payment_done(session()->get('payment_data'), $paykuTransaction->toJson());
  86. }
  87. if($payment_type == 'seller_package_payment') {
  88. return (new SellerPackageController)->purchase_payment_done(session()->get('payment_data'), $paykuTransaction->toJson());
  89. }
  90. }
  91. else{
  92. flash(translate('Payment failed'))->error();
  93. return redirect()->route('home');
  94. }
  95. }
  96. }