微信小微商戶申請入駐介面PHP示例
阿新 • • 發佈:2018-11-20
介面文件地址 【檢視】
該介面請求需要雙向證書。 【詳見證書使用】
在curl請求這裡和圖片上傳介面一樣都需要上傳證書的,詳見我的上一篇部落格 微信小微商戶圖片上傳介面
下面給出的程式碼 publicKeyEncrypt 方法中的 $publickey 就是證書下載介面返回的 encrypt_certificat
e.ciphertext 解密後的明文證書,具體解密方法見微信小微商戶下載平臺證書介面(PHP SHA256 with RSA 簽名,AEAD_AES_256_GCM解密方法)
github地址 :【 https://github.com/liumenglei/WechatXiaowei 】
下面直接貼程式碼
/** * publicKeyEncrypt 對身份證等敏感資訊加密 * @param string $string * @return string * @throws WxException */ protected function publicKeyEncrypt(string $string) { $crypted = ''; $publicKey = $this->getPublicKey(); if ($publicKey) { $publicKeyResource = openssl_get_publickey($publicKey); $f = openssl_public_encrypt($string, $crypted, $publicKeyResource, OPENSSL_PKCS1_PADDING); openssl_free_key($publicKeyResource); if ($f) { return base64_encode($crypted); } } throw new WxException(20002); } /** * MakeSign 生成簽名 * @param $data * @param string $signType * @return string */ protected function makeSign(array $data, $signType = 'HMAC-SHA256') { //簽名步驟一:按字典序排序引數 ksort($data); $string = $this->toUrlParams($data); //簽名步驟二:在string後加入KEY $string = $string . "&key=" . $this->diy_key;//. $this->aes_key; //簽名步驟三:MD5加密或者HMAC-SHA256 if ($signType == 'md5') { //如果簽名小於等於32個,則使用md5驗證 $string = md5($string); } else { //是用sha256校驗 $string = hash_hmac("sha256", $string, $this->diy_key); } //簽名步驟四:所有字元轉為大寫 $result = strtoupper($string); return $result; } /** * applyEnter 申請入駐小微商戶 * @return mixed */ public function applyEnter(array $params) { // 校驗引數 if (!$this->checkParams($params)) { throw new WxException(20004); } // 校驗銀行卡號字首是否支援 if ($this->accountNumberIsSupport($params['account_number'] ?? '')) { throw new WxException(20003); } $data = [ 'version' => '2.0', 'cert_sn' => $this->newResponseData()['serial_no'], 'mch_id' => $this->mch_id, 'nonce_str' => $this->getRandChar(), 'sign_type' => 'HMAC-SHA256', 'sign' => '', 'business_code' => $this->getBusinessCode(), // 業務申請編號 'id_card_copy' => $params['id_card_copy'], // 身份證人像面照片 media_id 'id_card_national' => $params['id_card_national'], // 身份證國徽面照片 'id_card_name' => $this->publicKeyEncrypt($params['id_card_name']), 'id_card_number' => $this->publicKeyEncrypt($params['id_card_number']), 'id_card_valid_time' => $params['id_card_valid_time'], // '["1970-01-01","長期"]' string(50) 'account_name' => $this->publicKeyEncrypt($params['account_name']), 'account_bank' => $params['account_bank'], 'bank_address_code' => $params['bank_address_code'], 'bank_name' => $params['bank_name'] ?? '', 'account_number' => $this->publicKeyEncrypt($params['account_number']), 'store_name' => $params['store_name'], 'store_address_code' => $params['store_address_code'], 'store_street' => $params['store_street'], 'store_longitude' => $params['store_longitude'] ?? '', 'store_latitude' => $params['store_latitude'] ?? '', 'store_entrance_pic' => $params['store_entrance_pic'], 'indoor_pic' => $params['indoor_pic'], 'address_certification' => $params['address_certification'] ?? '', 'merchant_shortname' => $params['merchant_shortname'], 'service_phone' => $params['service_phone'], 'business' => $params['business'], 'product_desc' => $params['product_desc'] ?? '', 'qualifications' => $params['qualifications'] ?? '', 'rate' => $params['rate'], 'business_addition_desc' => $params['business_addition_desc'] ?? '', 'business_addition_pics' => $params['business_addition_pics'] ?? '', // ["123","456"] 最多可上傳5張照片,請填寫已預先上傳圖片生成好的MediaID 'contact' => $this->publicKeyEncrypt($params['contact']), 'contact_phone' => $this->publicKeyEncrypt($params['contact_phone']), 'contact_email' => isset($params['contact_email']) && !empty($params['contact_email']) ? $this->publicKeyEncrypt($params['contact_email']) : '', ]; // 簽名 $data['sign'] = $this->makeSign($data, $data['sign_type']); $url = self::WXAPIHOST . 'applyment/micro/submit'; // 陣列轉xml $xml = $this->toXml($data); // 發起入駐申請請求 $res = $this->httpsRequest($url, $xml, [], true); // 處理返回值 return $this->disposeReturn($res, ['applyment_id'], ['business_code' => $data['business_code']]); }
最後介面完成 postman 跑一下