天天看點

python調用微信支付_python 微信支付

微信支付的3種方式:

Native支付是指商戶系統按微信支付協定生成支付二維碼,使用者再用微信“掃一掃”完成支付的模式。該模式适用于PC網站、實體店單品或訂單、媒體廣告支付等場景。

APP支付是指商戶通過在移動端應用APP中內建開放SDK調起微信支付子產品來完成支付。适用于在移動端APP中內建微信支付功能的場景。

JSAPI支付是指商戶通過調用微信支付提供的JSAPI接口,在支付場景中調起微信支付子產品完成收款。

應用場景有:

線下場所:調用接口生成二維碼,使用者掃描二維碼後在微信浏覽器中打開頁面後完成支付

公衆号場景:使用者在微信公衆賬号内進入商家公衆号,打開某個首頁面,完成支付

PC網站場景:在網站中展示二維碼,使用者掃描二維碼後在微信浏覽器中打開頁面後完成支付

一.建立預支付訂單(Native&APP):

@classmethod

def dict_to_xml(cls, data, is_cdata=False):

xml = ['']

for k, v in data.items():

if not is_cdata:

xml.append('{1}{0}>'.format(k, v))

else:

xml.append('{0}>'.format(k, v))

xml.append('')

return ''.join(xml)

def sign(self, data):

# 1) 排序

slist = self._ordered_data(data)

# 2) &連接配接,拼接密鑰

slist.append('key={0}'.format(self.key))

string = '&'.join(slist)

# 3) md5加密

string = hashlib.md5(string.encode('utf-8')).hexdigest()

# 4) 轉大寫

result = string.upper()

return result

def build_body(self, data):

if not data.get('appid'):

data['appid'] = self.appid #應用ID

data['mch_id'] = self.mch_id #商戶号

data['nonce_str'] = self._get_nonce_str() #随機字元串

data['notify_url'] = self.notify_url #回調位址

data['sign'] = self.sign(data)

return self.dict_to_xml(data)

def request(self, url, content, headers=None):

if headers is None:

headers = {'Content-Type': 'text/xml'}

content = self.build_body(content)

req = urllib.request.Request(

url,

data=bytes(content, 'utf-8'),

headers=headers

)

result = urllib.request.urlopen(req, timeout=30).read().decode('utf-8')

return result

def api_wxpay_unifiedorder(self, body, out_trade_no, spbill_create_ip, total_fee, trade_type, product_id, **kwargs):

content = {

'body': body, #支付訂單的描述

'out_trade_no': out_trade_no, #訂單編号,

'spbill_create_ip': spbill_create_ip, #終端IP

'total_fee': total_fee, #商品價格

'trade_type': trade_type, #交易類型 APP or JSAPI or Native

'product_id': product_id #商品id

}

content.update(kwargs)

# UNIFIEDORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"

# UNIFIEDORDER_URL = "https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder"

result = self.request(UNIFIEDORDER_URL, content)

return result

data = wxpay.api_wxpay_unifiedorder(

'wechat_pay_test', out_trade_no, spbill_create_ip, total_fee,

'NATIVE', product_id)

data = wxpay.api_wxpay_unifiedorder(

'wechat_pay_test', out_trade_no, spbill_create_ip, total_fee,

'APP', '', appid=APPID)

二.建立預支付訂單(JSAPI):

def build_body(self, data):

data['mch_id'] = self.mch_id

data['nonce_str'] = self._get_nonce_str()

data['notify_url'] = self.notify_url

data['sign'] = self.sign(data)

return self.dict_to_xml(data)

def request(self, url, content, headers=None):

if headers is None:

headers = {'Content-Type': 'text/xml'}

content = self.build_body(content)

req = urllib.request.Request(

url,

data=bytes(content, 'utf-8'),

headers=headers

)

result = urllib.request.urlopen(req, timeout=30).read().decode('utf-8')

return result

def api_wxpay_unifiedorder(self, body, out_trade_no, spbill_create_ip, total_fee, trade_type, product_id, open_id, appid, **kwargs

):

content = {

'body': body,

'out_trade_no': out_trade_no,

'spbill_create_ip': spbill_create_ip,

'total_fee': total_fee,

'trade_type': trade_type,

'product_id': product_id,

'openid': open_id,

'appid': appid

}

content.update(kwargs)

result = self.request(self.config.UNIFIEDORDER_URL, content)

return result

data = applepay.api_wxpay_unifiedorder(

'wechat_pay_test', out_trade_no, spbill_create_ip, total_fee,

'JSAPI', product_id, open_id, appid)