def confirm_pay(self, request): if request.method == "GET": params = request.args.to_dict() else: params = request.form.to_dict() # 弹出签名 sign = params.pop('sign', None) # 弹出签名类型 params.pop('sign_type', None) params_sort = sorted(params.items(), key=lambda e: e[0], reverse=False) # 取出字典元素按key的字母升序排序形成列表 message = "&".join(u"{}={}".format(k, v) for k, v in params_sort).encode() # 将列表转为二进制参数字符串 self.logger.debug(message) try: # 验证签名并获取结果 status = verify_with_rsa(self.alipay_client_config.app_public_key, message, sign) except Exception as e: # 验证失败 return False, {}, "fail" if 'trade_status' in params: if params['trade_status'] == 'TRADE_SUCCESS': params['total_price'] = float(params['total_amount']) return True, params, 'success' else: return False, params, 'success' else: # 返回验证结果 return True, False, ''
def check_notify(params): ''' 对从支付宝获取的 notify 信息进行处理,判断订单状态 :传入参数: 支付宝发送的包含签名类型、签名等信息的字典参数 :返回: bool 值 ''' sign = params.pop('sign', None) params.pop('sign_type') params = sorted(params.items(), key=lambda e: e[0], reverse=False) message = "&".join(u"{}={}".format(k, v) for k, v in params).encode() if 'sign_type' in params and 'sign' in params: sign = params['sign'] del params['sign_type'] del params['sign'] try: status = verify_with_rsa( CLIENTCONFIG.alipay_public_key, message, sign) return status except VerificationError as error: print(error) return False return False
def verify_params_sign(self, params): # 文档 https://docs.open.alipay.com/194/103296#s5 sign = params.pop('sign', None) # 取出sign参数 params.pop('sign_type') # 取出sign_type参数 params = sorted(params.items(), key=lambda e: e[0], reverse=False) # 取出字典元素按key的字母升序排序形成列表 message = "&".join(u"{}={}".format(k, v) for k, v in params).encode() # 将列表转为二进制参数字符串 try: status = verify_with_rsa( self.alipay_public_key.encode('utf-8').decode('utf-8'), message, sign) # 验证签名并获取结果 return status # 返回验证结果 except Exception as e: # 如果验证失败,返回假值。 return False
def CheckAppPay(self, params): sign = params.pop('sign', None) # 取出签名 params.pop('sign_type') # 取出签名类型 params = sorted(params.items(), key=lambda e: e[0], reverse=False) # 取出字典元素按key的字母升序排序形成列表 message = "&".join(u"{}={}".format(k, v) for k, v in params).encode() # 将列表转为二进制参数字符串 # with open(settings.ALIPAY_PUBLIC_KEY_PATH, 'rb') as public_key: # 打开公钥文件 try: # status =verify_with_rsa(public_key.read().decode(),message,sign) # 验证签名并获取结果 status = verify_with_rsa( self.alipay_client_config.alipay_public_key.encode( 'utf-8').decode('utf-8'), message, sign) # 验证签名并获取结果 return status # 返回验证结果 except: # 如果验证失败,返回假值。 return False
def CheckAppPay(self, params): # 取出签名 sign = params.pop('sign', None) # 取出签名类型 params.pop('sign_type') # 取出字典元素按key的字母升序排序形成列表 params = sorted(params.items(), key=lambda e: e[0], reverse=False) # 将列表转为二进制参数字符串 message = "&".join(u"{}={}".format(k, v) for k, v in params).encode() try: status = verify_with_rsa( self.alipay_client_config.alipay_public_key.encode( 'utf-8').decode('utf-8'), message, sign) # 验证签名并获取结果 return status # 返回验证结果 except: # 如果验证失败,返回假值。 return False
def check_pay(self, params): # 定义检查支付结果的函数 from alipay.aop.api.util.SignatureUtils import verify_with_rsa sign = params.pop('sign', None) # 取出签名 params.pop('sign_type') # 取出签名类型 params = sorted(params.items(), key=lambda e: e[0], reverse=False) # 取出字典元素按key的字母升序排序形成列表 message = "&".join(u"{}={}".format(k, v) for k, v in params).encode() # 将列表转为二进制参数字符串 # with open(settings.ALIPAY_PUBLIC_KEY_PATH, 'rb') as public_key: # 打开公钥文件 try: # status =verify_with_rsa(public_key.read().decode(),message,sign) # 验证签名并获取结果 status = verify_with_rsa( settings.ALIPAY_PUBLIC_KEY.encode('utf-8').decode('utf-8'), message, sign) # 验证签名并获取结果 return status # 返回验证结果 except: # 如果验证失败,返回假值。 return False
def check_pay(self, params): """ 支付宝返回结果验签 :param params: :return: """ sign = params.pop('sign', None) # 取出签名 params.pop('sign_type') # 取出签名类型 params = sorted(params.items(), key=lambda e: e[0], reverse=False) # 取出字典元素按key的字母升序排序形成列表 message = "&".join(u"{}={}".format(k, v) for k, v in params).encode() # 将列表转为二进制参数字符串 try: with open(ali_pub_key_path) as public_key: # 打开公钥文件 status = verify_with_rsa( public_key.read().encode('utf-8').decode('utf-8'), message, sign) # 验证签名并获取结果 return status # 返回验证结果 except VerificationError: return status
def check_pay(params): """定义检查支付结果的函""" sign = params.pop('sign', None) # 取出签名 sigan_type = params.pop('sign_type') # 取出签名类型 params = sorted(params.items(), key=lambda e: e[0], reverse=False) message = "&".join("{}={}".format(k, v) for k, v in params).encode() # 将列表转为二进制参数字符串 public_key_dir = os.path.join(os.path.dirname(__file__), 'keys/alipay_public_key.txt') print("校验中 公钥是%s" % public_key_dir) with open(public_key_dir, 'rb') as public_key: try: status = verify_with_rsa(public_key.read().decode('utf-8'), message, sign) # 验证签名并获取结果 return status # 返回验证结果 except Exception as e: # 如果验证失败,返回假值 return False
def post(self, request, *args, **kwargs): """ alipay asyncnotify api view """ try: params = { smart_str(k): smart_str(v) for k, v in request.data.iteritems() } sign = params.pop('sign') params.pop('sign_type') with open( settings.ALIPAY_APP_INFO['basic_info'] ['ALIPAY_RSA_PUBLIC_KEY'], 'r') as fp: public_key = fp.read() if verify_with_rsa(public_key, get_sign_content(params), sign): passback_params = request.data.get("passback_params") out_trade_no = request.data.get("out_trade_no", "") post_data = { "trade_type": "alipay", "out_trade_no": out_trade_no, "trade_no": request.data.get("trade_no"), "total_fee": request.data.get("total_amount"), "buyer_email": request.data.get("buyer_email"), "extra_common_param": request.data.get("passback_params"), } if out_trade_no != "": rep = requests.post(passback_params, data=post_data) rep_data = rep.json() if rep_data.get('result') == "success": return HttpResponse('success') except Exception, e: log.exception(e)
def check_pay(params): # 定义检查支付结果的函数 from alipay.aop.api.util.SignatureUtils import verify_with_rsa sign = params.pop('sign', None) # 取出签名 print('public_key:') print(public_key) print('sign:') print(sign) params.pop('sign_type') # 取出签名类型 params = sorted(params.items(), key=lambda e: e[0], reverse=False) # 取出字典元素按key的字母升序排序形成列表 message = "&".join(u"{}={}".format(k, v) for k, v in params).encode() # 将列表转为二进制参数字符串 print('message:') print(message) # with open(settings.ALIPAY_PUBLIC_KEY_PATH, 'rb') as public_key: # 打开公钥文件 try: status = verify_with_rsa(public_key, message, sign) # 验证签名并获取结果 return status # 返回验证结果 except Exception as e: # 访问异常的错误编号和详细信息 print(e.args) print(str(e)) print(repr(e)) return False