<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2017/12/9
 * Time: 12:02
 */

namespace App\Libs\Pay\Merchants;

use GuzzleHttp\Client;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order as Wxorder;
use Log;

class Official
{

    private $appid;
    private $secret;
    private $token;
    private $merchant_id;
    private $key;
    private $cert_path;
    private $key_path;
    public $app;
    public function __construct($config)
    {
        $this->appid = $config['appid'];
        $this->secret = $config['secret'];
        $this->token = $config['token'];
        $this->merchant_id = $config['merchant_id'];
        $this->key = $config['key'];
        $this->cert_path = $config['cert_path'];
        $this->key_path = $config['key_path'];

        $options = [
            'app_id'=>$this->appid,
            'secret'=>$this->secret,
            'token'=>$this->token,
            'payment'=>[
                'merchant_id'=>$this->merchant_id,
                'key'=>$this->key,
                'cert_path'=>public_path($this->cert_path),
                'key_path'=>public_path($this->key_path)
            ]
        ];
        $this->app = new Application($options);
    }

    function send($data)
    {
        Log::info('Official---enter--------------');
        $app =  $this->app;
        $payment = $app->payment;
        $attributes = [
            'trade_type' => 'JSAPI',
            'body' => $data['body'],
            'detail' => $data['detail'],
            'out_trade_no' => $data['trade_no'],
            'total_fee' => $data['price'],
            'notify_url' => env('OFFICIAL_PAY_CALL_BACK_URL'),
            'openid' => $data['openid'],
            'spbill_create_ip' => $data['create_ip'],
            'attach' => $data['remark']
        ];
        $order = new Wxorder($attributes);
        $result = $payment->prepare($order);
        Log::info('Official -------pay-----result----------');
        Log::info($result);
        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
            $data = [
                'appId' => $result->appid,
                'package' => 'prepay_id=' . $result->prepay_id,
                'nonceStr' => $result->nonce_str,
                'timeStamp' => time(),
                'signType' => 'MD5',
            ];
            $data['paySign'] = $this->MakeSign($data);
            return $data;
        }
        return [];
    }

    protected function MakeSign($value)
    {
        $data = $value;
        //签名步骤一：按字典序排序参数
        ksort($data);
        $buff = "";
        foreach ($data as $k => $v) {
            if ($k != "sign" && $v != "" && !is_array($v)) {
                $buff .= $k . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        //签名步骤二：在string后加入KEY
        $string = $buff . "&key=".$this->key;
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
}