Laravel ve İyzico ile e-Ticaret Ödeme

Makale İçerik Listesi

  1. İyzico Sandbox'tan giriş yap - link

İyzico Ödeme Çıktısı

  1. Ödeme çıktısı şu şekildedir:(BAŞARILI)
    {
      "status": "success",
      "locale": "tr",
      "systemTime": 1664928813907,
      "price": 1271.186440678,
      "paidPrice": 1500,
      "installment": 1,
      "paymentId": "18319995",
      "fraudStatus": 1,
      "merchantCommissionRate": 18,
      "merchantCommissionRateAmount": 228.813559322,
      "iyziCommissionRateAmount": 60,
      "iyziCommissionFee": 0.25,
      "cardType": "DEBIT_CARD",
      "cardAssociation": "MASTER_CARD",
      "cardFamily": "Neo",
      "binNumber": "589004",
      "lastFourDigits": "0016",
      "currency": "TRY",
      "itemTransactions": [
        {
          "itemId": "1",
          "paymentTransactionId": "19527423",
          "transactionStatus": 2,
          "price": 1271.186440678,
          "paidPrice": 1499.999999998,
          "merchantCommissionRate": 18,
          "merchantCommissionRateAmount": 228.81355932,
          "iyziCommissionRateAmount": 60,
          "iyziCommissionFee": 0.25,
          "blockageRate": 0,
          "blockageRateAmountMerchant": 0,
          "blockageRateAmountSubMerchant": 0,
          "blockageResolvedDate": "2022-10-13 00:00:00",
          "subMerchantPrice": 0,
          "subMerchantPayoutRate": 0,
          "subMerchantPayoutAmount": 0,
          "merchantPayoutAmount": 1439.749999998,
          "convertedPayout": {
            "paidPrice": 1500,
            "iyziCommissionRateAmount": 60,
            "iyziCommissionFee": 0.25,
            "blockageRateAmountMerchant": 0,
            "blockageRateAmountSubMerchant": 0,
            "subMerchantPayoutAmount": 0,
            "merchantPayoutAmount": 1439.75,
            "iyziConversionRate": 0,
            "iyziConversionRateAmount": 0,
            "currency": "TRY"
          }
        }
      ],
      "authCode": "079519",
      "phase": "AUTH",
      "hostReference": "mock00027iyzihostrfn"
    }

İyzico Ödeme Formu

  1. $options = new \Iyzipay\Options();
    $options->setApiKey(env('APIKEY'));
    $options->setSecretKey(env('SECRETKEY'));
    $options->setBaseUrl(env('URL'));
    
    $iyzico_request = new \Iyzipay\Request\CreateCheckoutFormInitializeRequest();
    $iyzico_request->setConversationId($payment->id);
    $iyzico_request->setPrice($payment->amount / 1.18);
    $iyzico_request->setPaidPrice($payment->amount);
    $iyzico_request->setCurrency(\Iyzipay\Model\Currency::TL);
    $iyzico_request->setCallbackUrl(env('IYZICO_CALLBACK') . '?order=' . $payment->id);
    $iyzico_request->setEnabledInstallments([2, 3, 6, 9]);
    
    $buyer = new \Iyzipay\Model\Buyer();
    $buyer->setId($buyer->id);
    $buyer->setName($buyer->name);
    $buyer->setSurname($buyer->surname);
    $buyer->setIdentityNumber($buyer->id);
    $buyer->setCity($buyer->city);
    $buyer->setCountry($buyer->country);
    $buyer->setEmail($buyer->email);
    $buyer->setIp($_SERVER['REMOTE_ADDR']);
    $buyer->setRegistrationAddress($buyer->address);
    $iyzico_request->setBuyer($buyer);
    
    $billingAddress = new \Iyzipay\Model\Address();
    $billingAddress->setContactName($buyer->name);
    $billingAddress->setCity($buyer->city);
    $billingAddress->setCountry($buyer->country);
    $billingAddress->setAddress($buyer->address);
    $iyzico_request->setBillingAddress($billingAddress);
    
    $basketItems = [];
    $firstBasketItem = new \Iyzipay\Model\BasketItem();
    $firstBasketItem->setId('B67832');
    $firstBasketItem->setName("Name of Item");
    $firstBasketItem->setCategory1("Category of Product Name");
    $firstBasketItem->setItemType(\Iyzipay\Model\BasketItemType::VIRTUAL);
    $firstBasketItem->setPrice($payment->amount / 1.18);
    $basketItems[0] = $firstBasketItem;
    $iyzico_request->setBasketItems($basketItems);
    
    $checkoutFormInitialize = \Iyzipay\Model\CheckoutFormInitialize::create($iyzico_request, $options);
    
    

Kredi Kartını Formda Stilli olarak göstermek

  1. Öncelikle basit bir HTML Formunu kredi kartı bilgisini rahat alacak şekilde formatlamak istersek şu şekilde autocomplete ayarlarını yapabiliriz. 
    <form>
      <label for="cc-num">Credit Card Number</label>
      <input type="text" id="cc-num" name="cc-num" autocomplete="cc-number" x-autocompletetype="cc-number" pattern="\d*">
      
      <label for="cc-exp-month">Expiration Month</label>
      <input type="number" id="cc-exp-month" name="cc-exp-month" autocomplete="cc-exp-month" x-autocompletetype="cc-exp-month">
      
      <label for="cc-exp-year">Expiration Year</label>
      <input type="number" id="cc-exp-year" name="cc-exp-year" autocomplete="cc-exp-year" x-autocompletetype="cc-exp-year">
      
      <label for="cvv2">CVV</label>
      <input type="text" id="cvv2" name="cvv2" autocomplete="cc-csc" max="9999">
      
    </form>
  2. Eğer kredi kartın numara alanını XXXX XXXX XXXX XXXX formatında yazmak istersek o zaman bu formda da bazı değişiklikler yaparak ilerlememiz gerekir. 1) Öncelikle cc-number alanında type olarak number yazmadığımızdan emin olalım. 2) pattern özelliğini kaldıralım.
  3. Sonra bir formatlama fonksiyonu yazmamız gerekecek 
    function cc_format(value) {
      var v = value.replace(/\s+/g, '').replace(/[^0-9]/gi, '')
      var matches = v.match(/\d{4,16}/g);
      var match = matches && matches[0] || ''
      var parts = []
      for (i=0, len=match.length; i<len; i+=4) {
        parts.push(match.substring(i, i+4))
      }
      if (parts.length) {
        return parts.join(' ')
      } else {
        return value
      }
    }
  4. Ardından bu fonksiyonu kredi kartı alanına her input girdiğimizde çağırmamız gerekecek 
    document.querySelector('[name=card_number]').addEventListener('input', function(e) {
        this.value = cc_format(this.value);
    });
  5. Ön tarafta bunu yaptık ama backend'e gelen değeri XXXX XXXX XXXX XXXX Pos'a yollamadan önce tekrar XXXXXXXXXXXXXXX formatına döndürmemiz lazım. 
    $paymentCard->setCardNumber(Str::replace(' ', '', $request->cc_num));

Kaynaklar

  1. İyzico Docs, API - Ödeme - link
  2. İyzico Github Doc - Ödeme PHP - link
  3. Test Kartları - link
  4. HTML Form ile kredi kartı - link
  5. Youtube Video - Kredi kartı normalization - link
  6. Kredi kartı formatlama bilgisi - link
  7. Github repo - link
  8. CVV Validation - link