独立站对接支付渠道

前言

独立站上线之前,要对接好支付系统,这样客户才能通过网站购买商品,完成整个流程。

流程图

l11zzwbt.png

对接过程比较关键两步就是组织订单参数,签名,提交数据,接受返回数据,验证签名,然后将结果返回给前端。

关键函数(python)

md5加密

代码语言:javascript
复制
    def _sign_acquire(self, data):
        '''
        组织签名字符串
        '''
        unsigned_string = "{}&{}&{}&{}&{}".format(data['CurrencyType'], data['MerchantNo'], data['ProxyChannelType'], data['TransData'], self._md5_key)
        logger.debug(unsigned_string)
    sign = self._sign(unsigned_string)
    logger.debug(sign)
    # 获得最终的订单信息字符串
    return sign

def _sign(self, s):
    """
    生成签名字符串

    """
    h = hashlib.md5()
    h.update(s.encode(encoding='utf-8'))
    digested = h.hexdigest()
    #logging.warning(digested)</code></pre></div></div><h3 id="8n8vb" name="%E6%8F%90%E4%BA%A4%E6%95%B0%E6%8D%AE">提交数据</h3><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">    def _fetch(self, url, data_json):
    &#34;&#34;&#34;
    post 提交数据
    &#34;&#34;&#34;
    headers = {&#39;Content-Type&#39;: &#39;application/json;charset=UTF-8&#39;}
    #logger.debug(url)
    req = urllib.request.Request(url, data=json.dumps(data_json).encode(), headers=headers)
    try:
        resp = urllib.request.urlopen(req, timeout=30)

    except urllib.error.HTTPError as e:
        resp = e
    #logger.debug(resp.read().decode())
    raw_dict = json.loads(resp.read().decode())
    return raw_dict</code></pre></div></div><blockquote><p>本文共 127 个字数,平均阅读时长 ≈ 1分钟</p></blockquote>