Demo entry 5862309

php

   

Submitted by anonymous on Aug 03, 2016 at 08:00
Language: PHP. Code size: 3.1 kB.

<?php
/**
 * @author: zhaobolu@baidu.com
 * description:钱包返现打款
 */
class Rebate
{
    public $opUrl;
    public $signKey;
    public $config;
    public function __construct($orderId, $uid, $amount) {
        if(!is_numeric($amount) || intval($amount * 100) <= 0) {
            return false;
        }
        $this->opUrl = 'http://bdpay.baidu.com/api/0/transfer_application';
        $this->signKey = '';  //百度钱包合作密钥,保密
        $this->config = array(
            'service_code'      => 10,          //服务编号,固定值
            'sp_no'             => '',          //百付宝商户号
            'order_create_time' => date('YmdHis'), //转账交易时间,YYYYMMDDHHMMSS
            'order_no'          => $orderId,    //转账交易号,不超过20个数字
            'transfer_desc'     => '',          //转账的描述信息
            'transfer_amount'   => round($amount * 100),    //转账金额,以分为单位
            'currency'          => 1,           //币种,暂只支持人民币
            'input_charset'     => 1,           //请求参数的字符编码,暂只支持GBK
            'opposite_type'     => 1,           //收款方ID类型,1 passid, 2 商户号, 3 oauthId
            'opposite_no'       => $uid,        //收款方ID
            'output_type'       => 1,           //响应数据的格式,暂只支持XML
            'output_charset'    => 1,           //响应数据的字符编码,暂只支持GBK
            'version'           => 1,           //接口的版本号,此版本号为1
            'sign_method'       => 1,           //1 md5, 2 sha-1
        );
    }
    /**
     * 返现打款
     * @param int $orderId
     * @param int $uid
     * @param float $amount
     * @return bool
     */
    public function transfer() {
        $params = $this->config;
        $params['transfer_desc'] = mb_convert_encoding($params['transfer_desc'], 'GBK', 'UTF-8');
        $params = $this->getBaifubaoSign($params, $this->signKey);
        $url = $this->opUrl.'?'.http_build_query($params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $output = curl_exec($ch);
        curl_close($ch);
        $rst = simplexml_load_string($output);
        if(is_object($rst)) {
            if($rst->transfer_status == '0') {
                //打款成功
                return true;
            }
            else if($rst->transfer_status == '20302') {
                //重复打款成功
                return true;
            }
            else {
                //打款失败
                return false;
            }
        }
        //返回xml解析失败
        return false;
    }
    /**
     * @param array $arrParam
     * @param string $key
     * @return string
     */
    private function getBaifubaoSign(&$arrParam, $key)
    {
        $arrKeyValue = array();
        unset($arrParam['sign']);
        ksort($arrParam);
        foreach ($arrParam as $k => $v) {
            $arrKeyValue[] = $k . '=' . $v;
        }
        $arrKeyValue[] = 'key=' . $key;
        $params = implode('&', $arrKeyValue);
        $arrParam['sign'] = md5($params);
        return $arrParam;
    }
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).