python實現支付功能

2021-08-29 18:30:14 字數 2754 閱讀 5514

# 支付寶開發者公鑰(支付寶生成)

alipay_public_key = os.path.join(base_dir, 'utils/paycenter/alipay/certs/alipay_public_key')

_alipay_public_key_obj_pub = openssl.crypto.load_publickey(openssl.crypto.filetype_pem, open(alipay_public_key).read())

_alipay_public_key_obj_x509 = openssl.crypto.x509()

_alipay_public_key_obj_x509.set_pubkey(_alipay_public_key_obj_pub)

alipay_public_key_obj = _alipay_public_key_obj_x509

銀聯支付

import time

import hashlib

import urllib, urllib2

import base64

import openssl

from django.conf import settings

def build_sign(param_map, sign_type="rsa"):

'''構建簽名'''

# 將篩選的引數按照第乙個字元的鍵值ascii碼遞增排序(字母公升序排序),如果遇到相同字元則按照第二個字元的鍵值ascii碼遞增排序,以此類推。

sort_param = sorted([(key, unicode(value, settings.unionpay_encoding).encode(settings.unionpay_encoding)) for key, value in param_map.iteritems()], key=lambda x: x[0])

content = '&'.join(['='.join(x) for x in sort_param])

message = hashlib.sha1(content).hexdigest()

return base64.b64encode(openssl.crypto.sign(settings.unionpay_private_key_obj, message, 'sha1'))

def build_params(out_trade_no, total_amount):

params = {}

# 獲取配置資訊

params['acctype'] = settings.unionpay_acc_type

params['accesstype'] = settings.unionpay_access_type

params['backurl'] = settings.unionpay_back_url

params['fronturl'] = settings.unionpay_front_url

params['biztype'] = settings.unionpay_biz_type

params['certid'] = settings.unionpay_cert_id

params['channeltype'] = settings.unionpay_channel_type

params['currencycode'] = settings.unionpay_currency_code

params['encoding'] = settings.unionpay_encoding

params['merid'] = settings.unionpay_mer_id

params['signmethod'] = settings.unionpay_sign_method

params['txntype'] = settings.unionpay_txn_type

params['txnsubtype'] = settings.unionpay_txn_subtype

params['version'] = settings.unionpay_version

params['orderid'] = out_trade_no

params['txnamt'] = '%d' % int(total_amount) # 單位為分

params['txntime'] = time.strftime('%y%m%d%h%m%s') # 

params['signature'] = build_sign(params)

#     return params

return urllib.urlencode(params)

def check_sign(message, sign):

try:

openssl.crypto.verify(settings.unionpay_public_key_obj, sign, message, 'sha1')

return true

except exception as _:

return false

# 銀聯公鑰證書

unionpay_public_key_cert = os.path.join(unionpay_certs_path, unionpay_public_key_cert_filename)

unionpay_public_key_obj = openssl.crypto.load_certificate(openssl.crypto.filetype_pem, open(unionpay_public_key_cert).read())

小程式支付功能的實現

1.支付按鈕 price suffix label 積分 button text 提交訂單 bind submit gocreateorder 2.js const wxpay require utils pay.js 去建立訂單 gocreateorder 建立訂單 createorder 呼叫建...

支付寶介面呼叫實現支付功能筆記

登入支付寶開放平台,進入沙箱服務。支付寶牛皮!sign 簽名 timestamp datetime.datetime.now strftime y m d h m s version 1.0 biz content json.dumps separators 將字典轉換為字串,去掉空格。print ...

PHP實現的支付寶支付功能示例

介面 import alipay.aopclient extend path import alipay.request.alipaytradeapppayrequest extend path aop new aopclient aop gatewayurl aop appid config ge...