PaymentController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Configuration;
  5. use App\Models\Payment;
  6. use App\Models\PaypalProduct;
  7. use App\Models\Product;
  8. use App\Models\User;
  9. use App\Notifications\ConfirmPaymentNotification;
  10. use Exception;
  11. use Illuminate\Contracts\Foundation\Application;
  12. use Illuminate\Contracts\View\Factory;
  13. use Illuminate\Contracts\View\View;
  14. use Illuminate\Http\JsonResponse;
  15. use Illuminate\Http\RedirectResponse;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use PayPalCheckoutSdk\Core\PayPalHttpClient;
  19. use PayPalCheckoutSdk\Core\ProductionEnvironment;
  20. use PayPalCheckoutSdk\Core\SandboxEnvironment;
  21. use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
  22. use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
  23. use PayPalHttp\HttpException;
  24. class PaymentController extends Controller
  25. {
  26. /**
  27. * @return Application|Factory|View
  28. */
  29. public function index()
  30. {
  31. return view('admin.payments.index')->with([
  32. 'payments' => Payment::paginate(15)
  33. ]);
  34. }
  35. /**
  36. * @param Request $request
  37. * @param PaypalProduct $paypalProduct
  38. * @return Application|Factory|View
  39. */
  40. public function checkOut(Request $request, PaypalProduct $paypalProduct)
  41. {
  42. return view('store.checkout')->with([
  43. 'product' => $paypalProduct
  44. ]);
  45. }
  46. /**
  47. * @param Request $request
  48. * @param PaypalProduct $paypalProduct
  49. * @return RedirectResponse
  50. */
  51. public function pay(Request $request, PaypalProduct $paypalProduct)
  52. {
  53. $request = new OrdersCreateRequest();
  54. $request->prefer('return=representation');
  55. $request->body = [
  56. "intent" => "CAPTURE",
  57. "purchase_units" => [
  58. [
  59. "reference_id" => uniqid(),
  60. "amount" => [
  61. "value" => $paypalProduct->price,
  62. "currency_code" => strtoupper($paypalProduct->currency_code)
  63. ]
  64. ]
  65. ],
  66. "application_context" => [
  67. "cancel_url" => route('payment.cancel'),
  68. "return_url" => route('payment.success', ['product' => $paypalProduct->id]),
  69. 'brand_name' => config('app.name', 'Laravel'),
  70. ]
  71. ];
  72. try {
  73. // Call API with your client and get a response for your call
  74. $response = $this->getPayPalClient()->execute($request);
  75. return redirect()->away($response->result->links[1]->href);
  76. // If call returns body in response, you can get the deserialized version from the result attribute of the response
  77. } catch (HttpException $ex) {
  78. echo $ex->statusCode;
  79. dd(json_decode($ex->getMessage()));
  80. }
  81. }
  82. /**
  83. * @return PayPalHttpClient
  84. */
  85. protected function getPayPalClient()
  86. {
  87. $environment = env('APP_ENV') == 'local'
  88. ? new SandboxEnvironment($this->getClientId(), $this->getClientSecret())
  89. : new ProductionEnvironment($this->getClientId(), $this->getClientSecret());
  90. return new PayPalHttpClient($environment);
  91. }
  92. /**
  93. * @return string
  94. */
  95. protected function getClientId()
  96. {
  97. return env('APP_ENV') == 'local' ? env('PAYPAL_SANDBOX_CLIENT_ID') : env('PAYPAL_CLIENT_ID');
  98. }
  99. /**
  100. * @return string
  101. */
  102. protected function getClientSecret()
  103. {
  104. return env('APP_ENV') == 'local' ? env('PAYPAL_SANDBOX_SECRET') : env('PAYPAL_SECRET');
  105. }
  106. /**
  107. * @param Request $laravelRequest
  108. */
  109. public function success(Request $laravelRequest)
  110. {
  111. /** @var PaypalProduct $paypalProduct */
  112. $paypalProduct = PaypalProduct::findOrFail($laravelRequest->input('product'));
  113. /** @var User $user */
  114. $user = Auth::user();
  115. $request = new OrdersCaptureRequest($laravelRequest->input('token'));
  116. $request->prefer('return=representation');
  117. try {
  118. // Call API with your client and get a response for your call
  119. $response = $this->getPayPalClient()->execute($request);
  120. if ($response->statusCode == 201 || $response->statusCode == 200) {
  121. //update credits
  122. $user->increment('credits', $paypalProduct->quantity);
  123. //update server limit
  124. if (Configuration::getValueByKey('SERVER_LIMIT_AFTER_IRL_PURCHASE', 10) !== 0) {
  125. if ($user->server_limit < Configuration::getValueByKey('SERVER_LIMIT_AFTER_IRL_PURCHASE', 10)) {
  126. $user->update(['server_limit' => 10]);
  127. }
  128. }
  129. //update role
  130. if ($user->role == 'member') {
  131. $user->update(['role' => 'client']);
  132. }
  133. //store payment
  134. $payment = Payment::create([
  135. 'user_id' => $user->id,
  136. 'payment_id' => $response->result->id,
  137. 'payer_id' => $laravelRequest->input('PayerID'),
  138. 'type' => 'Credits',
  139. 'status' => $response->result->status,
  140. 'amount' => $paypalProduct->quantity,
  141. 'price' => $paypalProduct->price,
  142. 'currency_code' => $paypalProduct->currency_code,
  143. 'payer' => json_encode($response->result->payer),
  144. ]);
  145. //payment notification
  146. $user->notify(new ConfirmPaymentNotification($payment));
  147. //redirect back to home
  148. return redirect()->route('home')->with('success', 'Credits have been increased!');
  149. }
  150. // If call returns body in response, you can get the deserialized version from the result attribute of the response
  151. if (env('APP_ENV') == 'local') {
  152. dd($response);
  153. } else {
  154. abort(500);
  155. }
  156. } catch (HttpException $ex) {
  157. if (env('APP_ENV') == 'local') {
  158. echo $ex->statusCode;
  159. dd($ex->getMessage());
  160. } else {
  161. abort(422);
  162. }
  163. }
  164. }
  165. /**
  166. * @param Request $request
  167. */
  168. public function cancel(Request $request)
  169. {
  170. return redirect()->route('store.index')->with('success', 'Payment Canceled');
  171. }
  172. /**
  173. * @return JsonResponse|mixed
  174. * @throws Exception
  175. */
  176. public function dataTable()
  177. {
  178. $query = Payment::with('user');
  179. return datatables($query)
  180. ->editColumn('user', function (Payment $payment) {
  181. return $payment->user->name;
  182. })
  183. ->editColumn('price', function (Payment $payment) {
  184. return $payment->formatCurrency();
  185. })
  186. ->editColumn('created_at', function (Payment $payment) {
  187. return $payment->created_at ? $payment->created_at->diffForHumans() : '';
  188. })
  189. ->make();
  190. }
  191. }