FqPay.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * 凤起充值
  3. */
  4. import { __PayData } from "../game/data/sdata/SManage";
  5. import FqLogin from "../login/FqLogin";
  6. import Main from "../main/Main";
  7. import { HttpStateType, ReveData } from "../util/CHttp";
  8. /**
  9. * 友盟 广告事件定义
  10. */
  11. export const UM_EVENT_ID = cc.Enum({
  12. ad_pdl_t_0: 'ad_pdl_t_0',//潘多拉铁盒广告开始
  13. ad_pdl_t_1: 'ad_pdl_t_1',//潘多拉铁盒广告观看结束
  14. ad_pdl_m_0: 'ad_pdl_m_0',//潘多拉魔盒广告开始
  15. ad_pdl_m_1: 'ad_pdl_m_1',//潘多拉魔盒广告观看结束
  16. ad_power_0: 'ad_power_0',//看广告购买体力
  17. ad_power_1: 'ad_power_1',//看广告购买体力
  18. ad_money_0: 'ad_money_0',//看广告购买金币
  19. ad_money_1: 'ad_money_1',//看广告购买金币
  20. ad_fuhuo_0: 'ad_fuhuo_0',//复活
  21. ad_fuhuo_1: 'ad_fuhuo_1',//复活
  22. ad_color_2_0: 'ad_color_2_0',//2关颜色灯
  23. ad_color_2_1: 'ad_color_2_1',//2关颜色灯
  24. ad_color_5_0: 'ad_color_5_0',//5关颜色灯
  25. ad_color_5_1: 'ad_color_5_1',//5关颜色灯
  26. ad_wish_0: 'ad_wish_0',//每日祝福
  27. ad_wish_1: 'ad_wish_1',//每日祝福
  28. });
  29. export default class FqPay {
  30. public main: Main;
  31. public _payData: __PayData;
  32. /**
  33. * 充值回调
  34. * result : 0 成功 -1 失败
  35. */
  36. public callback: (result: number) => void;
  37. private orderId:number;//订单id
  38. constructor(main: Main) {
  39. this.main = main
  40. }
  41. /**
  42. *
  43. * @param id 商品id
  44. */
  45. public pay(payData: __PayData, callback: (result: number) => void) {
  46. this.callback = callback
  47. this._payData = payData
  48. //创建订单
  49. this.createOrder((orderId) => {
  50. this.orderId = orderId
  51. this.openFqPay('' + orderId)
  52. })
  53. }
  54. /**
  55. * 创建订单
  56. * @param callback
  57. */
  58. private createOrder(callback: (orderId: number) => void) {
  59. let msg = {
  60. payId: this._payData.id
  61. }
  62. this.main.gameHttp.sendJson('pay/v1/createOrder', msg, (state, reve: ReveData) => {
  63. this.main.stopLoad();
  64. if (state == HttpStateType.SUCCESS) {
  65. if (reve.retCode == 0) {
  66. callback(reve.data.orderId)
  67. } else {
  68. this.main.showTips(reve.message);
  69. }
  70. } else {
  71. this.main.showTips('网络异常');
  72. }
  73. });
  74. }
  75. /**
  76. * String productCode = json.getString("productCode");
  77. String orderAmount = json.getString("orderAmount");
  78. String subject = json.getString("subject");
  79. String roleName = json.getString("roleName");
  80. String cpBillNo = json.getString("cpBillNo");
  81. String serverId = json.getString("serverId");
  82. String extraInfo = json.getString("extraInfo");
  83. paymentInfo.setOrderAmount(orderAmount); //订单金额 必须字段
  84. paymentInfo.setSubject(subject); //商品名 必须字段
  85. paymentInfo.setRoleName(roleName); //角色名 必须字段
  86. paymentInfo.setCpBillNo(cpBillNo); //CP订单号 必须字段
  87. paymentInfo.setUid(uid); //登录时成功拿到的UID 必须字段
  88. paymentInfo.setServerId(serverId); //区服ID 必须字段
  89. paymentInfo.setExtraInfo(extraInfo); //拓展信息 必须字段
  90. * @param orderId
  91. */
  92. private openFqPay(orderId: string) {
  93. let player = this.main.player
  94. let user = this.main.userData
  95. let msg = {
  96. productCode:''+this._payData.id,
  97. productCodeAndroid:this._payData.googleId,
  98. productCodeIos:this._payData.iosId,
  99. orderAmount:''+this._payData.usd,
  100. subject:''+this._payData.name,
  101. roleName:player.role.name,
  102. cpBillNo:''+orderId,
  103. serverId:''+user.zone.id,
  104. extraInfo:''+orderId
  105. }
  106. if(cc.sys.isNative){
  107. if (cc.sys.os === cc.sys.OS_ANDROID) {
  108. let myself = this
  109. window._fqAndroidPayCallback = (str) => {
  110. console.log('window._fqAndroidPayCallback', str)
  111. myself.getPay()
  112. return '1'
  113. }
  114. let className = "com/funqueue/android/xsdmx/MainActivity";
  115. let methodName = "pay";
  116. let methodSignature = "(Ljava/lang/String;)V";
  117. jsb.reflection.callStaticMethod(className, methodName, methodSignature, JSON.stringify(msg));
  118. } else if (cc.sys.os === cc.sys.OS_IOS || cc.sys.os === cc.sys.OS_OSX) {
  119. let myself = this
  120. window._fqIosPayCallback = (str)=>{
  121. console.log('window._fqIosPayCallback' , str)
  122. myself.getPay()
  123. return '1'
  124. }
  125. jsb.reflection.callStaticMethod('AppController', 'pay:',JSON.stringify(msg));
  126. }
  127. }
  128. }
  129. private getPay() {
  130. let msg = {
  131. orderId: this.orderId
  132. }
  133. this.main.gameHttp.sendJson('pay/v1/checkPay', msg, (state, reve: ReveData) => {
  134. this.main.stopLoad();
  135. if (state == HttpStateType.SUCCESS) {
  136. if (reve.retCode == 0) {
  137. this.callback(0)
  138. this.main.showReward(reve)
  139. } else {
  140. this.main.showTips(reve.message);
  141. }
  142. } else {
  143. this.main.showTips('网络异常');
  144. }
  145. });
  146. }
  147. /**
  148. * 用户观看视频
  149. * result : -1:广告还未准备好
  150. * 0:广告未播放完
  151. * 1:播放完成,给奖励
  152. */
  153. public adVideo(callback:(result: number)=>void,eventId1:string,eventId2:string){
  154. cc.log('用户观看视频 adVideo');
  155. this.callback = callback;
  156. if(cc.sys.isNative){
  157. if (cc.sys.os === cc.sys.OS_ANDROID) {
  158. FqLogin.commitEvent(eventId1,'','')
  159. let myself = this
  160. window._fqAndroidAdCallback = (str) => {
  161. console.log('window._fqAndroidAdCallback', str)
  162. if(str == '1'){
  163. myself.callback(1)
  164. FqLogin.commitEvent(eventId2,'','')
  165. }else if(str == '-1'){
  166. myself.callback(-1)
  167. }else{
  168. myself.callback(0)
  169. }
  170. return '1'
  171. }
  172. let className = "com/funqueue/android/xsdmx/MainActivity";
  173. let methodName = "showRewardedVideo";
  174. let methodSignature = "(Ljava/lang/String;)V";
  175. jsb.reflection.callStaticMethod(className, methodName, methodSignature, ' jlsp1');
  176. } else if (cc.sys.os === cc.sys.OS_IOS) {
  177. FqLogin.commitEvent(eventId1,'','')
  178. let myself = this
  179. window._fqIosAdCallback = (str)=>{
  180. console.log('window._fqIosAdCallback' , str)
  181. if(str == '1'){
  182. FqLogin.commitEvent(eventId2,'','')
  183. myself.callback(1)
  184. }else if(str == '-1'){
  185. myself.callback(-1)
  186. }else{
  187. myself.callback(0)
  188. }
  189. return '1'
  190. }
  191. jsb.reflection.callStaticMethod('AppController', 'showRewardedVideo:','jlsp1');
  192. }else{
  193. this.callback(1)
  194. }
  195. }else{
  196. this.callback(1)
  197. }
  198. }
  199. }