| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- /**
- *
- * example目录下为简单的支付样例,仅能用于搭建快速体验微信支付使用
- * 样例的作用仅限于指导如何使用sdk,在安全上面仅做了简单处理, 复制使用样例代码时请慎重
- * 请勿直接直接使用样例对外提供服务
- *
- **/
- $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
- require_once($wx . '/lib/WxPay.Api.php');
- require_once($wx . '/example/WxPay.Config.php');
- /**
- *
- * 刷卡支付实现类
- * 该类实现了一个刷卡支付的流程,流程如下:
- * 1、提交刷卡支付
- * 2、根据返回结果决定是否需要查询订单,如果查询之后订单还未变则需要返回查询(一般反复查10次)
- * 3、如果反复查询10订单依然不变,则发起撤销订单
- * 4、撤销订单需要循环撤销,一直撤销成功为止(注意循环次数,建议10次)
- *
- * 该类是微信支付提供的样例程序,商户可根据自己的需求修改,或者使用lib中的api自行开发,为了防止
- * 查询时hold住后台php进程,商户查询和撤销逻辑可在前端调用
- *
- * @author widy
- *
- */
- class MicroPay
- {
- /**
- *
- * 提交刷卡支付,并且确认结果,接口比较慢
- * @param WxPayMicroPay $microPayInput
- * @throws WxpayException
- * @return 返回查询接口的结果
- */
- public function pay($microPayInput,$sjExtend)
- {
- //①、提交被扫支付
- $config = new WxPayConfig();
- $mid = $sjExtend['wxPayMerchantId'] ?? '';
- $appId = $sjExtend['miniAppId'] ?? '';
- $key = $sjExtend['wxPayKey'] ?? '';
- $config->SetAppId($appId);
- $config->SetMerchantId($mid);
- $config->SetKey($key);
- $result = WxPayApi::micropay($config, $microPayInput, 5);
- return $result;
- }
-
- /**
- *
- * 查询订单情况
- * @param string $out_trade_no 商户订单号
- * @param int $succCode 查询订单结果
- * @return 0 订单不成功,1表示订单成功,2表示继续等待
- */
- public function query($out_trade_no, $sjExtend)
- {
- $queryOrderInput = new WxPayOrderQuery();
- $queryOrderInput->SetOut_trade_no($out_trade_no);
- $config = new WxPayConfig();
- $mid = $sjExtend['wxPayMerchantId'] ?? '';
- $appId = $sjExtend['miniAppId'] ?? '';
- $key = $sjExtend['wxPayKey'] ?? '';
- $config->SetAppId($appId);
- $config->SetMerchantId($mid);
- $config->SetKey($key);
- try{
- $result = WxPayApi::orderQuery($config, $queryOrderInput);
- return $result;
- } catch(Exception $e) {
- //Log::ERROR(json_encode($e));
- }
- }
-
- /**
- *
- * 撤销订单,如果失败会重复调用10次
- * @param string $out_trade_no
- * @param 调用深度 $depth
- */
- public function cancel($out_trade_no, $depth = 0)
- {
- try {
- if($depth > 10){
- return false;
- }
-
- $clostOrder = new WxPayReverse();
- $clostOrder->SetOut_trade_no($out_trade_no);
- $config = new WxPayConfig();
- $result = WxPayApi::reverse($config, $clostOrder);
-
- //接口调用失败
- if($result["return_code"] != "SUCCESS"){
- return false;
- }
-
- //如果结果为success且不需要重新调用撤销,则表示撤销成功
- if($result["result_code"] != "SUCCESS"
- && $result["recall"] == "N"){
- return true;
- } else if($result["recall"] == "Y") {
- return $this->cancel($out_trade_no, ++$depth);
- }
- } catch(Exception $e) {
- Log::ERROR(json_encode($e));
- }
- return false;
- }
- }
|