微信小微商户申请入驻接口PHP示例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010324331/article/details/82107623

接口文档地址 【查看】
该接口请求需要双向证书。 【详见证书使用】

在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 跑一下
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u010324331/article/details/82107623