为什么使用三方支付?
为什么使用三方支付?
再没有三方支付平台之前,用户发起支付请求的时候,用户要去和银行签约(转账),特别的不方便,为了解决这些问题,就有了三方支付,三方平台去完成签约,给用户节省时间。
支付宝支付的流程
商户拿到支付宝的公钥、自己的私钥(私钥加密、公钥解密),用私钥请求支付宝,支付宝解密、验签、进行支付处理,支付宝将处理的返回值传给商户,当支付成功后,返还给商户订单号、金额、时间戳等消息,支付失败后同样给商户反馈结果。
配置流程
1、获取APPID
支付宝开放平台:https://open.alipay.com/
登录支付宝开放平台–>点击控制台
点击沙箱(复制APPID)
2、在线生成密钥
点击文档,找到开发助手,点击在线加密。
获取私钥
3、获取公钥
点击应用公钥
生成公钥
现在已经拿到了需要的公钥。
python项目中集成支付宝
构建支付类
fromdatetimeimportdatetimefrom Crypto.PublicKeyimportRSAfrom Crypto.SignatureimportPKCS1_v1_5from Crypto.HashimportSHA256from urllib.parseimportquote_plusfrom base64importdecodebytes, encodebytesimport jsonclass AliPay:"""支付宝支付接口(PC端支付接口)"""def__init__(self, appid, app_notify_url, app_private_key_path,alipay_public_key_path, return_url, debug=False):self.appid = appidself.app_notify_url = app_notify_urlself.app_private_key_path = app_private_key_pathself.app_private_key =Noneself.return_url = return_urlwithopen(self.app_private_key_path)asfp:self.app_private_key = RSA.importKey(fp.read)self.alipay_public_key_path = alipay_public_key_pathwithopen(self.alipay_public_key_path)asfp:self.alipay_public_key = RSA.importKey(fp.read)ifdebugisTrue:self.__gateway ="https://openapi.alipaydev.com/gateway.do"else:self.__gateway ="https://openapi.alipay.com/gateway.do"defdirect_pay(self, subject, out_trade_no, total_amount, return_url=None, **kwargs):biz_content = {"subject": subject,"out_trade_no": out_trade_no,"total_amount": total_amount,"product_code":"FAST_INSTANT_TRADE_PAY",}biz_content.update(kwargs)data = self.build_body("alipay.trade.page.pay", biz_content, self.return_url)returnself.sign_data(data)defbuild_body(self, method, biz_content, return_url=None):data = {"app_id": self.appid,"method": method,"charset":"utf-8","sign_type":"RSA2","timestamp": datetime.now.strftime("%Y-%m-%d %H:%M:%S"),"version":"1.0","biz_content": biz_content}ifreturn_urlisnotNone:data["notify_url"] = self.app_notify_urldata["return_url"] = self.return_urlreturndatadefsign_data(self, data):data.pop("sign",None)unsigned_items = self.ordered_data(data)unsigned_string ="&".join("{0}={1}".format(k, v)fork, vinunsigned_items)sign = self.sign(unsigned_string.encode("utf-8"))quoted_string ="&".join("{0}={1}".format(k, quote_plus(v))fork, vinunsigned_items)signed_string = quoted_string +"&sign="+ quote_plus(sign)returnsigned_stringdefordered_data(self, data):complex_keys =forkey, valueindata.items:ifisinstance(value, dict):complex_keys.append(key)forkeyincomplex_keys:data[key] = json.dumps(data[key], separators=(,,:))returnsorted([(k, v)fork, vindata.items()])defsign(self, unsigned_string):key = self.app_private_keysigner = PKCS1_v1_5.new(key)signature = signer.sign(SHA256.new(unsigned_string))sign = encodebytes(signature).decode("utf8").replace("\n","")returnsigndef_verify(self, raw_content, signature):key = self.alipay_public_keysigner = PKCS1_v1_5.new(key)digest = SHA256.newdigest.update(raw_content.encode("utf8"))ifsigner.verify(digest, decodebytes(signature.encode("utf8"))):returnTruereturnFalsedefverify(self, data, signature):if"sign_type"indata:data.pop("sign_type")unsigned_items = self.ordered_data(data)message ="&".join(u"{}={}".format(k, v)fork, vinunsigned_items)returnself._verify(message, signature)实例化类
def init_alipay:初始化Alipayalipay = AliPay(appid="appid",app_notify_url="回调地址",return_url="回调地址",app_private_key_path="私钥相对路径",alipay_public_key_path="公钥相对路径",debug=True 支付环境)returnalipayAPI
asyncdef get(self):alipay = init_alipay传一个标题 订单号 订单价格params = alipay.direct_pay("三方广告平台", order_no, money)url = f"https://openapi.alipaydev.com/gateway.do?{params}"returnself.write(ret_json(url))构建一个回调地址,用于支付成功后回调,在回调地址中可以获取订单号(out_trade_no)、金额(total_amount)、时间戳(timestamp),然后进行处理业务逻辑。总结
支付包有自己的接口文档,以上是我在Python环境下配置的,可以直接使用。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。本文链接:https://blog.csdn.net/weixin_45394086/article/details/121843483
文章评论