微信支付的3种方式:
Native支付是指商户系统按微信支付协议生成支付二维码,用户再用微信“扫一扫”完成支付的模式。该模式适用于PC网站、实体店单品或订单、媒体广告支付等场景。
APP支付是指商户通过在移动端应用APP中集成开放SDK调起微信支付模块来完成支付。适用于在移动端APP中集成微信支付功能的场景。
JSAPI支付是指商户通过调用微信支付提供的JSAPI接口,在支付场景中调起微信支付模块完成收款。
应用场景有:
线下场所:调用接口生成二维码,用户扫描二维码后在微信浏览器中打开页面后完成支付
公众号场景:用户在微信公众账号内进入商家公众号,打开某个主页面,完成支付
PC网站场景:在网站中展示二维码,用户扫描二维码后在微信浏览器中打开页面后完成支付
一.创建预支付订单(Native&APP):
@classmethod
def dict_to_xml(cls, data, is_cdata=False):
xml = ['<xml>']
for k, v in data.items():
if not is_cdata:
xml.append('<{0}>{1}</{0}>'.format(k, v))
else:
xml.append('<{0}><![CDATA[{1}]]></{0}>'.format(k, v))
xml.append('</xml>')
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)
网友评论