Merhabalar, iyzico için 3d ödeme işlemini başlatmak istiyorum servisim var ama servis de nasıl bir düzenleme yapacağımı kafam da oturtamadım bir türlü belki de yorgunluktan emin değilim 3d sayfasına yönlendirmek oradan dönen sonuca göre de işlem yapmak istiyorum.
Kodlarımı paylaşıyorum
Tekrardan aynı paymentRequest controllerına geri gelmesini istiyorum ama nasıl yapacağımı bulamadım açıkcası.
İşlemi başlattığım controller
public function paymentRequest(Request $request)
{
$this->validate($request, [
'gateway' => 'required'
]);
$user = auth()->user();
$gateway = $request->input('gateway');
$orderId = $request->input('order_id');
$order = Order::where('id', $orderId)
->where('user_id', $user->id)
->first();
if ($order->type === Order::$meeting) {
$orderItem = OrderItem::where('order_id', $order->id)->first();
$reserveMeeting = ReserveMeeting::where('id', $orderItem->reserve_meeting_id)->first();
$reserveMeeting->update(['locked_at' => time()]);
}
// İyzico ödemesi için özel kontrol
if ($gateway === 'credit_card') {
try {
// Ödeme verilerini hazırlama
$dataRequest = [
'package_name' => $order->package_name ?? 'Default Package',
'price' => $order->total_amount,
'card_holder_name' => $request->input('card_holder_name'),
'card_number' => $request->input('card_number'),
'expire_month' => $request->input('expire_month'),
'expire_year' => $request->input('expire_year'),
'cvc' => $request->input('cvc'),
'name' => $user->name ?? 'Default Name',
'phone' => $user->info->phone ?? '5551234567',
'email' => $user->email ?? 'default@email.com',
'city' => $user->info->city ?? 'Istanbul',
'country' => $user->info->country ?? 'Turkey',
'address' => $user->info->address ?? 'Default Address',
'zipcode' => $user->info->zipcode ?? '34000',
'period' => 'once'
];
// İyzico servisini başlatma
$pay = new IyzipayService(new Options());
$payment = $pay->buyingPackage($dataRequest);
if ($payment['status'] === 'success') {
$order->update([
'status' => Order::$paid,
'payment_method' => 'payment_channel',
]);
foreach ($order->orderItems as $orderItem) {
$sale = Sale::createSales($orderItem, $order->payment_method);
Invoice::create([
'sale_id' => $sale->id,
'webinar_id' => $sale->webinar_id,
'user_id' => Auth::id(),
'invoice_number' => 'INV-' . strtoupper(uniqid()),
'type' => 'webinar',
'payment_method' => $sale->payment_method,
'amount' => $sale->amount,
'tax' => $sale->tax,
'discount' => $sale->discount,
'total_amount' => $sale->total_amount,
'status' => 'pending',
'pdf_path' => null, // PDF path will be updated later
]);
if (!empty($orderItem->product_id)) {
$this->updateProductOrder($sale, $orderItem);
}
}
Cart::emptyCart($order->user_id);
session()->put($this->order_session_key, $order->id);
return redirect('/payments/status');
} else {
$order->update(['status' => Order::$fail]);
$toastData = [
'title' => trans('cart.fail_purchase'),
'msg' => $payment['message'] ?? trans('cart.gateway_error'),
'status' => 'error'
];
return back()->with(['toast' => $toastData]);
}
} catch (\Exception $exception) {
\Log::error('Iyzico Payment Error: ' . $exception->getMessage());
$toastData = [
'title' => trans('cart.fail_purchase'),
'msg' => trans('cart.gateway_error'),
'status' => 'error'
];
return back()->with(['toast' => $toastData]);
}
}
$paymentChannel = PaymentChannel::where('id', $gateway)
->where('status', 'active')
->first();
if (!$paymentChannel) {
$toastData = [
'title' => trans('cart.fail_purchase'),
'msg' => trans('public.channel_payment_disabled'),
'status' => 'error'
];
return back()->with(['toast' => $toastData]);
}
$order->payment_method = Order::$paymentChannel;
$order->save();
try {
$channelManager = ChannelManager::makeChannel($paymentChannel);
$redirect_url = $channelManager->paymentRequest($order);
if (in_array($paymentChannel->class_name, PaymentChannel::$gatewayIgnoreRedirect)) {
return $redirect_url;
}
return Redirect::away($redirect_url);
} catch (\Exception $exception) {
$toastData = [
'title' => trans('cart.fail_purchase'),
'msg' => trans('cart.gateway_error'),
'status' => 'error'
];
return back()->with(['toast' => $toastData]);
}
}
İyzipay Servisim
<?php
namespace App\Services;
use Carbon\Carbon;
use Iyzipay\Model\Payment;
use Iyzipay\Options;
use Iyzipay\Request\CreatePaymentRequest;
use Iyzipay\Model\Payment as IyzipayPayment;
use Illuminate\Http\JsonResponse;
use Iyzipay\Model\Subscription\SubscriptionCreate;
use Iyzipay\Model\Subscription\SubscriptionPricingPlan;
use Iyzipay\Model\Subscription\SubscriptionProduct;
use Iyzipay\Request\Subscription\SubscriptionCreatePricingPlanRequest;
use Iyzipay\Request\Subscription\SubscriptionCreateProductRequest;
use Iyzipay\Request\Subscription\SubscriptionDeletePricingPlanRequest;
use Iyzipay\Request\Subscription\SubscriptionDeleteProductRequest;
use Iyzipay\Request\Subscription\SubscriptionUpdatePricingPlanRequest;
use Iyzipay\Request\Subscription\SubscriptionUpdateProductRequest;
use function Webmozart\Assert\Tests\StaticAnalysis\true;
class IyzipayService
{
public $options;
public function __construct($options)
{
$this->options = new Options();
$this->options->setApiKey(config('iyzipay.api_key'));
$this->options->setSecretKey(config('iyzipay.secret_key'));
$this->options->setBaseUrl(config('iyzipay.base_url'));
}
public function buyingPackage($data) {
$request = new \Iyzipay\Request\CreatePaymentRequest();
$request->setLocale(\Iyzipay\Model\Locale::TR);
$request->setConversationId("123456789");
$request->setPrice($data['price']);
$request->setPaidPrice($data['price']);
$request->setCurrency(\Iyzipay\Model\Currency::TL);
$request->setInstallment(1);
$request->setBasketId("B67832");
$request->setPaymentChannel(\Iyzipay\Model\PaymentChannel::WEB);
$request->setPaymentGroup(\Iyzipay\Model\PaymentGroup::PRODUCT);
// Kart bilgileri ile ödeme için PaymentCard nesnesini güncelliyoruz
$paymentCard = new \Iyzipay\Model\PaymentCard();
$paymentCard->setCardHolderName($data['card_holder_name']); // Kart sahibinin adı
$paymentCard->setCardNumber($data['card_number']); // Kart numarası
$paymentCard->setExpireMonth($data['expire_month']); // Son kullanma ayı
$paymentCard->setExpireYear($data['expire_year']); // Son kullanma yılı
$paymentCard->setCvc($data['cvc']); // Güvenlik kodu
$paymentCard->setRegisterCard(0); // Kartı kaydetme seçeneği
$request->setPaymentCard($paymentCard);
$buyer = new \Iyzipay\Model\Buyer();
$buyer->setId("BY789");
$buyer->setName($data['name']);
$buyer->setSurname($data['name']);
$buyer->setGsmNumber($data['phone']);
$buyer->setEmail($data['email']);
$buyer->setIdentityNumber("74300864791");
$buyer->setLastLoginDate("2015-10-05 12:43:35");
$buyer->setRegistrationDate("2013-04-21 15:12:09");
$buyer->setRegistrationAddress($data['address']);
$buyer->setIp("85.34.78.112");
$buyer->setCity($data['city']);
$buyer->setCountry($data['country']);
$buyer->setZipCode($data['zipcode']);
$request->setBuyer($buyer);
$shippingAddress = new \Iyzipay\Model\Address();
$shippingAddress->setContactName($data['name']);
$shippingAddress->setCity($data['city']);
$shippingAddress->setCountry($data['country']);
$shippingAddress->setAddress($data['address']);
$shippingAddress->setZipCode($data['zipcode']);
$request->setShippingAddress($shippingAddress);
$billingAddress = new \Iyzipay\Model\Address();
$billingAddress->setContactName($data['name']);
$billingAddress->setCity($data['city']);
$billingAddress->setCountry($data['country']);
$billingAddress->setAddress($data['address']);
$billingAddress->setZipCode($data['zipcode']);
$request->setBillingAddress($billingAddress);
$basketItems = array();
$firstBasketItem = new \Iyzipay\Model\BasketItem();
$firstBasketItem->setId("BI101");
$firstBasketItem->setName($data['package_name']);
$firstBasketItem->setCategory1("Fitness");
$firstBasketItem->setItemType(\Iyzipay\Model\BasketItemType::PHYSICAL);
$firstBasketItem->setPrice($data['price']);
$basketItems[0] = $firstBasketItem;
$request->setBasketItems($basketItems);
try {
# make request
$payment = Payment::create($request, $this->options);
if ($payment->getStatus() == 'success') {
return [
'status' => $payment->getStatus(),
'payment_id' => $payment->getPaymentId(),
];
} else {
return [
'status' => 'error',
'message' => $payment->getErrorMessage()
];
}
} catch (\Exception $e) {
return [
'status' => 'error',
'message' => $e->getMessage()
];
}
}
}