WeChat Pay-how to obtain the [acceptance signature] in the payment acceptance guidelines?

recently I am doing the acceptance of WeChat Pay of App. Does the signature of the acceptance need to be obtained by writing a separate code and then remain unchanged during the whole acceptance period, or do you need to dynamically obtain it every time in the payment acceptance process until the acceptance is completed, and then change it to your original API key? I really don"t understand here. I"ve been searching the Internet for a long time, but I don"t have any relevant information.

I wrote a small program to simulate post behavior with curl, which always failed

.
  whether a certificate is required for https://api.mch.weixin.qq.com.

request method POST

request parameters:
Field name field required sample value type description
Merchant number mch_id is 1305638280 String (32) Wechat Merchant number assigned by WeChat Pay
Random string nonce_str is 5K8264ILTKCH16CQ2502SI8ZNMTM67VS String (32) random string, no longer than 32 bits
signature sign is 5K8264ILTKCH16CQ2502SI8ZNMTM67VS String (32) signature value

< hr >
Mar.09,2021

I checked a lot of posts on the Internet, and finally got it done, so as to provide dinner for the newcomers ~
PS.. If you confirm that all your parameters are correct, but still prompt for a signature error, update the API key, and then ok. Wechat seems to have a cache

.

create a sandbox acceptance class that extends from WxPayDataBase

class Sandbox extends WxPayDataBase{

    //
    public function s_setValues($k, $v){
        $this->values[$k] = $v;
    }

    //
    public function s_getValues($k){
        return $this->values[$k];
    }
    
    //API
    public static function getSignKey($input, $mch_key){

        console_log( '1:'.json_encode($input->values) );
        
        //
        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey';
        
        //
        $input->setSign($mch_key);
        console_log( '2:'.json_encode($input->values) );
        
        //$values = $input->GetValues();
        //console_log( '3:'.json_encode($values) );
        
        $xml = $input->array2xml();
        console_log( '4:'.$xml );

        //
        $result = self::postXmlCurl($xml, $url);
        console_log( json_encode($result) );

        $result = $input->xml2array($result);
        console_log( json_encode($result) );

        return $result;
    }

    

    /**
     * 
     * 32
     * @param int $length
     * @return 
     */
    public static function getNonceStr($length = 32) 
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
        $str ="";
        for ( $i = 0; $i < $length; $iPP )  {  
            $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
        } 
        return $str;
    }


    /**
     * postxmlurl
     * 
     * @param string $xml  postxml
     * @param string $url  url
     * @param bool $useCert 
     * @param int $second   url30s
     * @throws WxPayException
     */
    private static function postXmlCurl($xml, $url, $useCert = false, $second = 30)
    {    
        $ch = curl_init();
        
        //
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);

        //
        curl_setopt($ch, CURLOPT_URL, $url);
        
        //skysowe_modifid
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        
        //header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        
        //
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        console_log( 'cert:'.$useCert );
        if($useCert == true){
            //
            //:cert  key .pem
            curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
            curl_setopt($ch,CURLOPT_SSLCERT, WxPayConfig::SSLCERT_PATH);
            curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
            curl_setopt($ch,CURLOPT_SSLKEY, WxPayConfig::SSLKEY_PATH);
        }
        
        //post
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        //console_log( 'xml:'.json_encode($xml) );

        //curl
        $data = curl_exec($ch);
        //console_log( 'curl_result:'.json_encode($data) );
        
        //
        if($data){
            curl_close($ch);
            return $data;
        } else { 
            $error = curl_errno($ch);
            curl_close($ch);
            throw new WxPayException("curl:$error");
            //console_log( 'curl_error:'.$error );
        }
    }


    /**
     * xml
     * @throws WxPayException
    **/
    public function array2xml()
    {
        if(!is_array($this->values) 
            || count($this->values) <= 0)
        {
            throw new WxPayException("");
        }
        
        $xml = "<xml>";
        foreach ($this->values as $key=>$val)
        {
            if (is_numeric($val) || $key=="nonce_str" || $key=="sign" ){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml; 
    }

    /**
     * xmlarray
     * @param string $xml
     * @throws WxPayException
     */
    public function xml2array($xml)
    {    
        if(!$xml){
            throw new WxPayException("xml");
        }
        //XMLarray
        //xml
        libxml_disable_entity_loader(true);
        $this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);        
        return $this->values;
    }

}

then get sandbox_signkey

    //:0 
    $sandbox_test = new Sandbox();

    $sandbox_test->s_setValues('mch_id', WxPayConfig::MCHID);
    $sandbox_test->s_setValues('nonce_str', $sandbox_test->getNonceStr());

    //debug
    console_log( $sandbox_test->s_getValues('mch_id') );
    console_log( $sandbox_test->s_getValues('nonce_str') );
    console_log( WxPayConfig::KEY );

    $rs = Sandbox::getSignKey($sandbox_test, WxPayConfig::KEY);

    //debug
    echo json_encode($rs);
    echo $rs['sandbox_signkey'];

    exit;
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b380d3-2c0c9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b380d3-2c0c9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?