Created
December 26, 2017 17:14
-
-
Save motord/c0d6979d7685708b02950216290e255f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *-* coding: UTF-8 *-* | |
import hashlib | |
import requests | |
from urllib.parse import urlencode, unquote_plus | |
def ksort(d): | |
return [(k, d[k]) for k in sorted(d.keys())] | |
class Payjs(object): | |
def __init__(self, merchant_id, merchant_key, notify_url, **kwargs): | |
self.merchant_key = merchant_key # 填写通信密钥 | |
self.merchant_id = merchant_id # 特写商户号 | |
self.notify_url = notify_url | |
def curl(self, data, url): | |
data['sign'] = self.sign(data) | |
r = requests.post(url, data=data) | |
return r | |
def sign(self, attributes): | |
attributes = ksort(attributes) | |
m = hashlib.md5() | |
m.update((unquote_plus(urlencode(attributes)) + '&key=' + self.merchant_key).encode(encoding='utf-8')) | |
sign = m.hexdigest() | |
sign = sign.upper() | |
return sign | |
def QRPay(self, total_fee, body, out_trade_no): | |
url = 'https://payjs.cn/api/native' | |
data = {} | |
data['mchid'] = self.merchant_id | |
data['total_fee'] = total_fee | |
data['body'] = body | |
data['out_trade_no'] = out_trade_no | |
data['notify_url'] = self.notify_url | |
return self.curl(data, url) | |
def JSPay(self, total_fee, body, out_trade_no, callback_url=None): | |
url = 'https://payjs.cn/api/jspay' | |
data = {} | |
data['mchid'] = self.merchant_id | |
data['total_fee'] = total_fee | |
data['body'] = body | |
data['out_trade_no'] = out_trade_no | |
# data['notify_url'] = self.notify_url | |
if callback_url: | |
data['callback_url'] = callback_url | |
return self.curl(data, url) | |
def Cashier(self, total_fee, body, out_trade_no, callback_url=None): | |
url = 'https://payjs.cn/api/cashier' | |
data = {} | |
data['mchid'] = self.merchant_id | |
data['total_fee'] = total_fee | |
data['body'] = body | |
data['out_trade_no'] = out_trade_no | |
# data['notify_url'] = self.notify_url | |
if callback_url: | |
data['callback_url'] = callback_url | |
return self.curl(data, url) | |
def Query(self, payjs_order_id): | |
url = 'https://payjs.cn/api/check' | |
data = {} | |
data['payjs_order_id'] = payjs_order_id | |
return self.curl(data, url) |
Author
motord
commented
Dec 26, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment