CocosCreator H5 微信内置浏览器调起微信支付
先来看看效果图
微信公众平台相关文档传送门https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
下面来实现h5支付的功能(使用的CocosCreator)
- 引用jssdk.js文件
在构建的web-mobile/index.html中添加
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
- 初始化jssdk
//这里的ticket是公众号用于调用微信JS接口的临时票据,正常情况下,ticket的有效期为7200秒,通过access_token来获取。由于获取ticket的api调用次数非常有限,频繁刷新ticket会导致api调用受限,影响自身业务,开发者必须在自己的服务全局缓存ticket 。
//这个ticket需要让服务器提供
//顺便在里面加了一个自定义分享的实现
initConfig : function (ticket) {
var CryptoJS = CryptoJS || function (e, m) {
var p = {}, j = p.lib = {}, l = function () {
}, f = j.Base = { extend: function (a) {
l.prototype = this; var c = new l; a && c.mixIn(a); c.hasOwnProperty("init") || (c.init = function () {
c.$super.init.apply(this, arguments) }); c.init.prototype = c; c.$super = this; return c }, create: function () {
var a = this.extend(); a.init.apply(a, arguments); return a }, init: function () {
}, mixIn: function (a) {
for (var c in a) a.hasOwnProperty(c) && (this[c] = a[c]); a.hasOwnProperty("toString") && (this.toString = a.toString) }, clone: function () {
return this.init.prototype.extend(this) } },
n = j.WordArray = f.extend({
init: function (a, c) {
a = this.words = a || []; this.sigBytes = c != m ? c : 4 * a.length }, toString: function (a) {
return (a || h).stringify(this) }, concat: function (a) {
var c = this.words, q = a.words, d = this.sigBytes; a = a.sigBytes; this.clamp(); if (d % 4) for (var b = 0; b < a; b++)c[d + b >>> 2] |= (q[b >>> 2] >>> 24 - 8 * (b % 4) & 255) << 24 - 8 * ((d + b) % 4); else if (65535 < q.length) for (b = 0; b < a; b += 4)c[d + b >>> 2] = q[b >>> 2]; else c.push.apply(c, q); this.sigBytes += a; return this }, clamp: function () {
var a = this.words, c = this.sigBytes; a[c >>> 2] &= 4294967295 <<
32 - 8 * (c % 4); a.length = e.ceil(c / 4)
}, clone: function () {
var a = f.clone.call(this); a.words = this.words.slice(0); return a }, random: function (a) {
for (var c = [], b = 0; b < a; b += 4)c.push(4294967296 * e.random() | 0); return new n.init(c, a) }
}), b = p.enc = {}, h = b.Hex = {
stringify: function (a) {
var c = a.words; a = a.sigBytes; for (var b = [], d = 0; d < a; d++) { var f = c[d >>> 2] >>> 24 - 8 * (d % 4) & 255; b.push((f >>> 4).toString(16)); b.push((f & 15).toString(16)) } return b.join("") }, parse: function (a) {
for (var c = a.length, b = [], d = 0; d < c; d += 2)b[d >>> 3] |= parseInt(a.substr(d,
2), 16) << 24 - 4 * (d % 8); return new n.init(b, c / 2)
}
}, g = b.Latin1 = { stringify: function (a) {
var c = a.words; a = a.sigBytes; for (var b = [], d = 0; d < a; d++)b.push(String.fromCharCode(c[d >>> 2] >>> 24 - 8 * (d % 4) & 255)); return b.join("") }, parse: function (a) {
for (var c = a.length, b = [], d = 0; d < c; d++)b[d >>> 2] |= (a.charCodeAt(d) & 255) << 24 - 8 * (d % 4); return new n.init(b, c) } }, r = b.Utf8 = { stringify: function