Business Payment to Customer's Change

Posted by Charles Wong on Tue, 01 Oct 2019 09:41:53 +0200

Business name (or other non-service business name with the main body) has been stationed for 90 days
Business name (or other non-service business name with the main body) has 30 consecutive days of normal transactions
Log in to pay business platform, product center, and open enterprise payment.
/**
* enterprise payment (request for payment to small businesses)
*@ param string $openid user openID
*@ param string $trade_no single number
*@ param string $money amount (unit score)
*@ param string $desc description
*@ param string appid Association appid @returnstring XML structure string / public function txFunc (appid Association appid) *@ Return string XML structured string **/ Public function txFunc (string of appid @returnstring XML structure of appid Association / public function txFunc (inputs)
{

    $params = array(
        'mch_appid'        => $inputs['appid'], //Applet id
        'mchid'            => $inputs['mchid'], //Merchant id
        'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], //Terminal ip
        'nonce_str'        => $this->getNonceStr(), //Random string
        'partner_trade_no' => $inputs['orderid'], //Merchant Order Number, Require Unique
        'openid'           => $inputs['openid'],
        'check_name'       => 'NO_CHECK', //OPTION_CHECK does not force the verification of real names. FORCE_CHECK: force NO_CHECK:
        'amount'           => $inputs['amount'], //The total amount//payment amount is divided into units
        'desc'             => $inputs['body'],
        //'re_user_name'= >'jorsh', / / payee's user name * selected
        //'device_info'=>'1000', //device number*
    );
    //Generate signature
    $sign           = $this->sign($params, $inputs['key']); //Merchant secret key
    $params['sign'] = $sign;
    //Construct XML data (packets are sent in XML format)

    //Convert the array content to xml format and request the request.
    $xmlData    = $this->arrayToXml($params);
    $xml_result = $this->curl_post_ssl('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', $xmlData, 60);
    $result     = $this->xmlToArray($xml_result);
    return $result;
}

//Array to String Method
public function arrayToXml($arr)
{
    $xml = "<xml>";
    foreach ($arr as $key => $val) {
        if (is_numeric($val)) {
            $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
        } else {
            $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }
    }
    $xml .= "</xml>";
    return $xml;
}
//Converting xml strings to arrays
public static function xmlToArray($xml)
{
    $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    return $array_data;
}

/**
 * Data signature
 * @param $data
 * @return string
 */
private function sign($data, $key)
{
    ksort($data);
    $string1 = "";
    foreach ($data as $k => $v) {
        if ($v && trim($v) != '') {
            $string1 .= "$k=$v&";
        }
    }
    $stringSignTemp = $string1 . "key=" . $key;
    $sign           = strtoupper(md5($stringSignTemp));
    return $sign;
}

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

// certificate
/**
 * [curl_post_ssl Send curl_post data]
 * @param  [type]  $url     [Send Address]
 * @param  [type]  $xmldata [Send file format]
 * @param  [type]  $second [Set the maximum number of seconds to execute]
 * @param  [type]  $aHeader [Setting Head]
 * @return [type]           [description]
 */
public function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array())
{
    $isdir = $_SERVER['DOCUMENT_ROOT'] . "/app/check/"; //Certificate location; absolute path

    $ch = curl_init(); //Initialize curl
    curl_setopt($ch, CURLOPT_TIMEOUT, $second); //Set the maximum number of seconds to execute
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Require the result to be a string and output to the screen
    curl_setopt($ch, CURLOPT_URL, $url); //Crawl the specified page
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Termination of validation from the server
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //
    curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); //Certificate type
    curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem'); //Certificate location
    curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); //Encryption type of private key specified in CURLOPT_SSLKEY
    curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem'); //Certificate location
    curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
    curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
    if (count($aHeader) >= 1) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader); //Set head
    }
    curl_setopt($ch, CURLOPT_POST, 1); //post submission
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata); //All data is sent using the "POST" operation in the HTTP protocol

    $data = curl_exec($ch); //Executive reply

    if ($data) {
        curl_close($ch);
        return $data;
    } else {
        $error = curl_errno($ch);
        echo "call faild, errorCode:$error\n";
        curl_close($ch);
        return false;
    }
}

Topics: xml curl