Skip to content

Latest commit

 

History

History
142 lines (115 loc) · 4.4 KB

README.md

File metadata and controls

142 lines (115 loc) · 4.4 KB

WechatPay - 微信支付

Latest Stable Version License Build Status codecov Scrutinizer Code Quality Author

simplest, minimal dependency

Feature

  • 使用简单
  • 支持php-7x
  • 不依赖第三方库(但依赖json、simpleXML、openssl、curl扩展)
  • 100%测试覆盖
  • 支持所有付款方式(公众号、APP、小程序、H5、扫码、刷卡)
  • 支持企业付款、红包、代金券

Install

composer require zhangv/wechat-pay

Demo

  • 配置参数
$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证书(通过商户后台下载)
    'ssl_key_path'      => __DIR__ .'/../cert/apiclient_key.pem', //SSL密钥(通过商户后台下载)
    'sign_type'         => 'MD5',
    'notify_url'        => 'http://XXX.XXX/paidnotify.php', //支付结果通知地址
    'refund_notify_url' => 'http://XXX.XXX/refundednotify.php', //退款结果通知地址
    'h5_scene_info'     => [//H5支付时需要
        'h5_info' => ['type' => 'Wap', 'wap_url' => 'http://wapurl', 'wap_name' => 'wapname']
    ],
    'rsa_pubkey_path'   => __DIR__ .'/../cert/pubkey.pem',
    'jsapi_ticket'      => __DIR__ .'/jsapi_ticket.json' //jsticket的临时存放路径
];
  • 获取公众号支付参数(公众号网页支付)
$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);
  • 获取APP的支付参数(APP支付)
$wp = WechatPay::App($cfg);
$prepayId = $wp->getPrepayId($body, $orderNo, $amt, $openid);
$package = $wp->getPackage($prepayId);
  • 获取H5支付URL(H5支付)
$wp = WechatPay::Mweb($cfg);
$url = $wp->getMwebUrl($body,$orderNo,$amt);
  • 获取扫码支付URL(扫码支付)
$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);