jq 微信公众支付前端代码

function pay(){
        $.get({
            url:"{{url('wechat/pay_vip')}}",
            dataType:'json',
            success:function (json) {
                var WXPayment = function() {
                    if( typeof WeixinJSBridge === 'undefined' ) {
                        alert('稍等一下!');
                        return false;
                    }
                    WeixinJSBridge.invoke(
                        'getBrandWCPayRequest',{"appId":json.appId,"timeStamp":json.timeStamp,"nonceStr":json.nonceStr,"package":json.package,"signType":json.signType,"paySign":json.paySign}, function(res) {
                            switch(res.err_msg) {
                                case 'get_brand_wcpay_request:cancel':
                                    alert('用户取消支付!');
                                    break;
                                case 'get_brand_wcpay_request:fail':
                                    alert('支付失败!('+res.err_desc+')');
                                    break;
                                case 'get_brand_wcpay_request:ok':
                                    $('.container').hide();
                                    $('.caocuo_success').show();
                                    break;
                                default:
                                    alert(JSON.stringify(res));
                                    break;
                            }
                        }
                    );
                };
                WXPayment();
            }
        })
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 `jQuery` 和 `Ajax` 实现微信公众号模板消息的发送。 以下是一个简单的示例: 1. HTML 页面中创建一个表单,包含发送模板消息所需的参数: ```html <form id="send-form"> <input type="hidden" name="openid" value="..."> <input type="hidden" name="template_id" value="..."> <input type="hidden" name="url" value="..."> <input type="hidden" name="data" value='{"key1": {"value": "...", "color": "#173177"}, "key2": {"value": "...", "color": "#173177"}}'> <button type="submit">发送模板消息</button> </form> ``` 2. 使用 `jQuery` 监听表单的提交事件,发送 `Ajax` 请求: ```javascript $(document).on('submit', '#send-form', function(e) { e.preventDefault(); // 阻止表单默认提交行为 $.ajax({ url: 'send.php', // 发送模板消息的 PHP 接口地址 type: 'POST', data: $(this).serialize(), dataType: 'json', success: function(resp) { if (resp.errcode === 0) { alert('发送成功'); } else { alert('发送失败:' + resp.errmsg); } }, error: function(xhr, status, err) { alert('发送失败:' + err); } }); }); ``` 3. 在服务器端编写 `send.php` 接口,使用 `curl` 发送模板消息: ```php <?php $access_token = 'YOUR_ACCESS_TOKEN'; // 替换为实际的 access_token $openid = $_POST['openid']; $template_id = $_POST['template_id']; $url = $_POST['url']; $data = json_decode($_POST['data'], true); $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token; $post_data = [ 'touser' => $openid, 'template_id' => $template_id, 'url' => $url, 'data' => $data, ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); echo $response; ``` 注意:您需要替换 `$access_token` 为实际的 access_token。此外,上述示例只是一个简单的参考,实际应用中还需要进行参数验证、错误处理等操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值