import requests
import re
import json

# İlk GET isteği - Ürün sayfası
headers_get = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36",
    "Pragma": "no-cache",
    "Accept": "*/*"
}

response = requests.get("https://www.nazilliavm.com/urun/mondial-strada-egzos-koruma-demiri", headers=headers_get)

# Cookie'leri al
cookies = response.cookies.get_dict()
phpsessid = cookies.get('PHPSESSID', '')
anticsrf = cookies.get('anticsrf', '')
cart_session_id = cookies.get('cart_session_id', '')
_cfuvid = cookies.get('_cfuvid', '')

print(f"Cookies alındı:")
print(f"PHPSESSID: {phpsessid}")
print(f"anticsrf: {anticsrf}")
print(f"cart_session_id: {cart_session_id}")
print(f"_cfuvid: {_cfuvid}\n")

# İkinci POST isteği - Sepete ekleme
headers_post = {
    "x-requested-with": "XMLHttpRequest",
    "accept": "*/*",
    "accept-language": "tr-TR,tr;q=0.7",
    "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
    "origin": "https://www.nazilliavm.com",
    "priority": "u=1, i",
    "referer": "https://www.nazilliavm.com/urun/mondial-strada-egzos-koruma-demiri",
    "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"
}

cookies_dict = {
    "PHPSESSID": phpsessid,
    "cart_session_id": cart_session_id,
    "_cfuvid": _cfuvid,
    "shipping_location_data": "5li24ayhgd0cg8sgow44gc4ckcc4s4c0owsoc40o8so8kg8kspwj6swv2s7_18",
    "LastVisitedProducts": "%7B%220%22%3A%7B%22ID%22%3A157%7D%2C%221%22%3A%7B%22ID%22%3A156%7D%2C%224%22%3A%7B%22ID%22%3A197%7D%7D",
    "anticsrf": anticsrf,
    "previousPage": "%2Furun%2Fmondial-strada-egzos-koruma-demiri"
}

data = {
    "anticsrf": anticsrf,
    "productId": "197",
    "quantity": "1"
}

response_cart = requests.post("https://www.nazilliavm.com/cart/add_item", headers=headers_post, cookies=cookies_dict, data=data)
print("Sepete ekleme yanıtı:")
print(response_cart.text)
print()

# Üçüncü POST isteği - Order Step 3
headers_order = {
    "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.nazilliavm.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.nazilliavm.com/order/step2",
    "accept-encoding": "gzip, deflate, br",
    "priority": "u=0, i"
}

cookies_order = {
    "PHPSESSID": phpsessid,
    "cart_session_id": cart_session_id,
    "_cfuvid": _cfuvid,
    "LastVisitedProducts": "%5B%7B%22ID%22%3A197%7D%5D",
    "anticsrf": anticsrf,
    "shipping_location_data": f"{cart_session_id}_16"
}

multipart_data = {
    "email": "asd128as9as89@asdasd.com",
    "firstname": "asddads",
    "surname": "asdasd",
    "country": "1",
    "location": "16",
    "subLocation": "924",
    "phoneNumber": "",
    "mobilePhoneNumber": "+90 (532) 189 58 92",
    "shippingIdentityRegistrationNumber": "11111111110",
    "address": "asd128 8a9sd8912 d242 asdas adas asda das da",
    "invoiceType": "individual",
    "commercialName": "",
    "taxNumber": "",
    "taxOffice": "",
    "billingFirstname": "",
    "billingSurname": "",
    "billingCountry": "1",
    "billingLocation": "",
    "billingSubLocation": "",
    "billingPhoneNumber": "",
    "billingMobilePhoneNumber": "",
    "billingAddress": "",
    "shippingCompanyId": "2",
    "giftNote": "",
    "anticsrf": anticsrf
}

response_order = requests.post("https://www.nazilliavm.com/order/step3", headers=headers_order, cookies=cookies_order, data=multipart_data)
print("Order step3 yanıtı:")
print(response_order.text[:500])  # İlk 500 karakter
print()

# Step 3'ten gelen yeni cookie'leri güncelle
for cookie_name, cookie_value in response_order.cookies.items():
    if cookie_name == 'PHPSESSID':
        phpsessid = cookie_value
    elif cookie_name == 'anticsrf':
        anticsrf = cookie_value
    elif cookie_name == 'cart_session_id':
        cart_session_id = cookie_value
    elif cookie_name == '_cfuvid':
        _cfuvid = cookie_value

print(f"Step 3'ten sonra güncellenmiş cookie'ler:")
print(f"PHPSESSID: {phpsessid}")
print(f"anticsrf: {anticsrf}")
print()

# Dördüncü POST isteği - Iyzico Prepare Form
headers_iyzico = {
    "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.nazilliavm.com",
    "sec-fetch-site": "same-origin",
    "sec-fetch-mode": "cors",
    "sec-fetch-dest": "empty",
    "referer": "https://www.nazilliavm.com/order/step3",
    "accept-encoding": "gzip, deflate, br",
    "priority": "u=1, i"
}

cookies_iyzico = {
    "PHPSESSID": phpsessid,
    "cart_session_id": cart_session_id,
    "_cfuvid": _cfuvid,
    "LastVisitedProducts": "%5B%7B%22ID%22%3A197%7D%5D",
    "shipping_location_data": f"{cart_session_id}_16",
    "anticsrf": anticsrf
}

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

response_iyzico = requests.post("https://www.nazilliavm.com/order/iyzico/prepare-form", headers=headers_iyzico, cookies=cookies_iyzico, data=data_iyzico)
print("Iyzico prepare-form yanıtı:")
print(response_iyzico.text[:1000])
print()

# Response'dan token'ı parse et
import re
token_match = re.search(r'token:"([^"]+)"', response_iyzico.text)
iyzico_token = token_match.group(1) if token_match else None
print(f"Iyzico Token: {iyzico_token}\n")

# Beşinci POST isteği - Iyzico Payment (Kart ile ödeme)
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.nazilliavm.com",
    "sec-fetch-site": "cross-site",
    "sec-fetch-mode": "cors",
    "sec-fetch-dest": "empty",
    "referer": "https://www.nazilliavm.com/",
    "accept-encoding": "gzip, deflate, br",
    "priority": "u=1, i"
}

payment_data = {
    "installment": 1,
    "paidPrice": 400,
    "paymentChannel": "WEB",
    "paymentCard": {
        "cardNumber": "5269110188858080",
        "cardHolderName": "asds dasdasd",
        "expireYear": "27",
        "expireMonth": "12",
        "cvc": "131",
        "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": ""
    }
}

response_payment = requests.post("https://api.iyzipay.com/payment/iyzipos/checkoutform/auth/ecom", 
                                  headers=headers_payment, 
                                  json=payment_data)
print("Iyzico Payment yanıtı:")
print(response_payment.text)
print()

print("\n=== ÖZET ===")
print(json.dumps({
    "step1": "Product page loaded",
    "step2_status": response_cart.status_code,
    "step3_status": response_order.status_code,
    "step4_status": response_iyzico.status_code,
    "step4_token": iyzico_token,
    "step5_payment_status": response_payment.status_code,
    "step5_payment_response": response_payment.text,
    "cookies": {
        "PHPSESSID": phpsessid,
        "anticsrf": anticsrf,
        "cart_session_id": cart_session_id,
        "_cfuvid": _cfuvid
    }
}, indent=2, ensure_ascii=False))
