<?php

namespace app\business;

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

class PayFourBusiness
{

    static private $url      = "https://xswg.bbbzf.xyz/api/pay/create_order";
    static private $apikey   = 'P4NFABPLJM1EXK4RP7YIM9XU0B4IKIJHP0RUJZRQIN95CJVV5C2NOMANHUZBW2ROFP1FJJQ8JQCFTDUKYVPPFMEQINBBCJILN6QA5TA4XHM0DLUGT7XHEKNNWETH5DP5';
    static private $memberid = 20000250;

    /**
     * @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'      => self::$memberid,
            'productId'  => $pay_bankcode,
            'mchOrderNo' => $order_no,
            'amount'     => (int)bcmul($money, 100),
            'notifyUrl'  => getenv('API_HOST') . '/api/pay/payment_callback_four.html',//异步通知地址，支付成功后将支付成功消息以POST请求发送给这个网址
            'subject'    => '商品',
            'body'       => '商品描述',
            'extra'      => 'abcd',
        ];
        $arr['sign'] = self::payMd5sign($arr);
        $data        = Http::post(self::$url, $arr)->array();
        return ['data' => $data, 'arr' => $arr];
    }

    static public function payMd5sign(array $param)
    {
        /* @对数组键进行ASCII码排序*/
        ksort($param);
        $arr = [];
        //将数组进行重组装

        foreach ($param as $k => $v) {
            if (!empty($v)) {
                $arr[] = $k . '=' . $v;
            }
        }
        //转换成字符串并且拼接上密钥
        $sign = implode('&', $arr) . '&key=' . self::$apikey;

        return strtoupper(md5($sign));

    }

}