Code Monkey home page Code Monkey logo

Comments (5)

fzlee avatar fzlee commented on July 18, 2024

同步验签和异步验签的技术实现原理应该差不多,异步需要验签是为了验证请求确实来自支付宝,而同步不太需要,是因为我们向支付宝发起HTTP请求时,已经验证了它的HTTPS证书。

不过多验证一步也没有问题,我稍后会添加这个功能

from alipay.

 avatar commented on July 18, 2024

OK,多谢!目前我也实现了同步响应验签的功能,不过感觉写得不太好,到时候可以学习下你的代码 👍

from alipay.

fzlee avatar fzlee commented on July 18, 2024

暂时推送到了dev, 我好想明白你是怎么纠结同步验签功能的了,获取待验证的字符串有点麻烦,你可以看看这个函数__get_string_to_be_signed,应该没有什么问题的

from alipay.

 avatar commented on July 18, 2024

同步验签比较麻烦,因为要保证原先 response 中的 key-value 顺序。我是用下面的方式先将 json 字符串提取成有序字典,从而保持原先的顺序不乱:

def load_from_json_text(text, keep_order=True):
    """
    加载 json 格式的文本
    """
    from json import loads
    from collections import OrderedDict

    if keep_order is True:
        return loads(text, object_pairs_hook=OrderedDict)
    else:
        return loads(text)

然后将 xxx_response 提取出来(仍然是有序字典)后再转回 json 字符串,参见下面的函数。在重新转成 json 字符串时,要把 ,, : 两边空格去掉,才能和原先的字符串一致。:

def to_json_text(data=None, sort_keys=True):
    """
    用于将字典转换成有序的字符串 JSON

    :param data: 字典数据
    :param sort_keys: 是否排序
    :return:
    """
    from json import dumps
    assert isinstance(data, dict)

    return dumps(data, ensure_ascii=False, sort_keys=sort_keys, separators=(',', ':'))

总的来说,也挺麻烦,不过多多借助了 json 模块。
:)

from alipay.

fzlee avatar fzlee commented on July 18, 2024

感觉你的方法好一点

from alipay.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.