一门APP开发平台使用JS调用IOS内购原生App功能

[复制链接]
曾清Lv.2 显示全部楼层 发表于 2020-2-17 12:05:46 |阅读模式 打印 上一主题 下一主题
苹果上架
一门app开发平台使用JS调用ios内购原生APP功能
demo参考:https://www.yimenapp.com/doc/demo_iap.cshtml

1.iOS内购(In-App Purchase)- 仅支持苹果端
canMakePayments 检查当前设备是否支持内购
可能由于家长控制等原因,当前设备不支持APP内购买,在发起内购前可以先判断是否支持。
  1. jsBridge.iap.canMakePayments(function(yes) {
  2.   alert(yes ? "支持内购" : "不支持内购");
  3. });
复制代码



2.getProducts 获取产品列表
  1. jsBridge.iap.getProducts({
  2.   //你在 https://appstoreconnect.apple.com/ 创建的产品ID数组
  3.   productIds: [
  4.     "com.yidiantongqa.app.vip30",
  5.     "com.yidiantongqa.app.vip60",
  6.     "com.yidiantongqa.app.vip360"
  7.   ]
  8. }, function(succ, products) {
  9.   if (succ) {
  10.     alert(JSON.stringify(products));
  11.   } else {
  12.     alert("操作失败\n" + JSON.stringify(products));
  13.   }
  14. });
  15. /**
  16. products 返回产品信息
  17. {   
  18.   invalidProductIds:[]      //无效的产品ID,如果你提供了无效的产品ID,会在这里返回,数组类型
  19.   products:                 //有效的产品信息,数组类型
  20.   [{
  21.     productId:              //产品id,字符串类型
  22.     title:                  //产品标题,字符串类型
  23.     description:            //产品描述,字符串类型
  24.     price:                  //产品价格,数字类型
  25.     formattedPrice:         //产品格式化后的价格,如¥6.00,字符串类型
  26.     downloadable:           //是否有下载内容,布尔类型
  27.     downloadContentLengths: //下载内容长度,数字组成的数组
  28.     downloadContentVersion: //下载内容的版本,字符串类型
  29.   }]
  30. }
  31. **/
复制代码


3.setTransanctionListener 设置交易监听器
  1. • 调用 getUnfinishedTransactions/purchase/restoreTransactions/finishTransaction 方法之前应设置本监听器,所有交易状态变更都会通知到此监听器。
  2. • 系统以数组形式将交易信息传递给回调参数 transactions
  3. • 如果多次设置监听器,只有最后一个才会收到通知。
复制代码

  1. jsBridge.iap.setTransanctionListener(function(succ, transactions) {
  2.   if (succ) {
  3.     var json = JSON.stringify(transactions);
  4.     jsBridge.setClipboardText(json);
  5.     //transactions.forEach(function(x){jsBridge.iap.finishTransaction({transactionId: x.transactionId});})
  6.     alert(json);
  7.   } else {
  8.     alert("操作失败\n" + JSON.stringify(transactions));
  9.   }
  10. });
  11. alert("已设置监听器");
  12. /**
  13. transactions 返回交易信息数组
  14. [{
  15.   state:                 //交易状态,详见交易状态常量,数字类型   
  16.   transactionId:         //交易id,字符串类型   
  17.   originalTransactionId: //原始交易id,只在state为恢复购买时有效,字符串类型
  18.   transactionDate:       //交易时间,UNIX 时间戳(秒)
  19.   receipt:               //交易凭证,经过base64编码,用于验证交易是否合法和有效,避免因越狱破解内购后造成损失,只在state为购买成功时有效,字符串类型
  20.   errorCode:             //交易失败时的错误码,数字类型   
  21.   errorMsg:              //交易失败时的说明,字符串类型   
  22.   payment:               //商品购买信息
  23.   {
  24.     productId:           //商品ID,字符串类型
  25.     quantity:            //购买数量,数字类型
  26.     applicationUsername: //用户信息,字符串类型
  27.   },
  28.   downloads:             //有下载内容时的下载信息列表
  29.   [{   
  30.     state:               //下载状态,详见下载状态常量,数字类型
  31.     transactionId:       //下载内容所属交易id,字符串类型
  32.     contentId:           //下载内容id,字符串类型
  33.     progress:            //下载进度,取值范围0~1,数字类型
  34.     contentLength:       //文件内容大小,数字类型
  35.     timeRemaining:       //下载剩余时间,-1时表示未知,数字类型
  36.     contentVersion:      //下载内容的版本,字符串类型
  37.     contentURL:          //下载成功后文件路径,字符串类型
  38.     errorCode:           //下载失败时的错误码,数字类型
  39.     errorMsg:            //下载失败时的错误描述,字符串类型
  40.   }]
  41. }]
  42. state(transaction) 交易状态常量说明
  43. 0: 已加入交易队列(Transaction is being added to the server queue.)
  44. 1: 交易完成(Transaction is in queue, user has been charged.  Client should complete the transaction.)
  45. 2: 交易失败(Transaction was cancelled or failed before being added to the server queue.)
  46. 3: 恢复购买(Transaction was restored from user's purchase history.  Client should complete the transaction.)
  47. 4: 交易等待被确认(The transaction is in the queue, but its final status is pending external action.)
  48.    
  49. state(download) 下载状态常量说明
  50. 0: 等待下载(Download is inactive, waiting to be downloaded)
  51. 1: 正在下载(Download is actively downloading)
  52. 2: 暂停下载(Download was paused by the user)
  53. 3: 下载完成(Download is finished, content is available)
  54. 4: 下载失败(Download failed)
  55. 5: 取消下载(Download was cancelled)
  56. **/
复制代码


4.getUnfinishedTransactions 获取未完成的交易
请在 setTransanctionListener 中接收交易数据
  1. jsBridge.iap.getUnfinishedTransactions();
复制代码


5.purchase 购买产品
请在 setTransanctionListener 中接收交易数据
  1. jsBridge.iap.purchase({
  2.   //你在 https://appstoreconnect.apple.com/ 创建的产品ID
  3.   productId : "com.yidiantongqa.app.vip60",
  4.   //可选,购买数量,不小于1的整数,默认1
  5.   quantity  : 1,
  6.   //可选,用户信息,交易回调里面原样返回,默认空
  7.   applicationUsername: "myString"
  8. });
复制代码



6.restoreTransactions 恢复用户以前购买过的所有商品交易
请在 setTransanctionListener 中接收交易数据
  1. jsBridge.iap.restoreTransactions();
复制代码


7.finishTransaction 完成指定交易
• 在完成购买并提供给用户相关功能后应该完成此次交易;
• finishTransaction 成功后,此交易将从 UnfinishedTransactions 队列中移除;
• 请在 setTransanctionListener 中接收交易数据;
  1. jsBridge.iap.finishTransaction({
  2.   //交易id
  3.   transactionId: "3A266867-8275-4299-9BEF-FF7A8A207D21"
  4. });
复制代码

8.setDownloadListener 设置下载监听器
• 调用 startDownloads/pauseDownloads/resumeDownloads/cancelDownloads 方法之前应设置本监听器,所有下载状态变更都会通知到此监听器。
• 系统以数组形式将下载信息传递给回调参数 downloads
• 如果多次设置监听器,只有最后一个才会收到通知。
  1. jsBridge.iap.setDownloadListener(function(succ, downloads) {
  2.   if (succ) {
  3.     alert(JSON.stringify(downloads));
  4.   } else {
  5.     alert("操作失败\n" + JSON.stringify(downloads));
  6.   }
  7. });
  8. alert("已设置监听器");
  9. /**
  10. downloads 下载信息数组
  11. [{   
  12.   state:         //下载状态,详见setTransanctionListener的下载状态常量说明,数字类型
  13.   transactionId: //下载内容所属交易id,字符串类型
  14.   contentId:     //下载内容id,字符串类型
  15.   progress:      //下载进度,取值范围0~1,数字类型
  16.   contentLength: //文件内容大小,数字类型
  17.   timeRemaining: //下载剩余时间,-1时表示未知,数字类型
  18.   contentVersion://下载内容的版本,字符串类型
  19.   contentURL:    //下载成功后文件路径,字符串类型
  20.   errorCode:     //下载失败时的错误码,数字类型
  21.   errorMsg:      //下载失败时的错误描述,字符串类型
  22. }]
  23. **/
复制代码


9.startDownloads 开始下载
请在 setDownloadListener 中接收下载信息
  1. jsBridge.iap.startDownloads({
  2.   //下载内容ID组成的数组
  3.   contentIds: [
  4.     "contentId1",
  5.     "contentId2",
  6.     "..."
  7.   ]
  8. });
复制代码


10.pauseDownloads 暂停下载
请在 setDownloadListener 中接收下载信息
  1. jsBridge.iap.pauseDownloads({
  2.   //下载内容ID组成的数组
  3.   contentIds: [
  4.     "contentId1",
  5.     "contentId2",
  6.     "..."
  7.   ]
  8. });
复制代码


11.resumeDownloads 恢复下载
请在 setDownloadListener 中接收下载信息
  1. jsBridge.iap.resumeDownloads({
  2.   //下载内容ID组成的数组
  3.   contentIds: [
  4.     "contentId1",
  5.     "contentId2",
  6.     "..."
  7.   ]
  8. });
复制代码


12.cancelDownloads 取消下载
请在 setDownloadListener 中接收下载信息
  1. jsBridge.iap.cancelDownloads({
  2.   //下载内容ID组成的数组
  3.   contentIds: [
  4.     "contentId1",
  5.     "contentId2",
  6.     "..."
  7.   ]
  8. });
复制代码


在服务器端验证支付凭证(receipt)
♦ 沙盒凭证验证接口 https://sandbox.itunes.apple.com/verifyReceipt
♦ 生产环境凭证验证接口 https://buy.itunes.apple.com/verifyReceipt
♦ 注意,APP上架审核时苹果审核员用的是沙盒账户;
♦ 识别沙盒环境下收据的方法有两种:
1.根据凭证验证返回的字段 environment = sandbox。
2.根据凭证验证返回的状态码,如果 status = 21007,则表示当前的收据为沙盒环境下凭证;
♦ 构造JSON { "receipt-data" : "(receipt 支付凭证)" } POST 到苹果的验证接口,处理返回结果即可。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

一门APP打包致力于H5混合APP基础框架领域的前沿探索,专注轻便的移动应用解决方案 提供基于HTML前端页面在各种应用层级的端延展。
  • 官方手机版

  • 微信公众号

  • 微信客服

  • Powered by Discuz! X3.4 | Copyright © 2001-2020, 一门APP. | 一门APP开发平台|热门标签
  • 蜀ICP备17005078号-4 | 川公网安备 51019002001185号 | |成都七扇门科技有限公司