- 須在微信支付平台申請開通相應資格
https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_1
2、設置支付平台密鑰和下載安全證書
https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=4_3
3、對照官方文檔,傳遞相應字段,根據官方提供的其他支付DEMO改寫代碼
/** * 企業付款 */ public function transfer($id) { $orderData = ShopOrder::get($id);//獲取訂單 trace($orderData,'wxpayorder'); $mp_id = '******';//區分后台公眾號標識; $appinfo = get_mpid_appinfo ( $mp_id ); //獲取公眾號信息 $cfg = array( 'APPID' => $appinfo['appid'], 'MCHID' => $appinfo['mchid'], 'KEY' => $appinfo['mchkey'], 'APPSECRET' => $appinfo['secret'], 'NOTIFY_URL' => $appinfo['notify_url'], ); WxPayConfig::setConfig($cfg);//設置公眾號信息 if (isset($id) && $id != 0) { //商戶訂單 $outid = $orderData->id.date("YmdHis",time()).rand(0,9); $price = intval($orderData->goods_price*100); //調用封裝類 $input = new WxPayTransfer(); $input->setDesc('商家結算入賬'); $input->setPartnerTradeNo($outid); //建議默認的預支付交易單商戶訂單號由date("YmdHis").'_'.order_id組成 $input->setAmount(100); $input->setOpenid($orderData->owner_openid); // $input->setCheckName('FORCE_CHECK'); $input->setCheckName('NO_CHECK'); // $input->setReUserName(); $order = WxPayApi::transfer($input);//傳遞對象到相應的處理方法 if ($order['return_code'] == 'SUCCESS'){ $orderData->is_checkout =1; $orderData->check_time = $order['payment_time']; $orderData->save(); return true; }else{ trace($order,'企業付款input'); return false; } } }
/** * 企業付款,WxPayTransfer中好多參數必填必填。文檔 https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2 * appid、mchid、spbill_create_ip、nonce_str不需要填入 * @param \com\wxpay\database\WxPayTransfer $inputObj * @param int $timeOut * @throws WxPayException * @return array 成功時返回,其他拋異常 * @throws \com\wxpay\WxPayException */ public static function transfer($inputObj, $timeOut = 6) { $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; //檢測必填參數 if (!$inputObj->isPartnerTradeNoSet()) { throw new WxPayException("缺少統一支付接口必填參數PartnerTradeNoSet!"); } else if (!$inputObj->isDescSet()) { throw new WxPayException("缺少統一支付接口必填參數desc!"); } else if (!$inputObj->isAmountSet()) { throw new WxPayException("缺少統一支付接口必填參數金錢amount!"); } else if (!$inputObj->isOpenidSet()) { throw new WxPayException("缺少統一支付接口必填參數openid!"); } //還有很多,不寫了,傳夠就好 $inputObj->setMchAppid(WxPayConfig::getConfig('APPID'));//公眾賬號ID $inputObj->setMchId(WxPayConfig::getConfig('MCHID'));//商戶號 $inputObj->setSpbillCreateIp($_SERVER['REMOTE_ADDR']);//終端ip $inputObj->setNonceStr(self::getNonceStr());//隨機字符串 //簽名 $inputObj->setSign(); $xml = $inputObj->toXml(); $startTimeStamp = self::getMillisecond();//請求開始時間 $response = self::postXmlCurl($xml, $url, true, $timeOut);//發送請求 // trace($response,'返回'); $result = $inputObj->fromXml($response); // trace($result,'返回結果'); self::reportCostTime($url, $startTimeStamp, $result);//上報請求花費時間 return $result; }
注意:
1、下載的證書后,代碼設置的證書路徑為絕對路徑。
2、如果不懂哪里出現了問題,可以抓取微信服務器返回的信息,提示一目了然