pythonsteamsteamworks-api

How to call Steam InitTxn properly using python?


I’m making an in app purchase for my game on Steam. On my server I use python 3. I’m trying to make an https request as follows:

conn = http.client.HTTPSConnection("partner.steam-api.com")
orderid = uuid.uuid4().int & (1<<64)-1
print("orderid = ", orderid)
key = "xxxxxxxxxxxxxxxxxxx" # omitted for security reason
steamid = "xxxxxxxxxxxxxxxxxxx" # omitted for security reason
pid = "testItem1"
appid = "480"
itemcount = 1
currency = 'CNY'
amount = 350
description = 'testing_description'
urlSandbox = "/ISteamMicroTxnSandbox/"
s = f'{urlSandbox}InitTxn/v3/?key={key}&orderid={orderid}&appid={appid}&steamid={steamid}&itemcount={itemcount}&currency={currency}&itemid[0]={pid}&qty[0]={1}&amount[0]={amount}&description[0]={description}'
print("s = ", s)
conn.request('POST', s)
r = conn.getresponse()
print("InitTxn result = ", r.read())

I checked the s in console, which is:

s =  /ISteamMicroTxnSandbox/InitTxn/v3/?key=xxxxxxx&orderid=11506775749761176415&appid=480&steamid=xxxxxxxxxxxx&itemcount=1&currency=CNY&itemid[0]=testItem1&qty[0]=1&amount[0]=350&description[0]=testing_description

However I got a bad request response:

InitTxn result =  b"<html><head><title>Bad Request</title></head><body><h1>Bad Request</h1>Required parameter 'orderid' is missing</body></html>"

How to solve this? Thank you!

BTW I use almost the same way to call GetUserInfo, except changing parameters and replace POST with GET request, and it works well.

Just read that I should put parameters in post. So I changed the codes to as follows, but still get the same error of "Required parameter 'orderid' is missing"

    params = {
    'key': key,
    'orderid': orderid,
    'appid': appid,
    'steamid': steamid,
    'itemcount': itemcount,
    'currency': currency,
    'pid': pid,
    'qty[0]': 1,
    'amount[0]': amount,
    'description[0]': description
}
s = urllib.parse.urlencode(params)
# In console: s =  key=xxxxx&orderid=9231307508782239594&appid=480&steamid=xxx&itemcount=1&currency=CNY&pid=testItem1&qty%5B0%5D=1&amount%5B0%5D=350&description%5B0%5D=testing_description
print("s = ", s)
conn.request('POST', url=f'{urlSandbox}InitTxn/v3/', body=s)

==== update ====

Format issue has been solved. Please see the answer below.


Solution

  • I missed the content-type part in headers, and language. Also itemid should be uint32.

    The final codes example:

    conn = http.client.HTTPSConnection("partner.steam-api.com")
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    orderid = uuid.uuid4().int & (1<<64)-1
    print("orderid = ", orderid)
    key = "xxxxxxxxxxxxxxxxxxx" # omitted for security reason
    steamid = "xxxxxxxxxxxxxxxxxxx" # omitted for security reason
    itemid = 100001
    appid = "480"
    itemcount = 1
    currency = 'CNY'
    amount = 350
    language = 'zh-CN'
    description = 'testing_description'
    urlSandbox = "/ISteamMicroTxnSandbox/"
    s = f'key={key}&orderid={orderid}&appid={appid}&steamid={steamid}&itemcount={itemcount}&language={language}&currency={currency}&itemid[0]={itemid}&qty[0]={1}&amount[0]={amount}&description[0]={description}'
    conn.request('POST', url=f'{urlSandbox}InitTxn/v3/', headers=headers, body=s)
    r = conn.getresponse()
    print("InitTxn result = ", r.read())