Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0] wechat #1445

Open
wants to merge 3 commits into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# generated from manifests external_dependencies
wechatpy
1 change: 1 addition & 0 deletions setup/wechat/odoo/addons/wechat
6 changes: 6 additions & 0 deletions setup/wechat/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
10 changes: 10 additions & 0 deletions wechat/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Use this docker for development
FROM itprojectsllc/install-odoo:12.0-dev

USER root

RUN pip install wechatpy[cryptography]
RUN pip install wdb requests-mock # those are included in base image now and can be removed
RUN pip install python-alipay-sdk # it's for alipay modules, but we install it here for convinience

USER odoo
89 changes: 89 additions & 0 deletions wechat/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
.. image:: https://itpp.dev/images/infinity-readme.png
:alt: Tested and maintained by IT Projects Labs
:target: https://itpp.dev

============
WeChat API
============

Basic tools to integrate Odoo and WeChat.

.. contents::
:local:

Payment methods
===============

Quick Pay (micropay)
--------------------

Buyer presents the pay code, Vendor scans the code to finish the transaction.

Native Payment (QR Code Payment)
--------------------------------

The Vendor gets one-time url and shows it to Buyer as a QR Code, Buyer scans to finish the transaction.

Official Account Payment
------------------------

There are two types of usage:

* **In-App Web-based Payment** -- The Payer opens the Vendor's HTML5 pages on their WeChat and calls the WeChat payment module via the JSAPI interface to pay their transaction. Client side of this process (i.e. web pages) is not supported. While it could be implemented as additional module, we recommend to develop *Mini programs* instead.
* **Mini program** -- an application as a part of WeChat App is created via *WeChat Developer tools*.

In-App Payment
--------------

This payment way is only for native mobile application. This module provides server part of the process.

WeChat Documentation & tools
============================

Sandbox & Debugging
-------------------

* API Debug Console https://open.wechat.com/cgi-bin/newreadtemplate?t=overseas_open/docs/oa/basic-info/debug-console
* Creating Test Accounts https://admin.wechat.com/debug/cgi-bin/sandbox?t=sandbox/login

* Note: it may not work from non-chinese IP addresses
* You will get ``appid`` and ``appsecret`` values
* To work with WeChat payments you also need Merchant ID, which this sandbox
doesn't provide. It seems, that to work with Payments you need a real
account and use *sandbox* mode (*System Parameter* ``wechat.sandbox``).

Payments
--------

* https://pay.weixin.qq.com/wechatpay_guide/help_docs.shtml

Debugging
=========

Local Debug
-----------

To debug UI, create *System Parameter* ``wechat.local_sandbox`` with value ``1``. All requests to wechat will return fake result without making a request.

Native Payments debugging
-------------------------

* It seems that in sandbox mode it's allowed to use only prices ``1.01`` and ``1.02``.

Questions?
==========

To get an assistance on this module contact us by email :arrow_right: [email protected]

Contributors
============
* `Ivan Yelizariev <https://it-projects.info/team/yelizariev>`__


Further information
===================

Odoo Apps Store: https://apps.odoo.com/apps/modules/13.0/wechat/


Tested on `Odoo 12.0 <https://github.com/odoo/odoo/commit/b05e34a0d9b13a1c6971b99ed3e5fa20199f3545>`_
3 changes: 3 additions & 0 deletions wechat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License MIT (https://opensource.org/licenses/MIT).
from . import models
from . import controllers
29 changes: 29 additions & 0 deletions wechat/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2018 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2018 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
# License MIT (https://opensource.org/licenses/MIT).
{
"name": """WeChat API""",
"summary": """Technical module to integrate Odoo with WeChat""",
"category": "Hidden",
"images": [],
"version": "14.0.1.0.1",
"application": False,
"author": "IT-Projects LLC, Ivan Yelizariev",
"support": "[email protected]",
"website": "https://github.com/itpp-labs/pos-addons#readme",
"license": "Other OSI approved licence", # MIT
"depends": ["product", "account", "qr_payments"],
"external_dependencies": {"python": ["wechatpy"], "bin": []},
"data": [
"views/account_menuitem.xml",
"views/wechat_micropay_views.xml",
"views/wechat_order_views.xml",
"views/wechat_refund_views.xml",
"views/account_journal_views.xml",
"data/ir_sequence_data.xml",
"security/ir.model.access.csv",
],
"qweb": [],
"auto_install": False,
"installable": True,
}
2 changes: 2 additions & 0 deletions wechat/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# License MIT (https://opensource.org/licenses/MIT).
from . import wechat_controllers
37 changes: 37 additions & 0 deletions wechat/controllers/wechat_controllers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2018 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2018 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
# License MIT (https://opensource.org/licenses/MIT).
import logging

from lxml import etree

from odoo import http
from odoo.http import request

_logger = logging.getLogger(__name__)


class WechatController(http.Controller):
@http.route(
"/wechat/callback", methods=["POST"], auth="public", type="http", csrf=False
)
def wechat_callback(self):
xml_raw = request.httprequest.get_data().decode(request.httprequest.charset)
_logger.debug(
"/wechat/callback request data: %s\nheaders %s: ",
xml_raw,
request.httprequest.headers,
)

# convert xml to object
xml = etree.fromstring(xml_raw)
data = {}
for child in xml:
data[child.tag] = child.text

res = request.env["wechat.order"].sudo().on_notification(data)

if res is not False:
return """<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>"""
else:
return """<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[Signature failure]]></return_msg></xml>"""
58 changes: 58 additions & 0 deletions wechat/data/ir_sequence_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2018 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
License MIT (https://opensource.org/licenses/MIT). -->
<odoo noupdate="1">
<record id="seq_wechat_micropay" model="ir.sequence">
<field name="name">WeChat Micropay</field>
<field name="code">wechat.micropay</field>
<field name="prefix">WMP-</field>
<field name="padding">3</field>
<field name="company_id" eval="False" />
</record>
<record id="seq_wechat_order" model="ir.sequence">
<field name="name">WeChat Order</field>
<field name="code">wechat.order</field>
<field name="prefix">WO-</field>
<field name="padding">3</field>
<field name="company_id" eval="False" />
</record>
<record id="seq_wechat_refund" model="ir.sequence">
<field name="name">WeChat Refund</field>
<field name="code">wechat.refund</field>
<field name="prefix">WR-</field>
<field name="padding">3</field>
<field name="company_id" eval="False" />
</record>
<record id="wechat_local_sandbox" model="ir.config_parameter">
<field name="key">wechat.local_sandbox</field>
<field name="value" />
</record>
<record id="wechat_app_id" model="ir.config_parameter">
<field name="key">wechat.app_id</field>
<field name="value" />
</record>
<record id="wechat_app_secret" model="ir.config_parameter">
<field name="key">wechat.app_secret</field>
<field name="value" />
</record>
<record id="wechat_mch_id" model="ir.config_parameter">
<field name="key">wechat.mch_id</field>
<field name="value" />
</record>
<record id="wechat_sub_mch_id" model="ir.config_parameter">
<field name="key">wechat.sub_mch_id</field>
<field name="value" />
</record>
<record id="wechat_sandbox" model="ir.config_parameter">
<field name="key">wechat.sandbox</field>
<field name="value" />
</record>
<record id="wechat_mch_cert" model="ir.config_parameter">
<field name="key">wechat.mch_cert</field>
<field name="value" />
</record>
<record id="wechat_mch_key" model="ir.config_parameter">
<field name="key">wechat.mch_key</field>
<field name="value" />
</record>
</odoo>
9 changes: 9 additions & 0 deletions wechat/doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
`1.0.1`
-------

**Improvement:** Automatic keys creation after module installation

`1.0.0`
-------

**Init version**
73 changes: 73 additions & 0 deletions wechat/doc/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
============
WeChat API
============

.. contents::
:local:

Installation
============

* Install `wechatpy library<https://github.com/jxtech/wechatpy>`__::

pip install wechatpy
pip install wechatpy[cryptography]

# to update existing installation use
pip install -U wechatpy

* Be sure that your server available for requests from outside world (i.e. it shall not be avaialble in local network only)

Multi database
--------------

If you have several databases, you need to check that all requests are sent to the desired database. The notification request from the WeChat cloud does not contain session cookies. So, if Odoo cannot determine which database to use, it will return a 404 error (Page not found).
In order for the requests to send to the desired database, you need to configure `dbfilter <https://odoo-development.readthedocs.io/en/latest/admin/dbfilter.html>`__.

WeChat APP
==========

Check WeChat documentation or contact WeChat Support about obtaining APP credentials.

Configuration
=============

Credentials
-----------

* `Activate Developer Mode <https://odoo-development.readthedocs.io/en/latest/odoo/usage/debug-mode.html>`__
* Open menu ``[[ Settings ]] >> Parameters >> System Parameters``
* Create following parameters

* ``wechat.app_id``
* ``wechat.app_secret``
* ``wechat.mch_id`` -- *Vendor ID*
* ``wechat.sub_mch_id`` -- *Sub Vendor ID*
* ``wechat.sandbox`` -- set to ``0`` or delete to disable. Any other value to means that sandbox is activated.
* ``wechat.mch_cert``, ``wechat.mch_key`` -- **path** to key and certificate files at server. Example of values:

* ``wechat.mch_cert``: ``/path/to/apiclient_cert.pem``
* ``wechat.mch_key``: ``/path/to/apiclient_key.pem``

* ``wechat.payment_result_notification_url`` -- optional. Use it if doesn't work automatiically. The url must be ``http(s)://YOUR_HOST/wechat/callback``.

Internal Numbers
----------------

If you get error ``invalid out_trade_no``, it means that you use the same
credentials in new database and odoo sends Wechat Order IDs that were previously
used in another system. To resolve this do as following:

* Go to ``[[ Settings ]] >> Technical >> Sequence & Identifiers >> Sequences``
* Find record *WeChat Order*, *Wechat Refund* or *Wechat Micropay*, depending on which request has the problem
* Change either **Prefix**, **Suffix** or **Next Number**
* If you get the error again, try to increase **Next Number**

Wechat tracking
---------------
Wechat records (Orders, Micropays, Refunds, etc.) can be found at ``[[ Invoicing ]] >> Configuration >> Wechat``. If you don't have that menu, you need to configure ``Show Full Accounting Features`` for your user first:

* `Activate Developer Mode <https://odoo-development.readthedocs.io/en/latest/odoo/usage/debug-mode.html>`__
* Open menu ``[[ Settings ]] >> Users & Companies >> Users``
* Open user you need
* Activate ``Show Full Accounting Features``
Loading