字节抖音小程序支付收款服务端php代码

代码语言:php

所属分类:其他

代码描述:字节抖音小程序支付收款服务端php代码

代码标签: 字节 抖音 小程序 支付 收款 服务端 php 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
Class  ttPay{
   
    private $api_url='https://developer.toutiao.com/api/apps/ecpay/v1/';
    private $app_id;
    private $token;
    private $salt;

    public function __construct() {
        $this->app_id = 'appid';
        $this->token='token';
        $this->salt='salt';
    }        
        
    public function run(){
         $action=addslashes($_GET['ac']);
         $action=$action?$action:'order';
         if(!in_array($action,['order','query','refund','settle','notify','set'])){
            echo '非法请求';die;
        }
        call_user_func(array($this,$action));
    }
    
    //下单
    private function  order(){
        $data=[
            'out_order_no'=>$this->order_number(),
            'total_amount'=>1,
            'subject'=>'测试商111品',
            'body'=>'测试详情',
            'valid_time'=>7200,
        ];
        $res=$this->post('create_order',$data);
        echo json_encode($res);die;
    }
    
    //查询订单
    private function  query(){
        $data=[
            'out_order_no'=>'2021110117254573565'
        ];
        $res=$this->post('query_order',$data,false);
        echo json_encode($res);die;
    }
    
    //订单退款
    private function refund(){
        $data=[
            'out_order_no'=>'2021110118351347832',
            'out_refund_no'=>$this->order_number(),
            'reason'=>'退款原因',
            'refund_amount'=>1,
        ];
        $res=$this->post('create_refund',$data);
        echo json_encode($res);die;
    }
    
    //订单分账
    private function settle(){
        $data=[
            'out_order_no'=>'2021110118301265990',
            'out_settle_no'=>$this->order_number(),
            'settle_desc'=>'分账描述',
            'settle_params'=>json_encode([]),//分润方参数 如[['merchant_uid'=>'商户号','amount'=>'10']]  可以有多个分账商户
        ];
        $res=$this->post('settle',$data);
        echo json_encode($res);die;
    }
    
    //支付设置回调测试
    private function set(){
        $content=file_get_contents('php://input');
        $this->log('log.txt',$content);
    }
    
    //回调
    private function notify(){
        $content=file_get_contents('php://input');
        if(empty($content)) return false;
        $this->log('notify.txt',$content);
        $content=json_decode($content,true);
        $sign=$this->handler($content);
        if($sign==$content['msg_signature']){
            $msg=json_decode($content['msg'],true); 
            echo '回调----'.$content['type']."\n";
            //这里更新应用业务逻辑代码,使用$msg跟应用订单比对更新订单,可以用 $content['type']判断是支付回调还是退款回调,payment支付回调 refund退款回调。
            $res=['err_no'=>0,'err_tips'=>'success'];
            echo json_encode($res);
        }
    }
    
    
    /**
    * 测试订单号,实际应用根据自己应用实际生成
    * @return string
    */
    private function order_number(){
        return date('YmdHis').rand(10000,99999);
    }
    
     /**
     * 请求小程序平台服务端
     * @param string $url 接口地址
     * @param array $data 参数内容
     * @param boolean $notify 是否有回调
     * @return array
    */
    private function post($method,$data,$notify=true){
        $data['app_id']=$this->app_id;
        if(!empty($notify)){
            $data['notify_url']='';//也可以在调用的时候分别设置
        }
        $data['sign']=$this->sign($data);
        $url=$this->api_url.$method;
        $res=$this->http('POST',$url,json_encod.........完整代码请登录后点击上方下载按钮下载查看

网友评论0