# 代码示例PHP

<?php
function query($config, $uri, $param, $method = 'GET')
{
    $param['pid'] = $config['pid'];
    $param['appId'] = $config['appid'];
    $param['apiName'] = $uri;
    $param['time'] = time() * 1000;
    $param['version'] = '1.0.0';

    ksort($param);
    $param['sign'] = getSign($param, $config['app_secret']);

    $client = new GuzzleHttp\Client();
    $response = $client->request($method, $config['domain'] . $uri, [
        'form_params' => $param
    ]);
    if ($response->getStatusCode() !== 200) {
        //exception
    }
    //方法调用完接口后,根据返回结果(参考文档)获取到返回的微信5个支付加密签名,并返回前端,唤起微信支付
    //{
    //    "appId":"wx88e42e952a2365a1",
    //    "timeStamp":"1561463756",
    //    "nonceStr":"0414b68abc5247d1905fa8370df945a1",
    //    "package":"prepay_id=wx2519555662982334908000291157975500",
    //    "signType":"RSA",
    //    "paySign":"cvDJq7lm3x04gDMBIAr0UpEayzM2RjplOfd/wC4qDKD+PUOqneZYNJZ2DD0oonpTpSVkrm5LrefjwwuLvwTFMdK+c+PenWpITbGFo7m2PUmTNV7rhYC7kXI4EUlQ+bXqhD+wFO8cPxr0pZf6um2xMagLsDjcNJY35jx1H4LBe4eWb6SilSv4va2AsBilCBKFHk7OGiYPrWmiDX5qYMF3SLlNK5p1jiIWbUkh7y4C3rWLvYggUiL4Iyp+cdaUSb0S/Whp4cOs5UZ5NNfXqLrR8ZPZKvK5hN0ndC+OTDQDCMIrX4sezh6tZuruwf8UQsjzZkReczbkcEhx0gvrtttNGA=="
    //}
    return json_decode($response->getBody() . '', true);
}

function getSign($param, $secret)
{
    return strtoupper(md5(array_reduce(array_keys($param), function ($content, $key) use ($param) {
        return $content . ($content ? '&' : '') . $key . '=' . $param[$key];
    }) . $secret));
}


//公用参数
$config = [
    'pid' => "PID",//平台颁发的PID
    'appId' => "APPID",//平台颁发的APPID
    'app_secret' => 'secret',//平台颁发的密钥
    'mid' => 'MID',//平台颁发的支付商户号
    'domain' => "http://opentest.eduxxx.com/open/", //平台基础host,根据生产和测试进行切换
]
$order = [];//订单数据业务方自己产生
$param = [
    'mid' => $config['mid'],
    'ouid' => $order->open_id,//获取小程序中的openId
    'outTradeNo' => $order->trade_id,//业务方生成自己的订单号,必须唯一
    'subject' => '交易标题', // 交易标题
    'body' => '交易信息', // 交易信息
    'tradeAmount' => bcmul($order->total_amount, 0.01, 2),
    'channelCode' => $order->channel_type, // WX 微信支付 ,ALI 支付宝支付,其他参考文档
    'expireTime' => 5, // 交易有效时间 单位 分钟
    'attach' => $order->id, // 回调时会原样返回
    'openId' => $order->open_id,
    'payMode' => 'UNIFIED', // SUBSCRIPTION 公众号支付 默认;MINI_PROGRAM 小程序支付方式,BAR条码支付模式,H5 h5支付模式,UNIFIED 统一下单模式,ZD账单模式,其他参考文档
    'deviceIp' => $order->device_ip,
    'payScene' => 'WX_K12',//当使用汇付对私通道的时候,必填;WX_K12_MINI(教育缴费小程序),WX_K12(教育K12) WX_MINI(线下小程序支付)WX_SUBSCRIPTION(线下公众号支付)WX_MINI_ONLINE(线上小程序支付);
    'sourceCode' => 'HF',
    // 'stageNum' => null, // 3,6,12
    // 'authCode' => null, // 授权码, 传入则为条码支付
    // 'industryInfo' => null, //行业信息 channelCode=K12时必填
    'notifyUrl' => "/api2/callback/order/pay/notify/", // 交易结果通知地址
    'returnUrl' => 'https://www.soeasypay.com' // 前端回跳地址
];

$resut = query($config,'/trade/create', $param, 'POST');
var_dump($resut);

?>