<?php

namespace App\Http\Controllers\API;

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

class DemoController extends Controller
{
  public function pay_back()
  {
    $input = file_get_contents('php://input');
    file_put_contents(base_path() . '/storage/app/pay/pay_back.txt', urldecode($input));
    return [
      'code' => 'SUCCESS',
      'message' => '成功',
    ];
  }

  public function pay(Request $request)
  {
    $openid = $request->post('openid');
    $total = $request->post('total');
    $wcp = new WeChatPayController();
    $wcp->builder([
      'appid' => 'wx0d92d2990ec16a55',
      'pem_path' => base_path() . '/storage/app/pay/key.pem',
      'cer_path' => base_path() . '/storage/app/pay/crt.pem',
      'cer_num' => '3CE37188EBCFBBEB800B0E1C69B360F05A3E80CD',
      'mchid' => '1638739772',
      'v3' => 'AVPV7NxK8cC2RvRrrwdTqUG9YbQXQe3w',
    ]);
    $out_trade_no = date('Ymd') . time() . rand(10, 99);
    $pay = $wcp->create([
      'description' => '体检预约',
      'out_trade_no' => $out_trade_no,
      'notify_url' => 'https://api.hainan2024.sa0.online/api/Demo/pay_back',
      'total' => $total,
      'openid' => $openid
    ]);

    return \Yz::Return(true, '获取成功', [
      'pay' => $pay
    ]);
  }
}
