PHP实现抖音小程序支付

1.获取自己创建订单的参数

// 获取订单信息
$order = new Order();
$order = $order->where('order_sn', $order_sn)->find();

//传参
//回调地址
$dynotifyUrl=$this->request->root(true) . '/api/pay/ksnotifyx';
//实例化快手引入文件
$dypay= new \library\DypayService($payment, $platform);
//预订单传入参数
$dyresult= $kspay->createOrder($order->order_sn, $order->total_fee,'抖音支付','抖音支付',$openid,$ksnotifyUrl);

return $this->success('获取预付款成功', ['pay_data' => $ksresult,]);

2.DypayService文件

<?php
/**
 * 抖音支付
 */
class DypayService
{
    protected $appid;
    protected $appSecret;
   
    public $data = null;
    public function __construct($payment,$platform = '')
    {
    	//从数据库读取抖音信息
        $paymentConfig = json_decode(\model\Config::get(['name' => $platform])->value, true);

        $this->appid = $paymentConfig['app_id']; //小程序APPID
        $this->appSecret = $paymentConfig['secret']; //小程序的appsecret
    }
    
    /**
     * 预下单
     * @param  [type] $orderid   [description]
     * @param  [type] $price     [description]
     * @param  [type] $subject   [description]
     * @param  [type] $body      [description]
     * @param  [type] $openid    [description]
     * @param  [type] $notifyUrl [description]
     * @return [type]            [description]
     */
    public function createOrder($orderid,$price,$subject,$body,$openid,$notifyUrl){

        $url = 'https://developer.toutiao.com/api/apps/ecpay/v1/create_order';
        $postdata['app_id'] = $this->appid;
        $postdata['out_order_no'] = $orderid;//订单号
        $postdata['total_amount'] = $price * 100 ;//支付金额
        $postdata['subject'] = $subject;
        $postdata['body'] = $body;//备注
        $postdata['valid_time'] = 3600;//订单有效时间
        // $postdata['cp_extra'] = urlencode(json_encode(['aaa'=>234,'bbb'=>'aaa']));
        $postdata['notify_url'] = $notifyUrl;
        $postdata['thirdparty_id'] = '';
        $postdata['disable_msg'] = 0;
        $postdata['msg_page'] = '';
        $postdata['store_uid'] = '';
        $postdata['limit_pay_way'] = 'LIMIT_ALI';//屏蔽支付宝支付
        $postdata['sign'] = $this->sign($postdata);//获取签名

        $res = $this->json_post($url, $postdata);

        return json_decode($res,true);
    }


    public 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 json_post($url, $data = NULL)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!$data) {
            return 'data is null';
        }
        if (is_array($data)) {
            $data = json_encode($data,320);
        }
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length:' . strlen($data),
            'Cache-Control: no-cache',
            'Pragma: no-cache'
        ));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    
}

3.回调方法

	 /**
     * 抖音支付成功回调
     * @ApiInternal
     */
    public function dynotifyx()
    {
        //获取参数
        $result = file_get_contents('php://input');
        $result = json_decode($result, true);
        Log::write('抖音支付成功回调信息:'.json_encode($result, JSON_UNESCAPED_UNICODE),'API');
        if (!$result || empty($result['msg']))

            die('暂无回调信息');
        $platform == 'dyMiniProgram';
        $payment = 'douyin';

        $data = json_decode($result['msg'], true); // 进行签名验证

        // 判断签名是否正确  判断支付状态
        if ($data && $result['type']=='payment') {
            try {
				//进行你的业务逻辑 
                
                //返回抖音成功信息
                $res = [
                    'err_no'=>1,
                    'err_tips'=>"success"
                ];
               
                echo json_encode($res);
                // return $this->payResponse($pay, $payment);
            } catch (\Exception $e) {
                Log::write('notifyx-error:' . json_encode($e->getMessage()));
                 $res = [
                    'err_no'=>400,
                    'err_tips'=>"business fail"
                ];
                echo json_encode($res);
            }

            return $result;

        } else {

            return false;

        }

    }

4.附抖音小程序官方文档 预下单接口: 预下单接口文档
回调接口文档: 回调接口文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值