iyzico .NET SDK

repository·master·Indexed 41 days ago

https://github.com/iyzico/iyzipay-dotnet

The official .NET SDK for integrating iyzico payment services. It provides strongly-typed classes for handling payments, basket items, and buyer information, with support for .NET Framework and .NET Standard 2.1.

Tokens
2.3K
Snippets
3
Records
7
Agent score
1%

What's inside iyzico

  1. Handle decimal deserialization differences between .NET Framework and .NET Standard

    master

    Be aware of how Newtonsoft.Json handles decimal-to-string deserialization, as it affects string comparisons of amount fields:

    • .NET Framework 4.5: Decimals are deserialized to strings with trailing zeros trimmed (e.g., "0.028875").
    • .NET Standard 2.1: Decimals are deserialized to strings without trimming trailing zeros (e.g., "0.02887500").

    When performing equality checks on string-based decimal fields, ensure your expected string matches the specific runtime's behavior.

    // .NET Framework 4.5 behavior
    bool isEqual = payment.IyziCommissionRateAmount.Equals("0.028875") // true
    
    // .NET Standard behavior
    bool isEqual = payment.IyziCommissionRateAmount.Equals("0.028875"); // false
    bool isEqual = payment.IyziCommissionRateAmount.Equals("0.02887500"); // true
  2. Configure Iyzipay dependencies

    master

    Depending on your target runtime, ensure you have the correct version of Newtonsoft.Json installed:

    • .NET Framework: Requires Newtonsoft.Json version 13.0.2.
    • .NET Standard 2.1: Requires Newtonsoft.Json version 11.0.2.
  3. Create a payment request

    master

    To process a payment, you must initialize an Options object with your credentials and a CreatePaymentRequest object containing the transaction details (price, currency, buyer info, card info, and basket items). Finally, call Payment.Create(request, options).

    Options options = new Options();
    options.ApiKey = "your api key";
    options.SecretKey = "your secret key";
    options.BaseUrl = "https://sandbox-api.iyzipay.com";
    
    CreatePaymentRequest request = new CreatePaymentRequest();
    request.Locale = Locale.TR.ToString();
    request.ConversationId = "123456789";
    request.Price = "1";
    request.PaidPrice = "1.2";
    request.Currency = Currency.TRY.ToString();
    request.Installment = 1;
    request.BasketId = "B67832";
    request.PaymentChannel = PaymentChannel.WEB.ToString();
    request.PaymentGroup = PaymentGroup.PRODUCT.ToString();
    
    PaymentCard paymentCard = new PaymentCard();
    paymentCard.CardHolderName = "John Doe";
    paymentCard.CardNumber = "5528790000000008";
    paymentCard.ExpireMonth = "12";
    paymentCard.ExpireYear = "2030";
    paymentCard.Cvc = "123";
    paymentCard.RegisterCard = 0;
    request.PaymentCard = paymentCard;
    
    Buyer buyer = new Buyer();
    buyer.Id = "BY789";
    buyer.Name = "John";
    buyer.Surname = "Doe";
    buyer.GsmNumber = "+905350000000";
    buyer.Email = "email@email.com";
    buyer.IdentityNumber = "74300864791";
    buyer.LastLoginDate = "2015-10-05 12:43:35";
    buyer.RegistrationDate = "2013-04-21 15:12:09";
    buyer.RegistrationAddress = "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1";
    buyer.Ip = "85.34.78.112";
    buyer.City = "Istanbul";
    buyer.Country = "Turkey";
    buyer.ZipCode = "34732";
    request.Buyer = buyer;
    
    Address shippingAddress = new Address();
    shippingAddress.ContactName = "Jane Doe";
    shippingAddress.City = "Istanbul";
    shippingAddress.Country = "Turkey";
    shippingAddress.Description = "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1";
    shippingAddress.ZipCode = "34742";
    request.ShippingAddress = shippingAddress;
    
    Address billingAddress = new Address();
    billingAddress.ContactName = "Jane Doe";
    billingAddress.City = "Istanbul";
    billingAddress.Country = "Turkey";
    billingAddress.Description = "Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1";
    billingAddress.ZipCode = "34742";
    request.BillingAddress = billingAddress;
    
    List<BasketItem> basketItems = new List<BasketItem>();
    BasketItem firstBasketItem = new BasketItem();
    firstBasketItem.Id = "BI101";
    firstBasketItem.Name = "Binocular";
    firstBasketItem.Category1 = "Collectibles";
    firstBasketItem.Category2 = "Accessories";
    firstBasketItem.ItemType = BasketItemType.PHYSICAL.ToString();
    firstBasketItem.Price = "0.3";
    basketItems.Add(firstBasketItem);
    
    // ... add other items ...
    
    request.BasketItems = basketItems;
    
    Payment payment = Payment.Create(request, options);
  4. Mock test cards for successful payments

    master

    Use these card numbers to simulate successful transactions in the sandbox environment:

    Card NumberBankCard Type
    5890040000000016AkbankMaster Card (Debit)
    5526080000000006AkbankMaster Card (Credit)
    4766620000000001DenizbankVisa (Debit)
    4603450000000000DenizbankVisa (Credit)
    4729150000000005Denizbank BonusVisa (Credit)
    4987490000000002FinansbankVisa (Debit)
    5311570000000005FinansbankMaster Card (Credit)
    9792020000000001FinansbankTroy (Debit)
    9792030000000000FinansbankTroy (Credit)
    5170410000000004Garanti BankasıMaster Card (Debit)
    5400360000000003Garanti BankasıMaster Card (Credit)
    374427000000003Garanti BankasıAmerican Express
    4475050000000003HalkbankVisa (Debit)
    5528790000000008HalkbankMaster Card (Credit)
    4059030000000009HSBC BankVisa (Debit)
    5504720000000003HSBC BankMaster Card (Credit)
    5892830000000000Türkiye İş BankasıMaster Card (Debit)
    4543590000000006Türkiye İş BankasıVisa (Credit)
    4910050000000006VakıfbankVisa (Debit)
    4157920000000002VakıfbankVisa (Credit)
    5168880000000002Yapı ve Kredi BankasıMaster Card (Debit)
    5451030000000000Yapı ve Kredi BankasıMaster Card (Credit)
  5. Mock test cards for error scenarios

    master

    Use these card numbers to trigger specific error responses during testing:

    Card NumberDescription
    5406670000000009Success but cannot be cancelled, refund or post auth
    4111111111111129Not sufficient funds
    4129111111111111Do not honour
    4128111111111112Invalid transaction
    4127111111111113Lost card
    4126111111111114Stolen card
    4125111111111115Expired card
    4124111111111116Invalid cvc2
    4123111111111117Not permitted to card holder
    4122111111111118Not permitted to terminal
    4121111111111119Fraud suspect
    4120111111111110Pickup card
    4130111111111118General error
    4131111111111117Success but mdStatus is 0
    4141111111111115Success but mdStatus is 4
    41511111111111123dsecure initialize failed