PHP code example of zhangv / wechat-pay

1. Go to this page and download the library: Download zhangv/wechat-pay library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zhangv / wechat-pay example snippets


$cfg = [
    'mch_id'            => 'XXXX', //商户号
    'app_id'            => 'XXXXXXXXXXXXXXXXXXX', //APPID
    'app_secret'        => 'XXXXXXXXXXXXXXXXXXXXXXXXX', //App Secret
    'api_key'           =>'XXXXXXXXXXXXXXXXXXXXXXX', //支付密钥
    'ssl_cert_path'     => __DIR__ . '/../cert/apiclient_cert.pem',
    'ssl_key_path'      => __DIR__ .'/../cert/apiclient_key.pem',
    'sign_type'         => 'MD5',
    'notify_url'        => 'http://XXX.XXX/paidnotify.php',
    'refund_notify_url' => 'http://XXX.XXX/refundednotify.php',
    'h5_scene_info'     => [//

$wp = WechatPay::Jsapi($cfg);
$prepayId = $wp->getPrepayId($body, $orderNo, $amt, $openid);
$package = $wp->getPackage($prepayId);


$wp = WechatPay::Weapp($cfg);
$prepayId = $wp->getPrepayId($body, $orderNo, $amt, $openid);
$package = $wp->getPackage($prepayId);


$wp = WechatPay::App($cfg);
$prepayId = $wp->getPrepayId($body, $orderNo, $amt, $openid);
$package = $wp->getPackage($prepayId);


$wp = WechatPay::Mweb($cfg);
$url = $wp->getMwebUrl($body,$orderNo,$amt);


$wp = WechatPay::Native($cfg);
$url = $wp->getCodeUrl($body,$out_trade_no,$total_fee,$product_id);


$wp = WechatPay::Micro($cfg);
$url = $wp->microPay($body,$out_trade_no,$total_fee,$spbill_create_ip,$auth_code);


$notifyxml = file_get_contents("php://input");
$wp = new WechatPay($cfg);
$msg = $wp->onPaidNotify($notifyxml, function($notifyArray){
    //处理逻辑
    return 'ok';
});
$xml = "<xml>
           <return_code>SUCCESS</return_code>
           <return_msg><![CDATA[$msg]]></return_msg>
        </xml>";
echo $xml;

$wxpay = new WechatPay($cfg);
$result = $wxpay->refundByOutTradeNo($out_trade_no,$out_refund_no,$total_fee,$refund_fee);


$wp = WechatPay::Mchpay($cfg);
//付款到零钱
$result = $wp->transferWallet($partner_trade_no,$openid,$amount,$desc);
//付款到银行卡
$result = $wp->transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc);

$wp = WechatPay::Redpack($cfg);
//发放普通红包
$result = $wp->sendRedPack($mch_billno,$send_name,$re_openid,$total_amount,$total_num,$wishing,$act_name,$remark);
//发放裂变红包
$result = $wp->sendGroupRedPack($mch_billno,$send_name,$re_openid,$total_amount,$total_num,$wishing,$act_name,$remark);


$wp = WechatPay::Coupon($cfg);
//发放代金券
$result = $wp->sendCoupon($coupon_stock_id,$open_id,$partner_trade_no);
//查询代金券批次
$result = $wp->queryCouponStock($coupon_stock_id);