头条小程序支付thinkphp + uniapp

2021/10/31 1:09:42

本文主要是介绍头条小程序支付thinkphp + uniapp,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

后端

后端php,按官方来处理就行

  /**
   * @name: 头条小程序 支付
   * @param {*}
   * @return {*}
   */
  public function pay_order_toutiao(Order $Order)
  {
    $param = $this->request->param();
    if (empty($param['order_id'])) {
      $this->error('订单不存在');
    }
    $order_info = $Order->where('id', $param['order_id'])->find();
    $url = 'https://developer.toutiao.com/api/apps/ecpay/v1/create_order';
    $http = is_https() ? 'https' : 'http';
    $pay_data = [
      "app_id" => env('toutiao.appid', ''),
      "out_order_no" => $order_info['order_sn'],  // 自己的订单号
      "total_amount" =>  $order_info['money'] * 100,
      "subject" => "这里是名称",
      "body" => ''一些内容,
      "valid_time" => 1800, // 过期时间
      "cp_extra" => "abcdefgh", // 这里是自定义字段
      "notify_url" => $http . '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . "/api/test/Bytedance/notify", // 回调地址
      "disable_msg" => 0,
      "msg_page" => "pages/index/index"
    ];
    $sign_res = $this->sign($pay_data);
    $pay_data['sign'] = $sign_res;  // 注意些这里,需要填写自己的SALT

    $res = $this->curl_post($url, json_encode($pay_data));
    $res_info = [];
    if ($res['err_no'] == 0) {
      $res_info['order_id'] = $res['data']['order_id'];
      $res_info['order_token'] = $res['data']['order_token'];
      $this->success('下单成功', $res_info);
    } else {
      $this->error($res['err_no'], $res['data']);
    }
  }

  function sign($map)
  {
    $rList = array();
    foreach ($map as $k => $v) {
      if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
        continue;
      $value = trim(strval($v));
      $len = strlen($value);
      if ($len > 1 && substr($value, 0, 1) == "\"" && substr($value, $len, $len - 1) == "\"")
        $value = substr($value, 1, $len - 1);
      $value = trim($value);
      if ($value == "" || $value == "null")
        continue;
      array_push($rList, $value);
    }
    array_push($rList, "这里填写自己的SALT");
    sort($rList, 2);
    return md5(implode('&', $rList));
  }


public function curl_post($url, $data = array())
{
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($ch, CURLOPT_HTTPHEADER,  array(
    'Content-Type: application/json; charset=utf-8',
  ));

  // POST数据
  curl_setopt($ch, CURLOPT_POST, 1);
  // 把post的变量加上
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  $output = curl_exec($ch);
  curl_close($ch);
  return $output;
}

前端



这篇关于头条小程序支付thinkphp + uniapp的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程