<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2019/2/22
 * Time: 18:11
 */

namespace App\Libs\Pay\Merchants;

class PalmpayV2
{
    function __construct($config)
    {
        $this->appId = $config['appId'];
        $this->mchId = $config['mchId'];
        $this->appkey = $config['appKey'];
        $this->subAppid = $config['subAppid'];
    }

    //生成订单
    function send($data)
    {
        $data = [
            'mchId'=>$this->mchId,
            'appid'=>$this->appId,
            'version'=>'3.0',
            'productName'=>$data['body'],
            'productDesc'=>'小说充值',
            //'openid'=>$data['openid'],
            //'subAppid'=>$this->subAppid,
            'money'=>$data['price'],
            'outTradeNo'=>$data['trade_no'],
            'notifyUrl'=>env('PALMPAY_NOFITY_URL'),
            'callbackUrl' => urlencode($data['pay_wait_url'])
        ];
        $data['sign'] = $this->sign($data);
        $pay_url = "sdkServer/thirdpays/pay/WECHAT_SUB?".http_build_query($data);

        return 'https://pay.palmpay.cn/'.$pay_url;
    }

    function sign($params)
    {
        return md5($params['appid'].'WECHAT_SUB'.$params['money'].$params['outTradeNo'].$this->appkey);
    }
}