setTransanctionListener 设置交易监听器 APP开发接入苹果内购IOS内购开发教程

setTransanctionListener 设置交易监听器 APP开发接入苹果内购IOS内购开发教程

用做网页的技术做APP
一门提供标准化的jsbridge-mini.js库,您只需要在页面引用执行JS即可实现各种原生APP、原生PC的功能能力。

==================APP端==================
【APP端】(安卓版&苹果版)目前提供200+原生功能,2000+JS映射接口,用做网站的技术即可实现各种原生APP能力、APP demo地址https://www.yimenapp.com/doc/demo.cshtml下载教程:
♦ JS-SDK 引用方式:
♦ 普通网页 script 方式加载:下载最新版,请在页面上调用 jsBridge 接口之前引用 jsbridge-mini.js 库;
♦ js module 方式引用:npm install ym-jsbridge 具体请参考 npm package
一门APP开发平台通用JS

setTransanctionListener 设置交易监听器 APP开发接入苹果内购IOS内购开发教程

• 调用 getUnfinishedTransactions/purchase/restoreTransactions/finishTransaction 方法之前应设置本监听器,所有交易状态变更都会通知到此监听器。

• 系统以数组形式将交易信息传递给回调参数 transactions

• 如果多次设置监听器,只有最后一个才会收到通知。

核心代码示例,详情参阅demo

jsBridge.iap.setTransanctionListener(function(succ, transactions) {
  if (succ) {
    var json = JSON.stringify(transactions);
    jsBridge.setClipboardText(json);
    //transactions.forEach(function(x){jsBridge.iap.finishTransaction({transactionId: x.transactionId});})
    alert(json);
  } else {
    alert("操作失败\n" + JSON.stringify(transactions));
  }
});
alert("已设置监听器");

/**
transactions 返回交易信息数组
[{
  state:                 //交易状态,详见交易状态常量,数字类型    
  transactionId:         //交易id,字符串类型    
  originalTransactionId: //原始交易id,只在state为恢复购买时有效,字符串类型
  transactionDate:       //交易时间,UNIX 时间戳(秒)
  receipt:               //交易凭证,经过base64编码,用于验证交易是否合法和有效,避免因越狱破解内购后造成损失,只在state为购买成功时有效,字符串类型
  errorCode:             //交易失败时的错误码,数字类型    
  errorMsg:              //交易失败时的说明,字符串类型    
  payment:               //商品购买信息
  {
    productId:           //商品ID,字符串类型
    quantity:            //购买数量,数字类型
    applicationUsername: //用户信息,字符串类型
  },
  downloads:             //有下载内容时的下载信息列表
  [{    
    state:               //下载状态,详见下载状态常量,数字类型
    transactionId:       //下载内容所属交易id,字符串类型
    contentId:           //下载内容id,字符串类型
    progress:            //下载进度,取值范围0~1,数字类型
    contentLength:       //文件内容大小,数字类型
    timeRemaining:       //下载剩余时间,-1时表示未知,数字类型
    contentVersion:      //下载内容的版本,字符串类型
    contentURL:          //下载成功后文件路径,字符串类型
    errorCode:           //下载失败时的错误码,数字类型
    errorMsg:            //下载失败时的错误描述,字符串类型
  }]
}]

state(transaction) 交易状态常量说明
0: 已加入交易队列(Transaction is being added to the server queue.)
1: 交易完成(Transaction is in queue, user has been charged.  Client should complete the transaction.)
2: 交易失败(Transaction was cancelled or failed before being added to the server queue.)
3: 恢复购买(Transaction was restored from user's purchase history.  Client should complete the transaction.)
4: 交易等待被确认(The transaction is in the queue, but its final status is pending external action.)
    
state(download) 下载状态常量说明
0: 等待下载(Download is inactive, waiting to be downloaded)
1: 正在下载(Download is actively downloading)
2: 暂停下载(Download was paused by the user)
3: 下载完成(Download is finished, content is available)
4: 下载失败(Download failed)
5: 取消下载(Download was cancelled)

**/

A标签执行JS示例代码

<a href="javascript:void(0)" onclick="zhixing()">执行</a> 
<!-- 写入一个A标签href值为"javascript:void(0)",定义点击事件onclick;-->
<script src="您的服务器URL/jsbridge-mini.js"></script>
<!-- 在您的服务器引入一门JS地址,请下载jsbridge-mini.js上传您自己的服务器获取链接;-->
<script type="text/javascript">
function zhixing() {


<!--这里可以直接复制JS核心代码到此{}括号内-->
jsBridge.iap.setTransanctionListener(function(succ, transactions) {
  if (succ) {
    var json = JSON.stringify(transactions);
    jsBridge.setClipboardText(json);
    //transactions.forEach(function(x){jsBridge.iap.finishTransaction({transactionId: x.transactionId});})
    alert(json);
  } else {
    alert("操作失败\n" + JSON.stringify(transactions));
  }
});
alert("已设置监听器");

/**
transactions 返回交易信息数组
[{
  state:                 //交易状态,详见交易状态常量,数字类型    
  transactionId:         //交易id,字符串类型    
  originalTransactionId: //原始交易id,只在state为恢复购买时有效,字符串类型
  transactionDate:       //交易时间,UNIX 时间戳(秒)
  receipt:               //交易凭证,经过base64编码,用于验证交易是否合法和有效,避免因越狱破解内购后造成损失,只在state为购买成功时有效,字符串类型
  errorCode:             //交易失败时的错误码,数字类型    
  errorMsg:              //交易失败时的说明,字符串类型    
  payment:               //商品购买信息
  {
    productId:           //商品ID,字符串类型
    quantity:            //购买数量,数字类型
    applicationUsername: //用户信息,字符串类型
  },
  downloads:             //有下载内容时的下载信息列表
  [{    
    state:               //下载状态,详见下载状态常量,数字类型
    transactionId:       //下载内容所属交易id,字符串类型
    contentId:           //下载内容id,字符串类型
    progress:            //下载进度,取值范围0~1,数字类型
    contentLength:       //文件内容大小,数字类型
    timeRemaining:       //下载剩余时间,-1时表示未知,数字类型
    contentVersion:      //下载内容的版本,字符串类型
    contentURL:          //下载成功后文件路径,字符串类型
    errorCode:           //下载失败时的错误码,数字类型
    errorMsg:            //下载失败时的错误描述,字符串类型
  }]
}]

state(transaction) 交易状态常量说明
0: 已加入交易队列(Transaction is being added to the server queue.)
1: 交易完成(Transaction is in queue, user has been charged.  Client should complete the transaction.)
2: 交易失败(Transaction was cancelled or failed before being added to the server queue.)
3: 恢复购买(Transaction was restored from user's purchase history.  Client should complete the transaction.)
4: 交易等待被确认(The transaction is in the queue, but its final status is pending external action.)
    
state(download) 下载状态常量说明
0: 等待下载(Download is inactive, waiting to be downloaded)
1: 正在下载(Download is actively downloading)
2: 暂停下载(Download was paused by the user)
3: 下载完成(Download is finished, content is available)
4: 下载失败(Download failed)
5: 取消下载(Download was cancelled)

**/

}
</script>
<!--执行核心代码;
//如果需要进入页面就执行,去掉点击事件即可;-->

按钮执行JS示例代码

<button onclick="zhixing()" >执行</button>
<!-- //写一个按钮,定义点击执行事件; -->
<script src="您的服务器URL/jsbridge-mini.js"></script>
<!-- //在您的服务器引入一门JS地址,请下载jsbridge-mini.js上传您自己的服务器获取链接; -->
<script type="text/javascript">
function zhixing() {


<!--这里可以直接复制JS核心代码到此{}括号内-->
jsBridge.iap.setTransanctionListener(function(succ, transactions) {
  if (succ) {
    var json = JSON.stringify(transactions);
    jsBridge.setClipboardText(json);
    //transactions.forEach(function(x){jsBridge.iap.finishTransaction({transactionId: x.transactionId});})
    alert(json);
  } else {
    alert("操作失败\n" + JSON.stringify(transactions));
  }
});
alert("已设置监听器");

/**
transactions 返回交易信息数组
[{
  state:                 //交易状态,详见交易状态常量,数字类型    
  transactionId:         //交易id,字符串类型    
  originalTransactionId: //原始交易id,只在state为恢复购买时有效,字符串类型
  transactionDate:       //交易时间,UNIX 时间戳(秒)
  receipt:               //交易凭证,经过base64编码,用于验证交易是否合法和有效,避免因越狱破解内购后造成损失,只在state为购买成功时有效,字符串类型
  errorCode:             //交易失败时的错误码,数字类型    
  errorMsg:              //交易失败时的说明,字符串类型    
  payment:               //商品购买信息
  {
    productId:           //商品ID,字符串类型
    quantity:            //购买数量,数字类型
    applicationUsername: //用户信息,字符串类型
  },
  downloads:             //有下载内容时的下载信息列表
  [{    
    state:               //下载状态,详见下载状态常量,数字类型
    transactionId:       //下载内容所属交易id,字符串类型
    contentId:           //下载内容id,字符串类型
    progress:            //下载进度,取值范围0~1,数字类型
    contentLength:       //文件内容大小,数字类型
    timeRemaining:       //下载剩余时间,-1时表示未知,数字类型
    contentVersion:      //下载内容的版本,字符串类型
    contentURL:          //下载成功后文件路径,字符串类型
    errorCode:           //下载失败时的错误码,数字类型
    errorMsg:            //下载失败时的错误描述,字符串类型
  }]
}]

state(transaction) 交易状态常量说明
0: 已加入交易队列(Transaction is being added to the server queue.)
1: 交易完成(Transaction is in queue, user has been charged.  Client should complete the transaction.)
2: 交易失败(Transaction was cancelled or failed before being added to the server queue.)
3: 恢复购买(Transaction was restored from user's purchase history.  Client should complete the transaction.)
4: 交易等待被确认(The transaction is in the queue, but its final status is pending external action.)
    
state(download) 下载状态常量说明
0: 等待下载(Download is inactive, waiting to be downloaded)
1: 正在下载(Download is actively downloading)
2: 暂停下载(Download was paused by the user)
3: 下载完成(Download is finished, content is available)
4: 下载失败(Download failed)
5: 取消下载(Download was cancelled)

**/

}
</script>
<!-- //执行核心代码;
//如果需要进入页面就执行,去掉点击事件即可; -->

编辑:yimen,如若转载,请注明出处:https://www.yimenapp.com/kb-yimen/14616/

部分内容来自网络投稿,如有侵权联系立删

(0)
上一篇 2022年12月6日 下午6:04
下一篇 2022年12月6日 下午6:10

相关推荐