Author: |
IT-Projects LLC, Dinar Gabbasov |
License: |
Other OSI approved licence |
Branch: |
11.0 |
Repository: |
dynaz/pos-addons |
Dependencies: |
wechat |
Languages: |
Python (370, 65.7%),
XML (20, 3.6%),
and
reStructuredText (173, 30.7%) |
Other branches: |
11.0-pos_cashbox |
Other repositories: |
AITIC/pos-addons,
Alitec-sg/pos-addons,
AndryEddy/pos-addons,
BrayhanJC/pos-addons,
ERPLibre/pos-addons,
Enigma228322/pos-addons,
KolushovAlexandr/pos-addons,
KonosCL/pos-addons,
TheCloneMaster/pos-addons,
akekaphop/pos-addons,
antherkiv/pos-addons,
diegogd/pos-addons,
elsemieni/pos-addons,
em230418/pos-addons,
germanponce/pos-addons,
gokhancetiner/pos-addons,
itpp-labs/pos-addons,
keadanis/pos-addons,
mgielissen/pos-addons,
robelale/pos-addons,
samsagaz/pos-addons,
trojikman/pos-addons,
and
yelizariev/pos-addons |
<a class="reference external image-reference" href="https://itpp.dev"><img alt="Tested and maintained by IT Projects Labs" src="https://itpp.dev/images/infinity-readme.png" /></a>
<a class="reference external image-reference" href="https://opensource.org/licenses/MIT"><img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-blue.svg">
</a>
<a name="wechat-mini-program-api"></a>
<h2>WeChat mini-program API</h2>
<p>Basic tools to integrate Odoo and WeChat mini-program.</p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#payment-method" id="id1">Payment method</a></li>
<li><a class="reference internal" href="#developing-mini-program" id="id2">Developing mini-program</a><ul>
<li><a class="reference internal" href="#authentication" id="id3">Authentication</a></li>
<li><a class="reference internal" href="#rpc-calls" id="id4">RPC calls</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sandbox-debugging-of-mini-program" id="id5">Sandbox & Debugging of mini-program</a></li>
<li><a class="reference internal" href="#questions" id="id6">Questions?</a></li>
<li><a class="reference internal" href="#contributors" id="id7">Contributors</a></li>
<li><a class="reference internal" href="#further-information" id="id8">Further information</a></li>
</ul>
</div>
<a name="payment-method"></a>
<h3><a class="toc-backref" href="#id1">Payment method</a></h3>
<p><strong>Mini program</strong> uses Official Account Payment method. For more information, please see the <a class="reference external" href="https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_3&index=1">WeChat Pay Interface Document</a></p>
<a name="developing-mini-program"></a>
<h3><a class="toc-backref" href="#id2">Developing mini-program</a></h3>
<a name="authentication"></a>
<h4><a class="toc-backref" href="#id3">Authentication</a></h4>
<p>To authenticate a user from the mini-program, you must send a request with a code and user information of the mini-program to the server. To receive the code and send the request, you must use <code>wx.login</code> provided by mini-program API. Odoo will create a user if one does not exist and assign session_id which has to be sent via a cookie on each RPC request.:</p>
<pre>
<code>function AuthenticateUser(code) {
return new Promise(function(resolve, reject) {
var userInfo = app.globalData.userInfo;
code = code || false;
function do_request() {
var options = {
url: 'https://ODOO_HOST/wechat/miniprogram/authenticate',
header: {
'Content-Type': 'application/json'
},
success: function(res) {
// save session_id
var data = res.data.result;
if (data.session_id) {
wx.setStorage({
key: 'session_id',
data: data.session_id,
success: function() {
resolve(data);
}
})
} else {
reject(res);
}
},
fail: function(res) {
reject(res);
}
};
var params = {
context: {
},
code: code,
user_info: userInfo
};
// send request to Odoo server
wxJsonRpc(params, options);
}
if (code) {
do_request();
} else {
wx.login({
success: function(data) {
code = data.code;
do_request();
}
})
}
});
}</code>
</pre>
<a name="rpc-calls"></a>
<h4><a class="toc-backref" href="#id4">RPC calls</a></h4>
<p>RPC request from mini-program:</p>
<pre>
<code>function odooRpc(params, options) {
return new Promise(function(resolve, reject){
options = options || {
};
function do_request(session_id) {
options.url = 'https://ODOO_HOST/web/dataset/call_kw';
options.header = {
'Content-Type': 'applications/json',
'X-Openerp-Session-Id': session_id
};
options.success = function(res) {
var data = res.data.result;
resolve(data);
};
options.fail = function(res) {
reject(res);
};
wxJsonRpc(params, options);
}
wx.getStorage({
key: 'session_id',
success: function(res) {
if (res.data) {
do_request(res.data);
} else {
AuthenticateUser().then(function(data){
do_request(data.session_id);
});
}
},
fail: function() {
AuthenticateUser().then(function(data){
do_request(data.session_id);
});
},
});
});
}
function wxJsonRpc(params, options) {
var data = {
"jsonrpc": "2.0",
"method": "call",
"params": params,
"id": Math.floor(Math.random() * 1000 * 1000 * 1000),
}
options.data = JSON.stringify(data);
options.dataType = 'json';
options.method = 'POST';
// send request to Odoo server
wx.request(options);
}</code>
</pre>
<p><strong>Example:</strong>
Load Products from Odoo Server:</p>
<pre>
<code>var params = {
models: 'product.product',
method: 'search_read',
args: [
],
context: {
},
kwargs: {
domain: [['sale_ok','=',true],['available_in_pos','=',true]],
fields: ['display_name', 'list_price', 'lst_price', 'standard_price', 'categ_id', 'pos_categ_id', 'taxes_id',
'barcode', 'default_code', 'to_weight', 'uom_id', 'description_sale', 'description',
'product_tmpl_id','tracking'],
}
}
odooRpc(params).then(function(res) {
console.log(res);
});</code>
</pre>
<p><strong>Result:</strong> list of Products</p>
<a name="sandbox-debugging-of-mini-program"></a>
<h3><a class="toc-backref" href="#id5">Sandbox & Debugging of mini-program</a></h3>
<ul class="simple">
<li>API Debug Console <a class="reference external" href="https://open.wechat.com/cgi-bin/newreadtemplate?t=overseas_open/docs/oa/basic-info/debug-console">https://open.wechat.com/cgi-bin/newreadtemplate?t=overseas_open/docs/oa/basic-info/debug-console</a></li>
<li>Creating Test Accounts <a class="reference external" href="https://admin.wechat.com/debug/cgi-bin/sandbox?t=sandbox/login">https://admin.wechat.com/debug/cgi-bin/sandbox?t=sandbox/login</a><ul>
<li>You will get <code>sub_appid</code> and <code>sub_appsecret</code> values for work with mini-programs</li>
</ul>
</li>
</ul>
<a name="questions"></a>
<h3><a class="toc-backref" href="#id6">Questions?</a></h3>
<p>To get an assistance on this module contact us by email :arrow_right: <a class="reference external" href="mailto:help@itpp.dev">help@itpp.dev</a></p>
<a name="contributors"></a>
<h3><a class="toc-backref" href="#id7">Contributors</a></h3>
<ul class="simple">
<li><a class="reference external" href="https://it-projects.info/team/GabbasovDinar">Dinar Gabbasov</a></li>
</ul>
<a name="further-information"></a>
<h3><a class="toc-backref" href="#id8">Further information</a></h3>
<p>Odoo Apps Store: <a class="reference external" href="https://apps.odoo.com/apps/modules/11.0/wechat_miniprogram/">https://apps.odoo.com/apps/modules/11.0/wechat_miniprogram/</a></p>
<p>Tested on <a class="reference external" href="https://github.com/odoo/odoo/commit/ee2b9fae3519c2494f34dacf15d0a3b5bd8fbd06">Odoo 11.0</a></p>