<?php

namespace App\Http\Controllers\API\Admin\YeWu;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class PayController extends Controller
{
  public static $appid = '8a81c1bf7bea6e9f017c119c6edf0095';
  public static $appkey = '2927e4d6d0aa424297043c5f0b0bc6ae';
  public static $mid = '898460180119047';
  public static $tid = 'H5UWV72J';

  //支付
  public function MakePay()
  {
    $msgSrcId = request('show_id');
    $title = request('title');
    $total = request('total');
    $openid = request('openid');
    $data = [];
    $data['requestTimestamp'] = date("YmdHis");
    $data['mid'] = self::$mid;
    $data['tid'] = self::$tid;
    $data['merOrderId'] = '30ZFA'.$msgSrcId;
    $data['instMid'] = 'MINIDEFAULT';
    $data['orderDesc'] = $title;
    $data['totalAmount'] = $total;
    $data['subAppId'] = 'wxfa449edc09757390';
    $data['subOpenId'] = $openid;
    $data['tradeType'] = 'MINI';
    $data['notifyUrl'] = 'https://tj.xdhosp.com:8123/api/pay/callback';
//        $data['notifyUrl'] = 'https://a1.uniapp.sa0.online/api/pay/callback';

    $body = json_encode($data);
    $url = 'https://api-mop.chinaums.com/v1/netpay/wx/unified-order';
    return self::curlpost($url, $body);
  }

  //支付查询
  public function PayCheck()
  {
    $show_id = request('show_id');
    $data = [];
    $data['requestTimestamp'] = date("YmdHis", time());    // 报文请求时间
    $data['mid'] = self::$mid;
    $data['tid'] = self::$tid;
    $data['merOrderId'] = '30ZFA' . $show_id;
    $data['instMid'] = 'MINIDEFAULT'; // 业务类型
    $body = json_encode($data);
    $url = 'https://api-mop.chinaums.com/v1/netpay/query';
    return self::curlpost($url, $body);
  }

  public static function curlpost($url, $body, $echo_ = false)
  {

    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen($body),
      'Authorization: ' . self::getOpenBodySig($body, $echo_)
    ));
    if ($echo_) {
      echo "<br>--------------------<br>";
      var_dump($url, $body);
      echo "<br>--------------------<br>";
    }
    $output = curl_exec($ch);//运行curl
    curl_close($ch);
    return json_decode($output, true);
  }

  public static function getOpenBodySig($body, $echo_ = false)
  {
    $appid = self::$appid;
    $appkey = self::$appkey;
    $timestamp = date("YmdHis", time());
    $nonce = md5(uniqid(microtime(true), true));
    $str = bin2hex(hash('sha256', $body, true));
    $signature = base64_encode(hash_hmac('sha256', "$appid$timestamp$nonce$str", $appkey, true));
    $authorization = "OPEN-BODY-SIG AppId=\"$appid\", Timestamp=\"$timestamp\", Nonce=\"$nonce\", Signature=\"$signature\"";
    if ($echo_) {
      echo "<br>--------------------<br>";
      var_dump($authorization);
      echo "<br>--------------------<br>";
    }
    return $authorization;
  }
}

