php 抖音支付

直接上代码

<?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";

    /**
     * &#26174;&#31034;&#36164;&#28304;&#21015;&#34920;
     * 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";//&#24320;&#21457;&#32773;&#20391;&#30340;&#35746;&#21333;&#21495;
        $totalAmount = 1;//&#25903;&#20184;&#20215;&#26684;; &#25509;&#21475;&#20013;&#21442;&#25968;&#25903;&#20184;&#37329;&#39069;&#21333;&#20301;&#20026;[&#20998;]
        $subject = "&#27979;&#35797;&#21830;&#21697;&#25551;&#36848;";
        $body = "&#27979;&#35797;&#21830;&#21697;&#35814;&#24773;";
        $validTimestamp = 60 * 60;//&#35746;&#21333;&#36807;&#26399;&#26102;&#38388;(&#31186;);
        $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拉起字节收银台,就行

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
LinuxPHP

Linux系统通过命令行运行PHP 指定运行用户

2022-7-22 14:20:18

PHP

抖音小程序担保支付(php + uniapp)

2022-8-30 9:33:24

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
搜索