Esempio n. 1
0
 /**
  *
  * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  * appid、mchid、spbill_create_ip、nonce_str不需要填入
  *
  * @param WxPayUnifiedOrder $inputObj
  * @param int $timeOut
  * @throws WxPayException
  * @return 成功时返回,其他抛异常
  */
 public static function unifiedOrder($inputObj, $timeOut = 30)
 {
     $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
     // 检测必填参数
     if (!$inputObj->IsAppidSet()) {
         throw new WxPayException('缺少公众号APPID!');
     } elseif (!$inputObj->IsMch_idSet()) {
         throw new WxPayException('缺少商户号!');
     } elseif (!$inputObj->IsOut_trade_noSet()) {
         throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
     } elseif (!$inputObj->IsBodySet()) {
         throw new WxPayException("缺少统一支付接口必填参数body!");
     } elseif (!$inputObj->IsTotal_feeSet()) {
         throw new WxPayException("缺少统一支付接口必填参数total_fee!");
     } elseif (!$inputObj->IsTrade_typeSet()) {
         throw new WxPayException("缺少统一支付接口必填参数trade_type!");
     }
     // 关联参数
     if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet() && !$inputObj->IsSub_openidSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
     }
     if ($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
     }
     // 签名
     $inputObj->SetSign();
     $xml = $inputObj->ToXml();
     $startTimeStamp = self::getMillisecond();
     // 请求开始时间
     $response = self::postXmlCurl($xml, $url, false, $timeOut);
     $result = WxPayResults::Init($response, $inputObj->wx_config);
     self::reportCostTime($url, $startTimeStamp, $result);
     // 上报请求花费时间
     return $result;
 }
Esempio n. 2
0
File: Api.php Progetto: ljimmy/pay
 /**
  *
  * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  * appid、mchid、spbill_create_ip、nonce_str不需要填入
  * @param WxPayUnifiedOrder $inputObj
  * @param int $timeOut
  * @throws Exception
  * @return 成功时返回,其他抛异常
  */
 public static function unifiedOrder($inputObj, $timeOut = 6)
 {
     $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
     //检测必填参数
     if (!$inputObj->IsOut_trade_noSet()) {
         throw new Exception("缺少统一支付接口必填参数out_trade_no!");
     } else {
         if (!$inputObj->IsBodySet()) {
             throw new Exception("缺少统一支付接口必填参数body!");
         } else {
             if (!$inputObj->IsTotal_feeSet()) {
                 throw new Exception("缺少统一支付接口必填参数total_fee!");
             } else {
                 if (!$inputObj->IsTrade_typeSet()) {
                     throw new Exception("缺少统一支付接口必填参数trade_type!");
                 }
             }
         }
     }
     //关联参数
     if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()) {
         throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
     }
     if ($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()) {
         throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为NATIVE时,product_id为必填参数!");
     }
     //异步通知url未设置,则使用配置文件中的url
     if (!$inputObj->IsNotify_urlSet()) {
         $inputObj->SetNotify_url(Config::NOTIFY_URL);
         //异步通知url
     }
     $inputObj->SetAppid(Config::APPID);
     //公众账号ID
     $inputObj->SetMch_id(Config::MCHID);
     //商户号
     if (!$inputObj->IsSpbill_create_ipSet()) {
         $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);
         //终端ip
     }
     //$inputObj->SetSpbill_create_ip("1.1.1.1");
     $inputObj->SetNonce_str(self::getNonceStr());
     //随机字符串
     //签名
     $inputObj->SetSign();
     $xml = $inputObj->ToXml();
     $startTimeStamp = self::getMillisecond();
     //请求开始时间
     $response = self::postXmlCurl($xml, $url, false, $timeOut);
     $result = Results::Init($response);
     self::reportCostTime($url, $startTimeStamp, $result);
     //上报请求花费时间
     if ($result['return_code'] != 'SUCCESS') {
         throw new Exception($result['return_msg']);
     }
     if ($result['result_code'] != 'SUCCESS') {
         throw new Exception('err_code:' . $result['err_code'] . ';err_code_des:' . $result['err_code_des']);
     }
     return $result;
 }
Esempio n. 3
0
 /**
  * 
  * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  * appid、mchid、spbill_create_ip、nonce_str不需要填入
  * @param WxPayUnifiedOrder $inputObj
  * @param int $timeOut
  * @throws WxPayException
  * @return 成功时返回,其他抛异常
  */
 public function unifiedOrder(WxPayUnifiedOrder $inputObj, $timeOut = 6)
 {
     $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
     //检测必填参数
     if (!$inputObj->IsOut_trade_noSet()) {
         throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
     } else {
         if (!$inputObj->IsBodySet()) {
             throw new WxPayException("缺少统一支付接口必填参数body!");
         } else {
             if (!$inputObj->IsTotal_feeSet()) {
                 throw new WxPayException("缺少统一支付接口必填参数total_fee!");
             } else {
                 if (!$inputObj->IsTrade_typeSet()) {
                     throw new WxPayException("缺少统一支付接口必填参数trade_type!");
                 }
             }
         }
     }
     //关联参数
     if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
     }
     if ($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
     }
     //异步通知url未设置,则使用配置文件中的url
     if (!$inputObj->IsNotify_urlSet()) {
         $inputObj->SetNotify_url($this->wxPayConfig['NOTIFY_URL']);
         //异步通知url
     }
     $inputObj->SetAppid($this->wxPayConfig['APPID']);
     //公众账号ID
     $inputObj->SetMch_id($this->wxPayConfig['MCHID']);
     //商户号
     try {
         $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);
         //终端ip
     } catch (\Exception $e) {
         $inputObj->SetSpbill_create_ip("1.1.1.1");
     }
     $inputObj->SetNonce_str(self::getNonceStr());
     //随机字符串
     //签名
     $inputObj->SetSign();
     $xml = $inputObj->ToXml();
     $startTimeStamp = self::getMillisecond();
     //请求开始时间
     $response = $this->postXmlCurl($xml, $url, false, $timeOut);
     //		print_r($response);
     $result = WxPayResults::Init($response, $this->wxPayConfig);
     $this->reportCostTime($url, $startTimeStamp, $result);
     //上报请求花费时间
     return $result;
 }
Esempio n. 4
0
 /**
  * 
  * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  * appid、mchid、spbill_create_ip、nonce_str不需要填入
  * @param WxPayUnifiedOrder $inputObj
  * @param int $timeOut
  * @throws WxPayException
  * @return 成功时返回,其他抛异常
  */
 public static function unifiedOrder($inputObj, $timeOut = 6)
 {
     $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
     //检测必填参数
     if (!$inputObj->IsOut_trade_noSet()) {
         throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
     } else {
         if (!$inputObj->IsBodySet()) {
             throw new WxPayException("缺少统一支付接口必填参数body!");
         } else {
             if (!$inputObj->IsTotal_feeSet()) {
                 throw new WxPayException("缺少统一支付接口必填参数total_fee!");
             } else {
                 if (!$inputObj->IsTrade_typeSet()) {
                     throw new WxPayException("缺少统一支付接口必填参数trade_type!");
                 }
             }
         }
     }
     //关联参数
     if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
     }
     if ($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
     }
     //异步通知url未设置,则使用配置文件中的url
     if (!$inputObj->IsNotify_urlSet()) {
         $inputObj->SetNotify_url(WxPayConfig::NOTIFY_URL);
         //异步通知url
     }
     $inputObj->SetAppid(WxPayConfig::APPID);
     //公众账号ID
     $inputObj->SetMch_id(WxPayConfig::MCHID);
     //商户号
     $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);
     //终端ip
     //$inputObj->SetSpbill_create_ip("1.1.1.1");
     $inputObj->SetNonce_str(self::getNonceStr());
     //随机字符串
     //签名
     $inputObj->SetSign();
     $xml = $inputObj->ToXml();
     $startTimeStamp = self::getMillisecond();
     //请求开始时间
     $response = self::postXmlCurl($xml, $url, false, $timeOut);
     $result = WxPayResults::Init($response);
     // 统一下单接口返回正常的prepay_id,再按签名规范重新生成签名后,将数据传输给APP。
     // 参与签名的字段名为appId,partnerId,prepayId,nonceStr,timeStamp,package。注意:package的值格式为Sign=WXPay
     $time_stamp = time();
     $pack = 'Sign=WXPay';
     //输出参数列表
     $prePayParams = array();
     $prePayParams['appid'] = $result['appid'];
     $prePayParams['partnerid'] = $result['mch_id'];
     $prePayParams['prepayid'] = $result['prepay_id'];
     $prePayParams['noncestr'] = $result['nonce_str'];
     $prePayParams['package'] = $pack;
     $prePayParams['timestamp'] = $time_stamp;
     //echo json_encode($prePayParams);
     $result = WxPayResults::InitFromArray($prePayParams, true)->GetValues();
     self::reportCostTime($url, $startTimeStamp, $result);
     //上报请求花费时间
     return $result;
 }