<?php

namespace app\business;

use support\Db;
use support\Redis;
use yzh52521\EasyHttp\Http;

class PayFiveBusiness
{

    static private $url="http://pay.hongyun.click/api/pay/create_order";
    static private $apikey='JCB0AQUZDAWIZGPQDSYTUUWC9URXY3RVEP1KMBMKJTJJPZZUCRF3WJ73FHSOHCB3WPSAT2WKCSJ2D0SX8DX48OWVH9YT9TLQCSTS0GNFJRO2FJCNSSXFPWT0KLMP9MDZ';
    static private $memberid=10308;

    /**
     * @param array $param[
     * order_no
     * ]
     * @return array
     */
    static public function payment($order_no,$pay_bankcode,$money,$url = '')
    {
        if(empty($url)){
            $url=getenv('WEB_HOST');
        }
        $arr=[
            'mchId'                 =>  (int)self::$memberid,//商户号
            'productId'             =>  (int)$pay_bankcode,//通道类型
            'mchOrderNo'            =>  (string)$order_no,//商户订单号
            'amount'                =>  (int)bcmul($money,100),//金额，单位为分
            'notifyUrl'             =>  (string)getenv('API_HOST').'/api/pay/payment_callback_five.html',//异步通知地址，支付成功后将支付成功消息以POST请求发送给这个网址
            'returnUrl'             =>  (string)$url
        ];
        $arr['sign']=self::payMd5sign($arr);
        $data=Http::post(self::$url,$arr)->array();
        return ['data'=>$data,'arr'=>$arr];
    }

    static public function payMd5sign(array $param)
    {
        ksort($param);                               //ASCII码排序
        $md5str = [];
        foreach ($param as $key => $val) {
            $md5str[] =   $key . "=" . $val ;
        }

        $sign = strtoupper(md5(implode('&',$md5str) . "&key=" .self::$apikey));

        //转换成字符串并且拼接上密钥
        return $sign;

    }

}