123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- require_once "WxPay.Api.php";
- class MicroPay
- {
- public function __construct($config)
- {
- $this->config = $config;
- }
-
- public function pay($microPayInput)
- {
-
- $result = WxPayApi::micropay($this->config, $microPayInput, 5);
-
- if(!array_key_exists("return_code", $result)
- || !array_key_exists("result_code", $result))
- {
- echo "接口调用失败,请确认是否输入是否有误!";
- throw new WxPayException("接口调用失败!");
- }
-
-
- $out_trade_no = $microPayInput->GetOut_trade_no();
-
-
- if($result["return_code"] == "SUCCESS" &&
- $result["result_code"] == "FAIL" &&
- $result["err_code"] != "USERPAYING" &&
- $result["err_code"] != "SYSTEMERROR")
- {
- return false;
- }
-
- $queryTimes = 10;
- while($queryTimes > 0)
- {
- $succResult = 0;
- $queryResult = $this->query($out_trade_no, $succResult);
-
- if($succResult == 2){
- sleep(2);
- continue;
- } else if($succResult == 1){
- return $queryResult;
- } else {
- break;
- }
- }
-
-
- if(!$this->cancel($out_trade_no))
- {
- throw new WxpayException("撤销单失败!");
- }
- return false;
- }
-
-
- public function query($out_trade_no, &$succCode)
- {
- $queryOrderInput = new WxPayOrderQuery();
- $queryOrderInput->SetOut_trade_no($out_trade_no);
- try{
- $result = WxPayApi::orderQuery($this->config, $queryOrderInput);
- } catch(Exception $e) {
- Log::ERROR(json_encode($e));
- }
- if($result["return_code"] == "SUCCESS"
- && $result["result_code"] == "SUCCESS")
- {
-
- if($result["trade_state"] == "SUCCESS"){
- $succCode = 1;
- return $result;
- }
-
- else if($result["trade_state"] == "USERPAYING"){
- $succCode = 2;
- return false;
- }
- }
-
-
- if($result["err_code"] == "ORDERNOTEXIST")
- {
- $succCode = 0;
- } else{
-
- $succCode = 2;
- }
- return false;
- }
-
-
- public function cancel($out_trade_no, $depth = 0)
- {
- try {
- if($depth > 10){
- return false;
- }
-
- $clostOrder = new WxPayReverse();
- $clostOrder->SetOut_trade_no($out_trade_no);
- $result = WxPayApi::reverse($this->config, $clostOrder);
-
-
- if($result["return_code"] != "SUCCESS"){
- return false;
- }
-
-
- 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;
- }
- }
|