python - 过帐付款数据

标签 python python-3.x

我一直在研究这个脚本。这是一个在基于 shopify 的网站上自动结帐的脚本,如下所示 ( https://www.cityblueshop.com/products/kixx_asst )。我的问题是除了提交付款数据之外一切正常。由于某种原因,即使我正确提取了 cc_verify_id 的 id,它也不会发布付款。如果你们可以测试一下并让我知道我做错了什么(在这一步上坚持了几天),那么我将非常感激。您可以输入虚假的联系方式和信用卡信息。附:我是编程新手,所以它可能看起来很困惑。预先感谢您的帮助。

[编辑]看起来它没有从 paymentdata 正确提交数据,但我仍然无法确定问题出在哪里。

import requests, sys, time, re
from datetime import datetime
from bs4 import BeautifulSoup
from urllib.parse import urlparse


s = requests.session()

def UTCtoEST():
    current=datetime.now()
    return str(current) + ' EST'

home = 'cityblueshop'
###Get Session Id###
session = s.get('https://www.'+home+'.com/cart.js').json()
sessionID = session['token']
print('SessionID:', sessionID)

###ATC###
print(UTCtoEST(), 'Adding item....')
atcdata = {
    'id': '37431305678',
    'quantity': '1'
}
for atcurlRetry in range(1):
    atcURL = s.post('https://www.'+home+'.com/cart/add.js', data=atcdata, allow_redirects=True)
    match = re.findall('"quantity":1', atcURL.text)
    if match:
        print(UTCtoEST(), 'ATC successful....')
        break
    print(UTCtoEST(), 'Trying to ATC....')
    time.sleep(0)
else:
    print(UTCtoEST(), 'Could not ATC after ' + ' retries, therefore exiting the bot.')
    sys.exit(1)

###Going to Checkout Page###
for cartRetry in range(1):
    cartdata = {
        'updates[]': 1,
        'note': '',
        'checkout': 'Check Out'
    }
    atc = s.post('https://www.'+home+'.com/cart', data=cartdata, allow_redirects=True)
###Parsing URL###
    parse = urlparse(atc.url)
    storeID = parse.path.split('/')[1]
    checkoutID = parse.path.split('checkouts/')[1]
    print('Checkout Session Id:', checkoutID)
###Get Token###
    soup = BeautifulSoup(atc.text, 'lxml')
    input = soup.find_all('input')[2]
    auth_token = input.get('value')
    print('Auth_token:', auth_token)
###Get Contact info###
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.8',
        'Host': 'checkout.shopify.com',
        'Referer': 'https: //checkout.shopify.com/'+storeID+'/checkouts/'+checkoutID+'?step=contact_information',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
    }
    qs = {
        'utf8': '✓',
        '_method': 'patch',
        'authenticity_token': auth_token,
        'previous_step': 'contact_information',
        'checkout[email]': 'email',
        'checkout[shipping_address][first_name]': 'First',
        'checkout[shipping_address][last_name]': 'Last',
        'checkout[shipping_address][company]': '',
        'checkout[shipping_address][address1]': 'Address 1',
        'checkout[shipping_address][address2]': '',
        'checkout[shipping_address][city]': 'City',
        'checkout[shipping_address][country]': 'United States',
        'checkout[shipping_address][province]': '',
        'checkout[shipping_address][province]': '',
        'checkout[shipping_address][province]': 'New York',
        'checkout[shipping_address][zip]': 'Zip',
        'checkout[shipping_address][phone]': 'Phone',
        'checkout[remember_me]': '',
        'checkout[remember_me]': '0',
        'checkout[client_details][browser_width]': '979',
        'checkout[client_details][browser_height]': '631',
        'checkout[client_details][javascript_enabled]': '1',
        'step': 'contact_information'
    }
    GETcontact = s.get(atc.url, data=qs, headers=headers, allow_redirects=True)

###Post Contact Info###
    headers1 = {
        'Origin': 'https://checkout.shopify.com',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.8',
        'Referer': 'https://checkout.shopify.com/'+storeID+'/checkouts/'+checkoutID,
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
    }
    formData = {
        'utf8': '✓',
        '_method': 'patch',
        'authenticity_token': auth_token,
        'button': '',
        'checkout[email]': 'Email',
        'checkout[shipping_address][first_name]': 'First',
        'checkout[shipping_address][last_name]': 'Last',
        'checkout[shipping_address][company]': '',
        'checkout[shipping_address][address1]': 'Address 1',
        'checkout[shipping_address][address2]': '',
        'checkout[shipping_address][city]': 'City',
        'checkout[shipping_address][country]': 'United States',
        'checkout[shipping_address][province]': 'New York',
        'checkout[shipping_address][zip]': 'Zip',
        'checkout[shipping_address][phone]': 'Phone',
        'checkout[remember_me]': '0',
        'checkout[client_details][browser_width]': '979',
        'checkout[client_details][browser_height]': '631',
        'checkout[client_details][javascript_enabled]': '1',
        'previous_step': 'contact_information',
        'step': 'shipping_method'
    }
    POSTcontact = s.post(atc.url, data=formData, headers=headers1, allow_redirects=True)
    ###Parsing Shipping Method###
    soup = BeautifulSoup(POSTcontact.text, 'html.parser')
    shipping = soup.find(attrs={'class': 'radio-wrapper'})
    shipping_method = ship.get('data-shipping-method')

###Submitting Shipping Data###
    headers2 = {
        'Origin': 'https://checkout.shopify.com',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.8',
        'Referer': 'https://checkout.shopify.com/'+storeID+'/checkouts/'+checkoutID,
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
    }
    ShipformData = {
        'utf8': '✓',
        '_method': 'patch',
        'authenticity_token': auth_token,
        'previous_step': 'shipping_method',
        'step': 'payment_method',
        'checkout[shipping_rate][id]': shipping_method,
        'button': '',
        'checkout[client_details][browser_width]': '1280',
        'checkout[client_details][browser_height]': '368',
        'checkout[client_details][javascript_enabled]': '1'
    }
    shippingmethod = s.post(atc.url, data=ShipformData, headers=headers2, allow_redirects=True)
    ###Parsing payment_gateaway###
    soup = BeautifulSoup(shippingmethod.text, 'html.parser')
    ul = soup.find(attrs={'class': 'radio-wrapper content-box__row '})
    payment_gateaway = ul.get('data-select-gateway')

###submitting payment info###
    CCheaders = {
        'accept': 'application/json',
        'Origin': 'https://checkout.shopifycs.com',
        'Accept-Language': 'en-US,en;q=0.8',
        'Host': 'elb.deposit.shopifycs.com',
        'content-type': 'application/json',
        'Referer': 'https://checkout.shopifycs.com/number?identifier='+checkoutID+'&location=3A%2F%2Fcheckout.shopify.com%2F'+storeID+'%2Fcheckouts%2F'+checkoutID+'%3Fpreviousstep%3Dshipping_method%26step%3Dpayment_method',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
    }
    ccinfo = {
        'number': "0000 0000 0000 0000",
        'name': "First Last",
        'month': 5,
        'year': 2020,
        'verification_value': "000"
    }
    creditcard = s.post('https://elb.deposit.shopifycs.com/sessions', json=ccinfo, headers=CCheaders, allow_redirects=True)
    cc_verify = creditcard.json()
    cc_verify_id = cc_verify['id']

###submitting credit card info##
    paymentheaders = {
        'Origin': 'https://checkout.shopify.com',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.8',
        'Referer': 'https://checkout.shopify.com/'+storeID+'/checkouts/'+checkoutID,
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
    }
    paymentdata = {
        '_method': 'patch',
        'authenticity_token': auth_token,
        'checkout[buyer_accepts_marketing]': '1',
        'checkout[client_details][browser_height]': '979',
        'checkout[client_details][browser_width]': '631',
        'checkout[client_details][javascript_enabled]': '1',
        'checkout[credit_card][vault]': 'false',
        'checkout[different_billing_address]': 'false',
        'checkout[payment_gateway]': payment_gateaway,
        'checkout[total_price]': '1199',
        'complete': '1',
        'previous_step': 'payment_method',
        's': cc_verify_id,
        'step': '',
        'utf8': '✓'
      }
    submitpayment = s.post(atc.url, data=paymentdata, headers=paymentheaders, allow_redirects=True)
    print(UTCtoEST(), submitpayment.status_code, submitpayment.url)

最佳答案

只是一个猜测,但如果您尝试发布 JSON,这不是一个正确的 key ..

'checkout[total_price]': '1199',

您需要重写为

'checkout' : {
    'total_price': '1199',
} 

您需要将此解决方案应用于该格式的所有其他值

例如

 'checkout' : {
    'remember_me' : '', 
    'shipping_address' : { 
        'first_name': 'First',
        "last_name': 'Last'

我认为您可以使用 Python False 值代替字符串 ' false',但这取决于 API

关于python - 过帐付款数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43077274/

相关文章:

python - 在Python字典中for循环时添加相同的键和值

python - 当我按下确认销售按钮时,创建一个 'ref' 字段自动递增

python - 高效计算点云的体素指数

python - 在输出末尾获取额外的空行

python - Cython:将 C 缓冲区内存 View 返回给 Python

Python - 随机婴儿名字生成器问题 - (重复输入,调用变量)

javascript - 如何使 Transcrypt 编译为对象而不是字典?

python - 选择 -> 选项抽象

python - 使用多个外键

python - tkinter python 的全局变量问题