-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
209 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
*.pyc | ||
odoo-bin.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
from . import wechat_user | ||
from . import address | ||
from . import order | ||
from . import payment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import json | ||
import xmltodict | ||
|
||
from odoo import http, exceptions | ||
from odoo.http import request | ||
|
||
from .error_code import error_code | ||
from .. import defs | ||
|
||
from weixin.pay import WeixinPay | ||
|
||
import logging | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class MakePayment(http.Controller): | ||
@http.route('/<string:sub_domain>/pay/wxapp/get-pay-data', | ||
auth='public', methods=['POST'], csrf=False, type='http') | ||
def post(self, sub_domain, **kwargs): | ||
try: | ||
user = request.env['res.users'].sudo().search([('sub_domain', '=', sub_domain)]) | ||
if not user: | ||
return request.make_response(json.dumps({'code': 404, 'msg': error_code[404]})) | ||
|
||
config = self.env['wechat_mall.config.settings'] | ||
app_id = config.get_config('app_id', uid=user.id) | ||
wechat_pay_id = config.get_config('wechat_pay_id', uid=user.id) | ||
wechat_pay_secret = config.get_config('wechat_pay_id', uid=user.id) | ||
|
||
if not app_id: | ||
return request.make_response(json.dumps({'code': 404, 'msg': error_code[404]})) | ||
|
||
if not wechat_pay_id or wechat_pay_secret: | ||
return request.make_response(json.dumps({'code': 404, 'msg': '未设置wechat_pay_id和wechat_pay_secret'})) | ||
|
||
if 'token' not in kwargs.keys(): | ||
return request.make_response(json.dumps({'code': 300, 'msg': error_code[300].format('token')})) | ||
|
||
token = kwargs.pop('token') | ||
|
||
args_key_set = {'token', 'order_id', 'money'} | ||
|
||
missing_args_key = args_key_set - set(kwargs.keys()) | ||
if missing_args_key: | ||
return request.make_response(json.dumps({'code': 600, 'msg': error_code[600]})) | ||
|
||
access_token = request.env(user=user.id)['wechat_mall.access_token'].search([ | ||
('token', '=', token), | ||
('create_uid', '=', user.id) | ||
]) | ||
|
||
if not access_token: | ||
return request.make_response(json.dumps({'code': 901, 'msg': error_code[901]})) | ||
|
||
wechat_user = request.env(user=user.id)['wechat_mall.user'].search([ | ||
('open_id', '=', access_token.open_id), | ||
('create_uid', '=', user.id) | ||
]) | ||
|
||
if not wechat_user: | ||
return request.make_response(json.dumps({'code': 10000, 'msg': error_code[10000]})) | ||
|
||
wxpay = WeixinPay(appid=app_id, mch_id=wechat_pay_id, partner_key=wechat_pay_secret) | ||
|
||
except Exception as e: | ||
_logger.exception(e) | ||
return request.make_response(json.dumps({'code': -1, 'msg': error_code[-1], 'data': e.message})) | ||
|
||
|
||
class WechatPaymentNotify(http.Controller): | ||
@http.route('/<string:sub_domain>/pay/notify', | ||
auth='public', methods=['POST', 'GET'], csrf=False, type='http') | ||
def post(self, sub_domain, **kwargs): | ||
try: | ||
user = request.env['res.users'].sudo().search([('sub_domain', '=', sub_domain)]) | ||
if not user: | ||
response = request.make_response( | ||
headers={ | ||
"Content-Type": "application/xml" | ||
}, | ||
data=xmltodict.unparse({ | ||
'xml': { | ||
'return_code': 'FAIL', | ||
'return_msg': '参数格式校验错误' | ||
} | ||
}) | ||
) | ||
return response | ||
|
||
xml_data = request.httprequest.stream.read() | ||
data = xmltodict.parse(xml_data)['xml'] | ||
if data['return_code'] == 'SUCCESS': | ||
data.update({'status': defs.PaymentStatus.success}) | ||
payment = request.env(user=user.id)['wechat_mall.payment'].search([ | ||
('payment_number', '=', data['out_trade_no']) | ||
]) | ||
payment.write(data) | ||
payment.order_id.write({'status': defs.OrderStatus.pending}) | ||
mail_template = request.env.ref('wechat_mall.wechat_mall_order_paid') | ||
mail_template.sudo().send_mail(payment.order_id.id, force_send=True, raise_exception=False) | ||
else: | ||
data.update({'status': defs.PaymentStatus.fail}) | ||
payment = request.env(user=user.id)['wechat_mall.payment'].search([ | ||
('payment_number', '=', data['out_trade_no']) | ||
]) | ||
payment.write(data) | ||
|
||
response = request.make_response( | ||
headers={ | ||
"Content-Type": "application/xml" | ||
}, | ||
data=xmltodict.unparse({ | ||
'xml': { | ||
'return_code': 'SUCCESS', | ||
'return_msg': 'SUCCESS' | ||
} | ||
}) | ||
) | ||
return response | ||
|
||
except Exception as e: | ||
_logger.exception(e) | ||
response = request.make_response( | ||
headers={ | ||
"Content-Type": "application/xml" | ||
}, | ||
data=xmltodict.unparse({ | ||
'xml': { | ||
'return_code': 'FAIL', | ||
'return_msg': '服务器内部错误' | ||
} | ||
}) | ||
) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<odoo> | ||
<data noupdate="1"> | ||
|
||
<record id="seq_wechat_mall_payment_payment_num" model="ir.sequence"> | ||
<field name="name">Wechat mall payment num</field> | ||
<field name="code">wechat_mall.payment_num</field> | ||
<field name="padding">4</field> | ||
<field name="prefix">PY%(y)s%(month)s%(day)s%(h24)s%(min)s%(sec)s</field> | ||
</record> | ||
|
||
</data> | ||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ | |
from . import confirm_wizard | ||
from . import shipper | ||
from . import mail_template | ||
from . import payment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from odoo import models, fields, api | ||
|
||
from .. import defs | ||
|
||
|
||
class Payment(models.Model): | ||
_name = 'wechat_mall.payment' | ||
_description = u'支付记录' | ||
|
||
order_id = fields.Many2one('wechat_mall.order', string='订单') | ||
payment_number = fields.Char('支付单号', index=True) | ||
wechat_user_id = fields.Many2one('wechat_mall.user', string='微信用户') | ||
price = fields.Float('支付金额') | ||
status = fields.Selection(defs.PaymentStatus.attrs.items(), '状态', | ||
default=defs.PaymentStatus.unpaid) | ||
|
||
# 微信notify返回参数 | ||
openid = fields.Char('openid') | ||
result_code = fields.Char('业务结果') | ||
err_code = fields.Char('错误代码') | ||
err_code_des = fields.Char('错误代码描述') | ||
transaction_id = fields.Char('微信订单号') | ||
bank_type = fields.Char('付款银行') | ||
fee_type = fields.Char('货币种类') | ||
total_fee = fields.Integer('订单金额(分)') | ||
settlement_total_fee = fields.Integer('应结订单金额(分)') | ||
cash_fee = fields.Integer('现金支付金额') | ||
cash_fee_type = fields.Char('现金支付货币类型') | ||
coupon_fee = fields.Integer('代金券金额(分)') | ||
coupon_count = fields.Integer('代金券使用数量') | ||
|
||
_sql_constraints = [( | ||
'wechat_mall_payment_payment_number_unique', | ||
'UNIQUE (payment_number)', | ||
'wechat payment payment_number is existed!' | ||
)] | ||
|
||
@api.model | ||
def create(self, vals): | ||
vals['payment_num'] = self.env['ir.sequence'].next_by_code('wechat_mall.payment_num') | ||
return super(Payment, self).create(vals) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters