<?php
namespace app\common\library;
use think\Controller;
use function EasyWeChat\Kernel\Support\get_client_ip;

date_default_timezone_set('Asia/Shanghai');
header('Content-type:text/html;charset=utf-8');
//require_once './class/common.php';
//require_once './class/H5FastPay.php';

require_once env('root_path').'application/common/library/shande/Common.php';
require_once env('root_path').'application/common/library/shande/H5FastPay.php';

class Shande extends Controller
{
    // 统一下单接口
    // https://open.sandpay.com.cn/product/detail/43308/43720/43721
    public static function orderPay($order_no,$amout,$body,$notifyurl,$frontUrl)
    {
        $client = new \H5FastPay;
        // 参数
        $client->body = array(
            'orderCode'   => $order_no,
            //'totalAmount' => '000000000012',
            'totalAmount' => $amout,
            'subject'     => $body,
            'body'        => $body,
            'payMode'     => 'sand_h5',
            'clientIp'    => get_client_ip(),
            //'notifyUrl'   => 'http://192.168.1.66/sandpay-qr-phpdemo/notifyurl.php',
            'notifyUrl'   => $notifyurl,
            //'frontUrl'    => 'http://192.168.1.66/sandpay-qr-phpdemo/notifyurl.php',
            'frontUrl'    => $frontUrl,
        );
        // 返回结果
        $ret = $client->request('orderPay');
        return $ret;
    }

    // 订单查询接口
    public function orderQuery()
    {
        $client = new \H5FastPay;
        // 参数, 每次需要重新赋值
        $client->body = array(
            'orderCode' => date('YmdHis', time()) + '0601',
            'extend'    => ''
        );
        // 返回结果
        $ret = $client->request('orderQuery');
        echo '<pre class="myCode">';
        print_r($ret);
        echo '</pre>';
    }

    // 退款申请
    public function orderRefund()
    {
        $client = new \H5FastPay;
        // 参数
        $client->body = array(
            'orderCode'          => 'Y20181204170925675836', //新的订单号
            'oriOrderCode'       => '2017091551421977', //原订单号
            'refundAmount'       => '000000000012', //退款金额
            'refundMarketAmount' => '000000000012', //退营销金额
            'notifyUrl'          => 'http://192.168.22.171/sandpay-qr-phpdemo.bak/test/dist/notifyUrl.php',
            'refundReason'       => 'test',
            'extend'             => ''
        );
        // 返回结果
        $ret = $client->request('orderRefund');
        echo '<pre class="myCode">';
        print_r($ret);
        echo '</pre>';
    }

    // 异步通知通用接口
    public function notify()
    {
        $client = new H5FastPay;
        $sign   = $_POST['sign']; //签名
        $data   = stripslashes($_POST['data']); //支付数据

        // 验签
        try {
            $verifyFlag = $client->verify($data, $sign);
            if ($verifyFlag) {
                echo '签名正确';
            } else {
                echo '签名错误';
            }
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    // 商户自主重发异步通知接口
    public function orderMcAutoNotice()
    {
        $client = new H5FastPay;
        // 参数
        $client->body = array(
            'orderCode'  => 'Y201805071725592',
            'noticeType' => '00',
        );
        // 返回结果
        $ret = $client->request('orderMcAutoNotice');
        echo '<pre class="myCode">';
        print_r($ret);
        echo '</pre>';
    }

    // 对账单申请接口
    public function clearfileDownload()
    {
        $client = new H5FastPay;
        // 参数
        $client->body = array(
            'clearDate' => '20200611', // 结算日期
            'fileType'  => '1', // 文件返回类型
            'extend'    => ''
        );
        // 返回值
        $ret = $client->request('clearfileDownload');
        echo '<pre class="myCode">';
        print_r($ret);
        echo '</pre>';
    }
}