字节跳动(头条、抖音)小程序支付(PHP + UniApp)
php
/** * 调起支付 * @Author Dc * @DateTime 2020-07-29T14:44:37+0800 */ public function pay(Request $request) { $openid = $request->param('openid', 0); $orderNo = $request->param('orderNo', 0); $findOrder = Db::table('gc_order')->where(['orderNo' => $orderNo, 'openid' => $openid])->find(); if (!$findOrder) { return self::err('订单不存在'); } $findUser = Db::table('data_user')->where('openid', $openid)->find(); if (!$findUser) { return self::err('用户不存在'); } $tt_pay_app_id = sysconf('wechat.dy_apppid'); $tt_merchant_id = sysconf('wechat.dy_mch_id'); $tt_pay_secret_key = $app_secret = sysconf('wechat.dy_appsecret'); $ip = $this->getRealIp(); $risk_info = [ 'ip' => $ip, ]; $orderInfo = [ "app_id" => $tt_pay_app_id, "sign_type" => "MD5", "out_order_no" => $orderNo, "merchant_id" => $tt_merchant_id, "timestamp" => (string) time(), "product_code" => "pay", "payment_type" => "direct", "total_amount" => $findOrder['price'] * 100, "trade_type" => "H5", "uid" => $openid, "version" => "2.0", "currency" => "CNY", "subject" => "格创课堂", "body" => "支付测试", "trade_time" => (string) $findOrder['create_at'], "valid_time" => "300", "notify_url" => "https://tp-pay.snssdk.com/cashdesk/test/paycallback", // "wx_url" => "https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx25161122572189727ea14cfd1832451500&package=2746219290", // "wx_type" => "MWEB", "alipay_url" => $this->getAliParam($findOrder), ]; $stringToBeSigned = $this->getSignContent($orderInfo, "UTF-8", $app_secret); $sign = md5($stringToBeSigned); $orderInfo['risk_info'] = json_encode($risk_info); $orderInfo['sign'] = $sign; return self::res($orderInfo); }
/** * 签名处理 * @param $params * @param $charset * @return string */ public function getSignContent($params, $charset = "", $app_secret) { ksort($params); $stringToBeSigned = ""; $i = 0; foreach ($params as $k => $v) { if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) { // 转换成目标字符集 $v = $this->characet($v, $charset); if ($i == 0) { $stringToBeSigned .= "$k" . "=" . "$v"; } else { $stringToBeSigned .= "&" . "$k" . "=" . "$v"; } $i++; } } $stringToBeSigned = $stringToBeSigned . $app_secret; unset($k, $v); return $stringToBeSigned; } /** * 校验$value是否非空 * @param $value * @return boolean; * if not set ,return true; * if is null , return true; **/ public function checkEmpty($value) { if (!isset($value)) { return true; } if ($value === null) { return true; } if (trim($value) === "") { return true; } return false; } /** * 转换字符集编码 * @param $data * @param $targetCharset * @return string */ public function characet($data) { if (!empty($data)) { $data = mb_convert_encoding($data, "UTF-8", "auto"); } return $data; }
UniApp
t.httpApi(t.p, b).then(function(orderInfo) { uni.requestPayment({ service: 1, _debug: 1, getOrderStatus(status) { resolve({ code: 0 }) }, orderInfo: orderInfo, payChannel: { default_pay_channel: 'alipay' // wx || alipay }, success: function(resPay) { let c = { orderNo: orderNo }; if (resPay.code == 0) { var count = 0, code = NaN; let timer = setInterval(() => { if (count > 3) { clearTimeout(timer) if (code != 1) { uni.showToast({ icon: 'none', title: '支付失败', duration: 1500 }) } return } t.httpApi(t.s, c).then(function(orderStatus) { if (orderStatus.pay_at > 0) { code = 1 } else { code = 0 } }) count++ }, 500); } else { // 关闭支付收银台 uni.showModal({ title: '支付提醒', content: "支付失败", showCancel: false }) } setTimeout(t.getCourseDetail(e), 1500) }, fail: function(err) { // 掉起收银台失败 uni.showToast({ icon: 'none', title: '支付信息错误' }) } }) })

浙公网安备 33010602011771号