直接上代码
在这里<?php
declare (strict_types=1);
namespace app\controller;
use think\facade\Log;
use think\Request;
class DemoController
{
protected $appId = "ttc596*********e46e9";
protected $secret = "420c20867e7**********8b005b5ddaa64a5b";
protected $salt = "aLaBzmbdL*******************j574oaeOCX";
protected $token = "123123";
/**
* 显示资源列表
* doc https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/api/open-interface/payment/secure/YE
* @return \think\Response
*/
public function index(): \think\Response
{
$outOrderNo = "test21052511401";//开发者侧的订单号
$totalAmount = 1;//支付价格; 接口中参数支付金额单位为[分]
$subject = "测试商品描述";
$body = "测试商品详情";
$validTimestamp = 60 * 60;//订单过期时间(秒);
$notifyUrl = "http://edf17151ed2b.ngrok.io/index.php/notify";
$response = $this->createOrder($outOrderNo, $totalAmount, $subject, $body, $validTimestamp, $notifyUrl);
dd($response);
}
/**
* @param Request $request
* @return \think\response\Json
*/
public function notify(Request $request): \think\response\Json
{
$notify = $request->param();
if ($notify['msg_signature'] !== $this->signature($notify, $this->token)) {
Log::record('回调验证错误');
} else {
//获取订单信息
$order = json_decode($notify['msg'], true);
//处理订单
}
$data = ['err_no' => '0', 'err_tips' => 'success'];
return json($data);
}
/**
* @param $outOrderNo
* @param $totalAmount
* @param $subject
* @param $body
* @param $validTimestamp
* @param $notifyUrl
* @return array|string
*/
private function CreateOrder($outOrderNo, $totalAmount, $subject, $body, $validTimestamp, $notifyUrl)
{
$params = [
'app_id' => $this->appId,
'out_order_no' => $outOrderNo,
'total_amount' => $totalAmount,
'subject' => $subject,
'body' => $body,
'valid_time' => $validTimestamp,
'notify_url' => $notifyUrl,
//'cp_extra' => $cpExtra,
//'thirdparty_id' => $thirdPartyId,
//'disable_msg' => $disableMsg,
//'msg_page' => $msgPage,
//'store_uid' => $storeUid
];
$params = array_filter($params);
$params['sign'] = $this->signature($params, $this->salt);
//var_dump($params);die;
return $this->post(
'https://developer.toutiao.com/api/apps/ecpay/v1/create_order',
$params
);
}
/**
* payment api signature
* @param array $body
* @param string $secret
* @return string
*/
public static function signature(array $body, string $secret): string
{
$filtered = [];
foreach ($body as $key => $value) {
if (in_array($key, ['sign', 'app_id', 'thirdparty_id'])) {
continue;
}
$filtered[] =
is_string($value)
? trim($value)
: $value;
}
$filtered[] = trim($secret);
sort($filtered, SORT_STRING);
return md5(trim(implode('&', $filtered)));
}
/**
* post request
* @param string $uri
* @param array $params
* @param array $headers
* @return string
*/
private function post(string $uri, array $params = [], array $headers = []): array
{
$headers[] = 'Content-type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
}
插入代码片
index方法成功返回
下方示例:
{
"order_id": "6819903302604491021",
"order_token": "CgsIARCABRgBIAQoARJOCkx+WgXqCUIwTel2V3siEGZ0++poigIM+SMMxtMx798Vj0ZYzoTYBqeNslodUC9X5KAOHkR1YbSBz6I6pXATh5faIGy7R72A9vwm0OczGgA="
}
前端调起tt.pay拉起字节收银台,就行