支付宝接口调用


以前写的一个使用支付宝支付的代码。

奉上源码:
python 2.64 下 成功运行

from hashlib import md5
import hashlib
import urllib
import datetime

def __init__(self):
        self.params = {}
        self.pay_gate_way = 'https://www.alipay.com/cooperate/gateway.do'
        self.security_code = 'XXXXXXXXXXXXXXXXXXXXX'    #支付宝安全码
      
      
  
  
    def creat_url(self,input_charset, 
                                    service, 
                                    partner, 
                                    show_url, 
                                    return_url,
                                    notify_url,
                                    sign_type,
                                    subject, 
                                    body, 
                                    out_trade_no, 
                                    price, 
                                    discount, 
                                    quantity, 
                                    payment_type, 
                                    logistics_type, 
                                    logistics_fee, 
                                    logistics_payment, 
                                    seller_email):
            self.params['service'] = service
            self.params['out_trade_no'] = out_trade_no
            self.params['partner'] = partner
            self.params['show_url'] = show_url
            self.params['notify_url'] = notify_url
            self.params['discount'] = discount
            self.params['quantity'] = quantity
            self.params['payment_type'] = payment_type 
            self.params['body'] = body
            self.params['price'] = price
            self.params['seller_email'] = seller_email
            self.params['return_url'] = return_url
            self.params['logistics_type'] = logistics_type
            self.params['logistics_fee'] = logistics_fee
            self.params['subject'] = subject
            self.params['logistics_payment'] = logistics_payment
            self.params['_input_charset'] = input_charset
            return self. build_url(self.params, sign_type)
      
      
    def build_url(self, params, sign_type='MD5'): 
         unsigned_data = '&'.join(["%s=%s" % (k,v) for k, v in sorted(params.items())])     #参数按照字母顺序排序
         unsigned_data += self.security_code
         sign = self.sign(unsigned_data) 
         self.params['sign'] = sign
         self.params['sign_type'] = sign_type
         request_data = self.pay_gate_way + '?'+ urllib.urlencode(self.params)
         return request_data
      
    def sign(self,urlstr,sign_type="MD5"):
         if sign_type == 'MD5':
             m = hashlib.md5()
             m.update(urlstr)
             sign =  m.hexdigest()
         else:
             sign = ''
         return sign

你可能感兴趣的:(python,Security)