|
| 1 | +from __future__ import unicode_literals |
| 2 | + |
| 3 | +import logging |
| 4 | +import random |
| 5 | +import time |
| 6 | + |
| 7 | +import requests |
| 8 | + |
| 9 | +import odoo |
| 10 | +from odoo.http import request |
| 11 | + |
| 12 | +_logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +try: |
| 15 | + from odoo.addons.bus.controllers.main import BusController |
| 16 | +except ImportError: |
| 17 | + _logger.error("pos_multi_session_sync inconsisten with odoo version") |
| 18 | + BusController = object |
| 19 | + |
| 20 | + |
| 21 | +class Controller(BusController): |
| 22 | + @odoo.http.route("/wechat/getsignkey", type="json", auth="public") |
| 23 | + def getSignKey(self, message): |
| 24 | + data = {} |
| 25 | + data["mch_id"] = request.env["ir.config_parameter"].get_param("wechat.mchId") |
| 26 | + wcc = request.env["wechat.config"] |
| 27 | + data["nonce_str"] = (wcc.getRandomNumberGeneration(message))[:32] |
| 28 | + data["sign"] = ( |
| 29 | + str(time.time()).replace(".", "") |
| 30 | + + "{:010}".format(random.randint(1, 9999999999)) |
| 31 | + + "{:010}".format(random.randint(1, 9999999999)) |
| 32 | + )[:32] |
| 33 | + post = wcc.makeXmlPost(data) |
| 34 | + url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey" |
| 35 | + r1 = requests.post(url, data=post, timeout=30) |
| 36 | + message = {} |
| 37 | + message["resp1"] = r1.text |
| 38 | + return message |
| 39 | + |
| 40 | + @odoo.http.route("/wechat/test", type="json", auth="public") |
| 41 | + def testAccessToken(self, message): |
| 42 | + wcc = request.env["wechat.config"] |
| 43 | + if not wcc: |
| 44 | + wcc = wcc.create({"token_validity": 7000, "access_token": "test"}) |
| 45 | + wcc.getAccessToken() |
| 46 | + |
| 47 | + @odoo.http.route("/wechat/payment_commence", type="json", auth="public") |
| 48 | + def micropay(self, message): |
| 49 | + # data = message['data'] |
| 50 | + # data['order_id'] = '{0:06}'.format(message['data']['order_id']) |
| 51 | + # data['cashier_id'] = '{0:05}'.format(message['data']['cashier_id']) |
| 52 | + # data['session_id'] = '{0:05}'.format(message['data']['session_id']) |
| 53 | + data = {} |
| 54 | + data["auth_code"] = message["data"]["auth_code"] |
| 55 | + data["appid"] = request.env["ir.config_parameter"].get_param("wechat.appId") |
| 56 | + data["mch_id"] = request.env["ir.config_parameter"].get_param("wechat.mchId") |
| 57 | + data["body"] = message["data"]["order_short"] |
| 58 | + |
| 59 | + data["out_trade_no"] = ( |
| 60 | + str(time.time()).replace(".", "") |
| 61 | + + "{:010}".format(random.randint(1, 9999999999)) |
| 62 | + + "{:010}".format(random.randint(1, 9999999999)) |
| 63 | + )[:32] |
| 64 | + wcc = request.env["wechat.config"] |
| 65 | + if not wcc: |
| 66 | + wcc = wcc.create({"token_validity": 1, "access_token": ""}) |
| 67 | + data["total_fee"] = message["data"]["total_fee"] |
| 68 | + data["spbill_create_ip"] = wcc.getIpList()[0] |
| 69 | + # data['auth_code'] = message['data']['auth_code'] |
| 70 | + # |
| 71 | + # device_info = |
| 72 | + # sign_type = |
| 73 | + # detail = |
| 74 | + # attach = |
| 75 | + # fee_type = |
| 76 | + # goods_tag = |
| 77 | + # limit_pay = |
| 78 | + # scene_info = |
| 79 | + # |
| 80 | + data["nonce_str"] = (wcc.getRandomNumberGeneration(message))[:32] |
| 81 | + data["sign"] = (wcc.getRandomNumberGeneration(message))[:32] |
| 82 | + |
| 83 | + post = wcc.makeXmlPost(data) |
| 84 | + r1 = requests.post( |
| 85 | + "https://api.mch.weixin.qq.com/sandboxnew/pay/micropay", |
| 86 | + data=post, |
| 87 | + timeout=30, |
| 88 | + ) |
| 89 | + message = {} |
| 90 | + message["resp1"] = r1 |
| 91 | + message["resp_text1"] = r1.text |
| 92 | + message["resp_cont1"] = r1.content |
| 93 | + # message['encode_text1'] = r1.text.encode('iso-8859-1').decode('utf-8') |
| 94 | + # print(r1.text.encode('utf-8')) |
| 95 | + time.sleep(5) |
| 96 | + # return request.redirect('/wechat/payment_query') |
| 97 | + # |
| 98 | + # @odoo.http.route('/wechat/payment_query', type="json", auth="public") |
| 99 | + # def queryOrderApi(self, message): |
| 100 | + data_qa = {} |
| 101 | + data_qa["appid"] = data["appid"] |
| 102 | + data_qa["mch_id"] = data["mch_id"] |
| 103 | + data_qa["out_trade_no"] = data["out_trade_no"] |
| 104 | + data_qa["nonce_str"] = data["nonce_str"] |
| 105 | + data_qa["sign"] = data["sign"] |
| 106 | + if hasattr(data, "sign_type"): |
| 107 | + data_qa["sign_type"] = data["sign_type"] |
| 108 | + |
| 109 | + post = wcc.makeXmlPost(data_qa) |
| 110 | + r2 = requests.post( |
| 111 | + "https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery", |
| 112 | + data=post, |
| 113 | + timeout=30, |
| 114 | + ) |
| 115 | + message["resp2"] = r2 |
| 116 | + message["resp_text2"] = r2.text |
| 117 | + message["resp_cont2"] = r2.content |
| 118 | + # message['encode_text2'] = r2.text.encode('iso-8859-1').decode('utf-8') |
| 119 | + # with open('txt.txt', 'w+') as fil: |
| 120 | + # fil.write(r1.text, r2.text) |
| 121 | + # print(r2.text.encode('utf-8')) |
| 122 | + # for each_unicode_character in r2.text.encode('utf-8').decode('utf-8'): |
| 123 | + # print(each_unicode_character) |
| 124 | + # print(message['encode_text1']) |
| 125 | + # print(message['encode_text2']) |
| 126 | + return message |
0 commit comments