validate([ "amount" => 'required|numeric', "currency" => 'required|in:USD,EUR,TL', "frequency" => 'required|in:once,monthly,annually', "card_holder_name" => 'required|string|max:100', "card_number" => 'required|digits_between:15,16', "card_expire_month" => 'required|digits:2', "card_expire_year" => 'required|digits:2', "card_cvc" => 'required|digits_between:3,4', "first_name" => 'required|string', "last_name" => 'required|string', "show_name" => 'nullable|boolean', "email" => 'required|email', "billing_contact_name" => 'required|string', "billing_address" => 'required|string', "billing_city" => 'required|string', "billing_country" => 'required|string', "billing_zipcode" => 'nullable|string', ]); $iyzipayCurrencies = [ "USD" => \Iyzipay\Model\Currency::USD, "EUR" => \Iyzipay\Model\Currency::EUR, "TL" => \Iyzipay\Model\Currency::TL, ]; $options = new \Iyzipay\Options(); $options->setBaseUrl(config("services.iyzipay.base_url")); $options->setApiKey(config("services.iyzipay.api_key")); $options->setSecretKey(config("services.iyzipay.secret_key")); $paymentRequest = new \Iyzipay\Request\CreatePaymentRequest(); $paymentRequest->setLocale(\Iyzipay\Model\Locale::EN); $paymentRequest->setConversationId(Str::random(6)); $paymentRequest->setPrice($request->amount); $paymentRequest->setPaidPrice($request->amount); $paymentRequest->setCurrency($iyzipayCurrencies[$request->currency]); $paymentRequest->setInstallment(1); $paymentRequest->setPaymentChannel(\Iyzipay\Model\PaymentChannel::WEB); $paymentRequest->setPaymentGroup(\Iyzipay\Model\PaymentGroup::SUBSCRIPTION); $paymentCard = new \Iyzipay\Model\PaymentCard(); $paymentCard->setCardHolderName($request->card_holder_name); $paymentCard->setCardNumber($request->card_number); $paymentCard->setExpireMonth($request->card_expire_month); $paymentCard->setExpireYear("20" . $request->card_expire_year); $paymentCard->setCvc($request->card_cvc); if ($request->frequency !== "once") { $paymentCard->setRegisterCard(1); } $paymentRequest->setPaymentCard($paymentCard); $buyer = new \Iyzipay\Model\Buyer(); $buyer->setId(md5($request->email)); $buyer->setName($request->first_name); $buyer->setSurname($request->last_name); $buyer->setEmail($request->email); $buyer->setIdentityNumber("11111111111"); $buyer->setRegistrationAddress($request->billing_address); $buyer->setIp($request->ip()); $buyer->setCity($request->billing_city); $buyer->setCountry($request->billing_country); if ($request->billing_zipcode) { $buyer->setZipCode($request->billing_zipcode); } $paymentRequest->setBuyer($buyer); $billingAddress = new \Iyzipay\Model\Address(); $billingAddress->setContactName($request->billing_contact_name); $billingAddress->setAddress($request->billing_address); $billingAddress->setCity($request->billing_city); $billingAddress->setCountry($request->billing_country); if ($request->billing_zipcode) { $billingAddress->setZipCode($request->billing_zipcode); } $paymentRequest->setBillingAddress($billingAddress); $basketItems = array(); $basketItem = new \Iyzipay\Model\BasketItem(); $basketItem->setId("donation-" . $request->frequency); $basketItem->setName("Donation - " . $request->frequency); $basketItem->setCategory1("Donation"); $basketItem->setCategory2($request->frequency); $basketItem->setItemType(\Iyzipay\Model\BasketItemType::VIRTUAL); $basketItem->setPrice($request->amount); $basketItems[] = $basketItem; $paymentRequest->setBasketItems($basketItems); $payment = \Iyzipay\Model\Payment::create($paymentRequest, $options); // if payment is not success if ($payment->getStatus() !== "success") { return redirect()->back()->withErrors([ 'payment_error' => "Payment Error: " . $payment->getErrorMessage() . " Code: " . $payment->getErrorCode(), ])->withInput(); } // if payment succedded $user = User::where('email', $request->email)->first(); if (!$user) { $user = new User; $user->name = $request->first_name . " " . $request->last_name; $user->email = $request->email; $randomPassword = Str::random(8); $user->password = Hash::make($randomPassword); $user->save(); Mail::to($user)->send(new UserCreated($user,$randomPassword)); } $donation = new Donation; $donation->user_id = $user->id; $donation->conversation_id = $payment->getConversationId(); $donation->first_name = $request->first_name; $donation->last_name = $request->last_name; if ($request->show_name) { $donation->show_name_on_list = $request->show_name; } $donation->email = $request->email; $donation->amount = $request->amount; $donation->currency = $request->currency; $donation->frequency = $request->frequency; $donation->billing_info = json_encode([ "contact_name" => $request->billing_contact_name, "address" => $request->billing_address, "city" => $request->billing_city, "country" => $request->billing_country, "zipcode" => $request->billing_zipcode, ]); $donation->payment_result = $payment->getRawResult(); if ($request->frequency != "once" && $payment->getCardToken() && $payment->getCardUserKey()) { $donation->card_token = $payment->getCardToken(); $donation->card_user_key = $payment->getCardUserKey(); $donation->card_cvc = $request->card_cvc; if ($request->frequency == "monthly") { $donation->next_payment_at = now()->addMonth()->format("Y-m-d"); } if ($request->frequency == "annually") { $donation->next_payment_at = now()->addYear()->format("Y-m-d"); } } $donation->save(); Mail::to($donation)->send(new SuccessfulPayment($donation)); return redirect()->route('donation.success', $donation->conversation_id); } public function success($conversation_id, Request $request) { $donation = Donation::where('conversation_id', $conversation_id)->firstOrFail(); return view('success', compact('donation')); } public function all(Request $request) { $donations = $request->user()->donations()->latest()->paginate(); return view('donations', compact('donations')); } }