├── README.md ├── alipay.py └── whmcs ├── alipaypersonal.php └── callback ├── alipaypersonal_callback.php └── pay-with-alipay.png /README.md: -------------------------------------------------------------------------------- 1 | #python版的 whmcs 的支付宝免签接口 2 | 3 | ##依赖: 4 | python,pyquery,requests 5 | 使用 pip 进行第三方库的安装 6 | 1、pip install pyquery 7 | 2、pip install requests 8 | 9 | ##使用方法: 10 | 先安装依赖、 11 | whmcs 安装 whmcs 文件夹里面的接口文件。并填写 key 12 | 填写 alipay.py里面的信息: 13 | key;api;接收邮件和发送邮件的邮件,密码。还有 cookies 的ALIPAYJSESSIONID 14 | 15 | 建议使用 screen 进行守护,没有的话也可以。 16 | Python alipay.py跑起来,screen 后台运行就好。 17 | ## 关于ALIPAYJSESSIONID的说明: 18 | 登陆https://lab.alipay.com/consume/record/items.htm 19 | 打开开发者工具(F12)里面的 console,输入 document.cookie 20 | 将ALIPAYJSESSIONID的值尽快复制到 alipay.py里面,并尽快跑起来。 21 | 22 | ##Thank: 23 | whmcs 的接口来自 https://github.com/qibinghua/whmcs_alipaypersonal 24 | 25 | -------------------------------------------------------------------------------- /alipay.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | from pyquery import PyQuery as pq 3 | import requests 4 | import time 5 | import smtplib 6 | from email.mime.text import MIMEText 7 | orderList = {} 8 | SessionID = "" # cookies中的ALIPAYJSESSIONID值 9 | key = " XXXXXX" # API 密匙 10 | api = "XXXXX" # API 地址 11 | mailto_list=['xxx@qq.com','xxxx@qq.com'] #收件人(列表) 12 | mail_host="smtp.qq.com" #使用的邮箱的smtp服务器地址 13 | mail_user="xxx" # 发邮件QQ 帐号 14 | mail_pass="xxxx" # QQ密码 15 | mail_postfix="qq.com" #邮箱的后缀 16 | def send_mail(to_list,sub,content): 17 | me="alipay"+"<"+mail_user+"@"+mail_postfix+">" 18 | msg = MIMEText(content,_subtype='plain') 19 | msg['Subject'] = sub 20 | msg['From'] = me 21 | msg['To'] = ";".join(to_list) #将收件人列表以‘;’分隔 22 | try: 23 | server = smtplib.SMTP() 24 | server.connect(mail_host) #连接服务器 25 | server.login(mail_user,mail_pass) #登录操作 26 | server.sendmail(me, to_list, msg.as_string()) 27 | server.close() 28 | except: 29 | exit() 30 | def postData(PaymentID, Time, Name, Amount): 31 | data = {'key': key,'ddh': PaymentID,'time': Time,'name': Name,'money': Amount} 32 | requests.post(api, data=data,timeout=5) 33 | def check_order(SessionID): 34 | localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 35 | print "staring get order", localtime 36 | r = requests.get("https://lab.alipay.com/consume/record/items.htm", cookies = {'ALIPAYJSESSIONID': SessionID},timeout=20) 37 | if r.url.startswith('https://auth.alipay.com/'): 38 | try: 39 | send_mail(mailto_list,"alipay dead","alipay dead") 40 | exit() 41 | except: 42 | exit() 43 | orderTable = pq(r.text)("tbody tr") 44 | for order in orderTable: 45 | order_data = {} 46 | order_pq = pq(order) 47 | try: 48 | order_data['money'] = order_pq(".amount.income").text() # 交易金额 49 | if order_data['money'] == "": 50 | continue 51 | order_data['time'] = order_pq(".time").text() # 交易时间 52 | order_data['name'] = order_pq(".name.emoji-li").text()[:-7].encode("utf-8") # 描述 53 | order_data['ddh'] = order_pq(".number").text() # 交易号 54 | if order_data['ddh'] in orderList: 55 | print "all order already exist" 56 | break 57 | else: 58 | postData(order_data['ddh'],order_data['time'],order_data['name'],order_data['money']) 59 | orderList[order_data['ddh']] = order_data 60 | except Exception as e: 61 | print e 62 | try: 63 | send_mail(mailto_list,"alipay dead","alipay dead") 64 | exit() 65 | except: 66 | exit() 67 | if __name__ == "__main__": 68 | while True: 69 | print len(orderList) 70 | if len(orderList) > 1000: 71 | orderList = {} 72 | check_order(SessionID) 73 | time.sleep(5) -------------------------------------------------------------------------------- /whmcs/alipaypersonal.php: -------------------------------------------------------------------------------- 1 | array( 8 | "Type" => "System", 9 | "Value" => "Alipay 支付宝收款接口" 10 | ), 11 | "seller_email" => array( 12 | "FriendlyName" => "卖家支付宝帐户", 13 | "Type" => "text", 14 | "Size" => "32", 15 | ), 16 | "security_code" => array( 17 | "FriendlyName" => "安全检验码", 18 | "Type" => "text", 19 | "Size" => "32", 20 | ), 21 | ); 22 | 23 | return $configarray; 24 | } 25 | 26 | function alipaypersonal_form($params) { 27 | 28 | # Invoice Variables 29 | $systemurl = $params['systemurl']; 30 | $invoiceid = $params['invoiceid']; 31 | $amount = $params['amount']; # Format: ##.## 32 | $seller_email = $params['seller_email']; 33 | $name = 'whmcs_' . $invoiceid; 34 | $memo = "请勿修改付款说明里内容,否则无法完成订购"; 35 | $form_html = '
36 | 37 | 38 | 39 | 40 | 41 |
'; 42 | $img = $systemurl . "/modules/gateways/callback/pay-with-alipay.png"; //这个图片要先存放好. 43 | $code = $form_html . '点击使用支付宝支付'; 44 | $script = ''; 50 | 51 | $code .= $script; 52 | return $code; 53 | } 54 | 55 | function alipaypersonal_link($params) { 56 | return alipaypersonal_form($params); 57 | } 58 | 59 | ?> -------------------------------------------------------------------------------- /whmcs/callback/alipaypersonal_callback.php: -------------------------------------------------------------------------------- 1 | 0 && !empty($order_data['ddh']) && !empty($order_data['key']) && $order_data['key'] == $key) { 24 | if (strpos($order_data['name'], 'whmcs_') === 0) { 25 | $invoiceid = (int)substr($order_data['name'], strlen('whmcs_')); 26 | if ($invoiceid > 0) { 27 | $order_data['invoice_id'] = $invoiceid; 28 | $order_data['status'] = 'success'; 29 | log_result($order_data); 30 | 31 | return $order_data; 32 | } 33 | 34 | } 35 | } 36 | 37 | return false; 38 | } 39 | 40 | function log_result($word) { 41 | $file = '/tmp/alipay_log.txt'; 42 | if (!is_string($word)) { 43 | $word = json_encode($word); 44 | } 45 | $string = strftime("%Y%m%d%H%I%S", time()) . "\t" . $word . "\n"; 46 | file_put_contents($file, $string, FILE_APPEND); 47 | } 48 | 49 | $gatewaymodule = "alipaypersonal"; # Enter your gateway module name here replacing template 50 | $GATEWAY = getGatewayVariables($gatewaymodule); 51 | if (!$GATEWAY["type"]) 52 | die("Module Not Activated"); # Checks gateway module is active before accepting callback 53 | 54 | $order_data = $_POST; 55 | $gatewaySELLER_EMAIL = $GATEWAY['seller_email']; 56 | $gatewaySECURITY_CODE = $GATEWAY['security_code']; 57 | $order_data = verify_post($order_data, $gatewaySECURITY_CODE); 58 | if (!$order_data) { 59 | logTransaction($GATEWAY["name"], $_POST, "Unsuccessful"); 60 | echo 'faild'; 61 | exit; 62 | } 63 | 64 | # Get Returned Variables 65 | $status = $order_data['status']; //获取支付宝传递过来的交易状态 66 | $invoiceid = $order_data['invoice_id']; //订单号 67 | $transid = $order_data['ddh']; //转账交易号 68 | $amount = $order_data['money']; //获取支付宝传递过来的总价格 69 | $fee = 0; 70 | if ($status == 'success') { 71 | $invoiceid = checkCbInvoiceID($invoiceid, $GATEWAY["name"]); # Checks invoice ID is a valid invoice number or ends processing 72 | //checkCbTransID($transid); # Checks transaction number isn't already in the database and ends processing if it does 73 | $table = "tblaccounts"; 74 | $fields = "transid"; 75 | $where = array("transid" => $transid); 76 | $result = select_query($table, $fields, $where); 77 | $data = mysql_fetch_array($result); 78 | if (!$data) { 79 | addInvoicePayment($invoiceid, $transid, $amount, $fee, $gatewaymodule); 80 | logTransaction($GATEWAY["name"], $_POST, "Successful"); 81 | } 82 | echo "success"; 83 | } else { 84 | echo 'faild'; 85 | } 86 | 87 | ?> -------------------------------------------------------------------------------- /whmcs/callback/pay-with-alipay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J3n5en/whmcs-alipay-python/bcf0c4a2970a65bb9a95ca077727bd44c2475d3f/whmcs/callback/pay-with-alipay.png --------------------------------------------------------------------------------