当前位置: 首页 > 工具软件 > EasyWeChat > 使用案例 >

easywechat6 微信支付(v3)

孙鑫鹏
2023-12-01
<?php
require 'vendor/autoload.php';

use EasyWeChat\Pay\Application;

$config = [
    'mch_id' => '商户号',

    // 商户证书
    'private_key' => 'D:\webroot\cgsxy\demo\cert\apiclient_key.pem',

    'certificate' => 'D:\webroot\cgsxy\demo\cert\apiclient_cert.pem',

     // v3 API 秘钥
    'secret_key' => '',

    // v2 API 秘钥
    //'v2_secret_key' => '',

    /**
     * 接口请求相关配置,超时时间等,具体可用参数请参考:
     * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php
     */
    'http' => [
        'throw'  => true, // 状态码非 200、300 时是否抛出异常,默认为开启
        'timeout' => 5.0,
        // 'base_uri' => 'https://api.mch.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
    ],
];

$app = new Application($config);

$response = $app->getClient()->postJson("v3/pay/transactions/jsapi", [
   "mchid" => "商户号", // <---- 请修改为您的商户号
   "out_trade_no" => "2059885565478203",
   "appid" => "", // <---- 请修改为服务号的 appid
   "description" => "这是来自屈威的微信支付测试",
   "notify_url" => "http://demo.sohu.com/demo/notify.php",
   "amount" => [
        "total" => 1,
        "currency" => "CNY"
    ],
    "payer" => [
        "openid" => "oXILr5uX73IiEvquefDK1Uy9rka8" // <---- 请修改为服务号下单用户的 openid
    ]
]);


$res = $response->toArray();

// var_dump($res);

$prepayId = $res['prepay_id'];

$utils = $app->getUtils();

$appId = ''; //微信公众号APPID
$signType = 'RSA'; // 默认RSA,v2要传MD5
$config = $utils->buildBridgeConfig($prepayId, $appId, $signType); // 返回数组
?>

<script type="text/javascript">
function jsApiCall()
{
    WeixinJSBridge.invoke(
     'getBrandWCPayRequest', {
       appId:'<?= $appId; ?>',
       timeStamp: "<?= $config['timeStamp'] ?>", //注意 timeStamp 的格式
       nonceStr: "<?= $config['nonceStr'] ?>",
       package: "<?= $config['package'] ?>",
       signType: "<?= $config['signType'] ?>",
       paySign: "<?= $config['paySign'] ?>", // 支付签名
     },
     function (res) {
         // 使用以上方式判断前端返回,微信团队郑重提示:
         // res.err_msg将在用户支付成功后返回
         // ok,但并不保证它绝对可靠。

        WeixinJSBridge.log(res.err_msg);
        //alert(res.err_code+res.err_desc+res.err_msg);
        if(res.err_msg.indexOf("ok")>0){
          alert("付款成功!");
          window.location.href='./';
        }else if(res.err_msg.indexOf("cancel")>0){
          alert("取消付款!");
          window.location.href='./';
        }else{
          alert(res.err_code+res.err_desc+res.err_msg);
        }
     }
    );
}

    function callpay()
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall();
        }
    }
</script>

<button type="button" onClick="callpay()">payment</button>

out_trade_no 每次在测试时,该号一定要变,否则微信服务器会返回错误.....

 类似资料: