IAP
IAP的概念
1. 客户 向 服务提供商发起 提供服务请求。 客户将传递账号令牌,服务提供商使用令牌来识别用户
2. 接受到客户请求服务后。 服务商检查 Odoo IAP客户的账号里是否有足够的余额。没有余额则拒绝掉本次服务
3. 在保留余额后,服务者执行服务。 某些情况下,服务提供者会调用外部服务来执行所请求的服务
4. 执行完服务后,服务提供者回到Odoo API来获取所预留的余额。 如果请求的服务未能正常提供服务,服务提供者会要求Odoo释放所预留余额
5. 服务提供者会回到客户。 通知他们请求服务成功,返回一些成功信息
1.服务提供者返回到客户并告知他们在账户中没有足够的余额,显示用户可以购买服务的信息(一个Odoo服务包链接)。
2. 客户重定向到Odoo并进行服务的充值。
注册OdooIAP服务
创建IAP模块
pass 跳过
@api.model
def _books_data_by_isbn(self, isbn):
book = self.search([('isbn', '=', isbn)], limit=1)
if book:
return {
'status': 'found',
'data': {
'name': book.name,
'isbn': book.isbn,
'date_release': book.date_release,
'cover_image': book.cover_image,
'authors': [a.name for a in book.author_ids]
}
}
else:
return {
'status': 'not found',
}
@http.route('/get_book_data', type='json', auth="public")
def get_book_data(self, account_token, isbn_number):
service_key = request.env['ir.config_parameter'].sudo().get_param('iap.isbn_service_key', False)
if not service_key:
return {
'status': 'service is not active'
}
credits_to_reserve = 1
data = {}
with iap.charge(request.env, service_key, account_token, credits_to_reserve,
credit_template='iap_isbn_service.no_credit_info'):
data = request.env['book.info'].sudo()._books_data_by_isbn(isbn_number)
if data['status'] == 'not found':
raise Exception('Book not found')
return data
授权并收取IAP余额
from odoo import models, fields
class ConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
isbn_service_key = fields.Char("ISBN service key", config_parameter='iap.isbn_service_key')
account_token 是 服务端价差当前客户余额时,服务者发送给客户端的令牌
def xxx(self,account_token,isbn_list=None):
.....
isbn_list = [el for el in isbn_list]
credits_to_reserve = len(isbn_list)
data_fond = []
with iap.charge(request.env, service_key, account_token, credits_to_reserve,
credit_template='iap_isbn_service.no_credit_info') as transection:
for isbn in isbn_list:
data = request.env['book.info'].sudo()._books_data_by_isbn(isbn)
if data['status'] == 'not found':
data_fond.append(data_fond)
transection.credit = len(data_fond)
return data_fond

创建IAP客户端模块
<button name="fetch_book_data"
string="Fetch Book Data" type="object"/>
def fetch_book_data(self):
self.ensure_one()
if not self.isbn:
raise UserError("Please add ISBN number")
user_token = self.env['iap.account'].get('book_isbn')
params = {
'account_token': user_token.account_token,
'isbn_number': self.isbn
}
service_endpoint = 'http://localhost:8888'
result = jsonrpc(service_endpoint + '/get_book_data', params=params)
if result.get('status') == 'found':
self.write(self.process_result(result['data']))
return True
@api.model
def process_result(self, result):
authors = []
existing_author_ids = []
for author_name in result['authors']:
author = self.env['res.partner'].search([('name', '=', author_name)], limit=1)
if author:
existing_author_ids.append(author.id)
else:
authors.append((0, 0, {'name': author_name}))
if existing_author_ids:
authors.append((6, 0, existing_author_ids))
return {
'author_ids': authors,
'name': result.get('name'),
'isbn': result.get('isbn'),
'cover_image': result.get('cover_image'),
'date_release': result.get('date_release'),
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?