IyzicoController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Http\Controllers\Api\V2;
  3. use App\Models\BusinessSetting;
  4. use App\Http\Controllers\CheckoutController;
  5. use App\Http\Controllers\CustomerPackageController;
  6. use App\Http\Controllers\WalletController;
  7. use App\Models\CombinedOrder;
  8. use App\Models\User;
  9. use Illuminate\Http\Request;
  10. use Redirect;
  11. class IyzicoController extends Controller
  12. {
  13. public function init(Request $request)
  14. {
  15. $payment_type = $request->payment_type;
  16. $combined_order_id = $request->combined_order_id;
  17. $amount = $request->amount;
  18. $user_id = $request->user_id;
  19. $options = new \Iyzipay\Options();
  20. $options->setApiKey(env('IYZICO_API_KEY'));
  21. $options->setSecretKey(env('IYZICO_SECRET_KEY'));
  22. if (get_setting('iyzico_sandbox') == 1) {
  23. $options->setBaseUrl("https://sandbox-api.iyzipay.com");
  24. } else {
  25. $options->setBaseUrl("https://api.iyzipay.com");
  26. }
  27. $iyzicoRequest = new \Iyzipay\Request\CreatePayWithIyzicoInitializeRequest();
  28. $iyzicoRequest->setLocale(\Iyzipay\Model\Locale::TR);
  29. $iyzicoRequest->setConversationId('123456789');
  30. $buyer = new \Iyzipay\Model\Buyer();
  31. $buyer->setId("BY789");
  32. $buyer->setName("John");
  33. $buyer->setSurname("Doe");
  34. $buyer->setEmail("email@email.com");
  35. $buyer->setIdentityNumber("74300864791");
  36. $buyer->setRegistrationAddress("Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1");
  37. $buyer->setCity("Istanbul");
  38. $buyer->setCountry("Turkey");
  39. $iyzicoRequest->setBuyer($buyer);
  40. $shippingAddress = new \Iyzipay\Model\Address();
  41. $shippingAddress->setContactName("Jane Doe");
  42. $shippingAddress->setCity("Istanbul");
  43. $shippingAddress->setCountry("Turkey");
  44. $shippingAddress->setAddress("Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1");
  45. $iyzicoRequest->setShippingAddress($shippingAddress);
  46. $billingAddress = new \Iyzipay\Model\Address();
  47. $billingAddress->setContactName("Jane Doe");
  48. $billingAddress->setCity("Istanbul");
  49. $billingAddress->setCountry("Turkey");
  50. $billingAddress->setAddress("Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1");
  51. $iyzicoRequest->setBillingAddress($billingAddress);
  52. if ($payment_type == 'cart_payment') {
  53. $combined_order = CombinedOrder::find($combined_order_id);
  54. $iyzicoRequest->setPrice(round($combined_order->grand_total));
  55. $iyzicoRequest->setPaidPrice(round($combined_order->grand_total));
  56. $iyzicoRequest->setCurrency(\Iyzipay\Model\Currency::TL);
  57. $iyzicoRequest->setBasketId(rand(000000, 999999));
  58. $iyzicoRequest->setPaymentGroup(\Iyzipay\Model\PaymentGroup::SUBSCRIPTION);
  59. $iyzicoRequest->setCallbackUrl(route('api.iyzico.callback'));
  60. $basketItems = array();
  61. $firstBasketItem = new \Iyzipay\Model\BasketItem();
  62. $firstBasketItem->setId(rand(1000, 9999));
  63. $firstBasketItem->setName("Cart Payment");
  64. $firstBasketItem->setCategory1("Accessories");
  65. $firstBasketItem->setItemType(\Iyzipay\Model\BasketItemType::VIRTUAL);
  66. $firstBasketItem->setPrice(round($combined_order->grand_total));
  67. $basketItems[0] = $firstBasketItem;
  68. $iyzicoRequest->setBasketItems($basketItems);
  69. } elseif ($payment_type == 'wallet_payment') {
  70. $iyzicoRequest->setPrice(round($amount));
  71. $iyzicoRequest->setPaidPrice(round($amount));
  72. $iyzicoRequest->setCurrency(\Iyzipay\Model\Currency::TL);
  73. $iyzicoRequest->setBasketId(rand(000000, 999999));
  74. $iyzicoRequest->setPaymentGroup(\Iyzipay\Model\PaymentGroup::SUBSCRIPTION);
  75. $iyzicoRequest->setCallbackUrl(route('api.iyzico.callback'));
  76. $basketItems = array();
  77. $firstBasketItem = new \Iyzipay\Model\BasketItem();
  78. $firstBasketItem->setId(rand(1000, 9999));
  79. $firstBasketItem->setName("Wallet Payment");
  80. $firstBasketItem->setCategory1("Wallet");
  81. $firstBasketItem->setItemType(\Iyzipay\Model\BasketItemType::VIRTUAL);
  82. $firstBasketItem->setPrice(round($amount));
  83. $basketItems[0] = $firstBasketItem;
  84. $iyzicoRequest->setBasketItems($basketItems);
  85. }
  86. $payWithIyzicoInitialize = \Iyzipay\Model\PayWithIyzicoInitialize::create($iyzicoRequest, $options);
  87. # print result
  88. return Redirect::to($payWithIyzicoInitialize->getPayWithIyzicoPageUrl());
  89. }
  90. public function callback(Request $request)
  91. {
  92. $options = new \Iyzipay\Options();
  93. $options->setApiKey(env('IYZICO_API_KEY'));
  94. $options->setSecretKey(env('IYZICO_SECRET_KEY'));
  95. if (BusinessSetting::where('type', 'iyzico_sandbox')->first()->value == 1) {
  96. $options->setBaseUrl("https://sandbox-api.iyzipay.com");
  97. } else {
  98. $options->setBaseUrl("https://api.iyzipay.com");
  99. }
  100. $iyzicoRequest = new \Iyzipay\Request\RetrievePayWithIyzicoRequest();
  101. $iyzicoRequest->setLocale(\Iyzipay\Model\Locale::TR);
  102. $iyzicoRequest->setConversationId('123456789');
  103. $iyzicoRequest->setToken($request->token);
  104. # make request
  105. $payWithIyzico = \Iyzipay\Model\PayWithIyzico::retrieve($iyzicoRequest, $options);
  106. $payment = $payWithIyzico->getRawResult();
  107. if ($payWithIyzico->getStatus() == 'success') {
  108. return response()->json(['result' => true, 'message' => translate("Payment is successful"), 'payment_details' => $payment]);
  109. } else {
  110. return response()->json(['result' => false, 'message' => translate("Payment unsuccessful"), 'payment_details' => $payment]);
  111. }
  112. }
  113. // the callback function is in the main controller of web | paystackcontroller
  114. public function success(Request $request)
  115. {
  116. try {
  117. $payment_type = $request->payment_type;
  118. if ($payment_type == 'cart_payment') {
  119. checkout_done($request->combined_order_id, $request->payment_details);
  120. }
  121. if ($payment_type == 'wallet_payment') {
  122. wallet_payment_done($request->user_id, $request->amount, 'Paystack', $request->payment_details);
  123. }
  124. return response()->json(['result' => true, 'message' => translate("Payment is successful")]);
  125. } catch (\Exception $e) {
  126. return response()->json(['result' => false, 'message' => $e->getMessage()]);
  127. }
  128. }
  129. }