QQ咨询 官方微信

添加微信好友

官方小程序

百度小程序

微信小程序

头条小程序

关于我们

抖音小程序

头条/抖音小程序担保支付

 admin  2023-05-22 21:30:16
首先把配置文件填写完整:
  1. // 支付相关配置 
  2. private static $config = array( 
  3.     'app_id'        => ''// App ID 
  4.     'salt'          => ''// 支付密钥值 
  5.     'notify_url'    => ''// 支付回调地址 
  6.     'thirdparty_id' => ''// 第三方平台服务商 id,非服务商模式留空 
  7. ); 
支付类封装相关方法:

method 描述
createOrder 下单支付
queryOrder 订单查询
notifyOrder 订单回调验证
createRefund 订单退款
queryRefund 退款查询
settle 分账请求
querySettle 分账查询

使用方法:
  1. <?php 
  2. require_once('./vendor/autoload.php');  
  3. $config = []; // 支付配置 
  4. $order = [ 
  5.     'order_sn' => time(), // 订单编号 
  6.     'total_amount' => 1, // 订单金额(分) 
  7.     'body' => '测试商品'// 商品名称 
  8. ]; 
  9.  
  10. $wechat = new fengkui\Pay\Bytedance($config); 
  11. $re = $wechat->createOrder($order); 
  12. die(json_encode($re)); // JSON化直接返回小程序客户端 
如下代码是封装好的完整支付类文件(Bytedance.php), 可以根据自己需求随意修改,详细的使用方法后期会有文档:
  1. <?php 
  2. /** 
  3. * @Author: [FENG] <1161634940@qq.com> 
  4. * @Date:   2020-05-13 17:02:49 
  5. * @Last Modified by:   [FENG] <1161634940@qq.com> 
  6. * @Last Modified time: 2021-06-15T17:06:18+08:00 
  7. */ 
  8. namespace fengkui\Pay; 
  9. use fengkui\Supports\Http; 
  10. /** 
  11. * Bytedance 字节跳动支付 
  12. * 小程序担保支付(V1) 
  13. */ 
  14. class Bytedance 
  15.     // 接口版本 
  16.     const EDITON = 'v1'
  17.     // 统一下订单管理 
  18.     private static $ecpayUrl = 'https://developer.toutiao.com/api/apps/ecpay/'
  19.     // 服务端预下单 
  20.     private static $createOrderUrl = 'https://developer.toutiao.com/api/apps/ecpay/v1/create_order'
  21.     // 订单查询 
  22.     private static $queryOrderUrl = 'https://developer.toutiao.com/api/apps/ecpay/v1/query_order'
  23.     // 退款 
  24.     private static $createRefundUrl = 'https://developer.toutiao.com/api/apps/ecpay/v1/create_refund'
  25.     // 查询退款 
  26.     private static $queryRefundUrl = 'https://developer.toutiao.com/api/apps/ecpay/v1/query_refund'
  27.     // 分账请求 
  28.     private static $settleUrl = 'https://developer.toutiao.com/api/apps/ecpay/v1/settle'
  29.     // 查询分账 
  30.     private static $querySettleUrl = 'https://developer.toutiao.com/api/apps/ecpay/v1/query_settle'
  31.     // 服务商进件 
  32.     private static $addMerchantUrl = 'https://developer.toutiao.com/api/apps/ecpay/saas/add_merchant'
  33.     // 分账方进件 
  34.     private static $addSubMerchantUrl = 'https://developer.toutiao.com/api/apps/ecpay/saas/add_sub_merchant'
  35.     // 支付相关配置 
  36.     private static $config = array( 
  37.         'app_id'        => ''// App ID 
  38.         'salt'          => ''// 支付密钥值 
  39.         'notify_url'    => ''// 支付回调地址 
  40.         'thirdparty_id' => ''// 第三方平台服务商 id,非服务商模式留空 
  41.     ); 
  42.     /** 
  43.     * [__construct 构造函数] 
  44.     * @param [type] $config [传递支付相关配置] 
  45.     */ 
  46.     public function __construct($config=NULL){ 
  47.         $config && self::$config = array_merge(self::$config, $config); 
  48.     } 
  49.     /** 
  50.     * [createOrder 下单支付] 
  51.     * @param  [type] $order [description] 
  52.     * @return [type]        [description] 
  53.     * $order = array( 
  54.     *      'body'         => '', // 产品描述 
  55.     *      'total_amount' => '', // 订单金额(分) 
  56.     *      'order_sn'     => '', // 订单编号 
  57.     * ); 
  58.     */ 
  59.     public static function createOrder($order) 
  60.     { 
  61.         $config = self::$config; 
  62.         $params = [ 
  63.             'app_id'        => $config['app_id'], // 是 小程序 AppID 
  64.             'out_order_no'  => (string)$order['order_sn'], // 是 开发者侧的订单号, 同一小程序下不可重复 
  65.             'total_amount'  => $order['total_amount'], // 是 支付价格; 接口中参数支付金额单位为[分] 
  66.             'subject'       => $order['body'], // 是 商品描述; 长度限制 128 字节,不超过 42 个汉字 
  67.             'body'          => $order['body'], // 是 商品详情 
  68.             'valid_time'    => 3600 * 2, // 是 订单过期时间(秒); 最小 15 分钟,最大两天 
  69.             // 'sign'          => '', // 是 开发者对核心字段签名, 签名方式见文档附录, 防止传输过程中出现意外 
  70.             // 'cp_extra'      => '', // 否 开发者自定义字段,回调原样回传 
  71.             // 'notify_url'    => $config['notify_url'], // 否 商户自定义回调地址 
  72.             // 'thirdparty_id' => '', // 否 第三方平台服务商 id,非服务商模式留空 
  73.             'disable_msg'   => 1, // 否 是否屏蔽担保支付的推送消息,1-屏蔽 0-非屏蔽,接入 POI 必传 
  74.             // 'msg_page'      => '', // 否 担保支付消息跳转页 
  75.             // 'store_uid'     => '', // 否 多门店模式下,门店 uid 
  76.         ]; 
  77.         !empty($order['cp_extra']) && $params['cp_extra'] = $order['cp_extra']; 
  78.         !empty($config['notify_url']) && $params['notify_url'] = $config['notify_url']; 
  79.         !empty($config['thirdparty_id']) && $params['thirdparty_id'] = $config['thirdparty_id']; 
  80.         if (!empty($config['msg_page'])) { 
  81.             $params['disable_msg'] = 0; 
  82.             $params['msg_page'] = $config['msg_page']; 
  83.         } 
  84.         $params['sign'] = self::makeSign($params); 
  85.         // dump($params);die; 
  86.         $url = self::$createOrderUrl; 
  87.         $response = Http::post($url, json_encode($params)); 
  88.         $result = json_decode($response, true); 
  89.         return $result; 
  90.     } 
  91.     /** 
  92.     * [queryOrder 订单查询] 
  93.     * @param  [type] $orderSn [开发者侧的订单号, 不可重复] 
  94.     * @return [type]          [description] 
  95.     */ 
  96.     public static function queryOrder($orderSn) 
  97.     { 
  98.         $config = self::$config; 
  99.         $params = [ 
  100.             'app_id' => $config['app_id'], // 小程序 AppID 
  101.             'out_order_no' => (string)$orderSn, // 开发者侧的订单号, 不可重复 
  102.             // 'sign' => '', // 开发者对核心字段签名, 签名方式见文档, 防止传输过程中出现意外 
  103.             // 'thirdparty_id' => '', // 服务商模式接入必传   第三方平台服务商 id,非服务商模式留空 
  104.         ]; 
  105.         !empty($config['thirdparty_id']) && $params['thirdparty_id'] = $config['thirdparty_id']; 
  106.         $params['sign'] = self::makeSign($params); 
  107.         $url = self::$queryOrderUrl; 
  108.         $response = Http::post($url, json_encode($params)); 
  109.         $result = json_decode($response, true); 
  110.         return $result; 
  111.     } 
  112.     /** 
  113.     * [notifyOrder 订单回调验证] 
  114.     * @return [array] [返回数组格式的notify数据] 
  115.     */ 
  116.     public static function notifyOrder() 
  117.     { 
  118.         $data = $_POST; // 获取回调数据 
  119.         $config = self::$config; 
  120.         if (!$data || empty($data['msg'])) 
  121.         die('暂无回调信息'); 
  122.         $result = json_decode($data['msg'], true); // 进行签名验证 
  123.         // 判断签名是否正确  判断支付状态 
  124.         if ($result && $data['type']=='payment') { 
  125.             return $data; 
  126.         } else { 
  127.             return false
  128.         } 
  129.     } 
  130.     /** 
  131.     * [createRefund 订单退款] 
  132.     * @param  [type] $order [订单相关信息] 
  133.     * @return [type]        [description] 
  134.     * $order = array( 
  135.     *      'order_sn'     => '', // 订单编号 
  136.     *      'refund_sn'    => '', // 退款编号 
  137.     *      'total_amount' => '', // 订单金额(分) 
  138.     *      'body'         => '', // 退款原因 
  139.     * ); 
  140.     */ 
  141.     public static function createRefund($order) 
  142.     { 
  143.         $config = self::$config; 
  144.         $params = [ 
  145.             'app_id'        => $config['app_id'], // 是   小程序 id 
  146.             'out_order_no'  => (string)$order['order_sn'], // 是  商户分配订单号,标识进行退款的订单 
  147.             'out_refund_no' => (string)$order['refund_sn'], // 是 商户分配退款号 
  148.             'refund_amount' => $order['total_amount'], // 是  退款金额,单位[分] 
  149.             'reason'        => $order['body'] ?? '用户申请退款'// 是  退款理由,长度上限 100 
  150.             // 'cp_extra'      => '', // 否   开发者自定义字段,回调原样回传 
  151.             // 'notify_url'    => '', // 否   商户自定义回调地址 
  152.             // 'sign'          => '', // 是   开发者对核心字段签名, 签名方式见文档, 防止传输过程中出现意外 
  153.             // 'thirdparty_id' => '', // 否,服务商模式接入必传 第三方平台服务商 id,非服务商模式留空 
  154.             'disable_msg'   => 1, // 否   是否屏蔽担保支付消息,1-屏蔽 
  155.             // 'msg_page'      => '', // 否   担保支付消息跳转页 
  156.             // 'all_settle'    => '', // 否   是否为分账后退款,1-分账后退款;0-分账前退款。分账后退款会扣减可提现金额,请保证余额充足 
  157.         ]; 
  158.         !empty($order['cp_extra']) && $params['cp_extra'] = $order['cp_extra']; 
  159.         !empty($order['all_settle']) && $params['all_settle'] = $order['all_settle']; 
  160.         !empty($config['thirdparty_id']) && $params['thirdparty_id'] = $config['thirdparty_id']; 
  161.         if (!empty($config['msg_page'])) { 
  162.             $params['disable_msg'] = 0; 
  163.             $params['msg_page'] = $config['msg_page']; 
  164.         } 
  165.         $params['sign'] = self::makeSign($params); 
  166.         $url = self::$queryOrderUrl; 
  167.         $response = Http::post($url, json_encode($params)); 
  168.         $result = json_decode($response, true); 
  169.         return $result; 
  170.     } 
  171.     /** 
  172.     * [queryRefund 退款查询] 
  173.     * @param  [type] $refundSn [开发者侧的订单号, 不可重复] 
  174.     * @return [type]           [description] 
  175.     */ 
  176.     public static function queryRefund($refundSn) 
  177.     { 
  178.         $config = self::$config; 
  179.         $params = [ 
  180.             'app_id' => $config['app_id'], // 小程序 AppID 
  181.             'out_refund_no' => $refundSn, // 开发者侧的退款号 
  182.             // 'sign' => '', // 开发者对核心字段签名, 签名方式见文档, 防止传输过程中出现意外 
  183.             // 'thirdparty_id' => '', // 服务商模式接入必传   第三方平台服务商 id,非服务商模式留空 
  184.         ]; 
  185.         !empty($config['thirdparty_id']) && $params['thirdparty_id'] = $config['thirdparty_id']; 
  186.         $params['sign'] = self::makeSign($params); 
  187.         $url = self::$queryRefundUrl; 
  188.         $response = Http::post($url, json_encode($params)); 
  189.         $result = json_decode($response, true); 
  190.         return $result; 
  191.     } 
  192.     /** 
  193.     * [notifyRefund 退款回调验证] 
  194.     * @return [array] [返回数组格式的notify数据] 
  195.     */ 
  196.     public static function notifyRefund() 
  197.     { 
  198.         $data = $_POST; // 获取回调数据 
  199.         $config = self::$config; 
  200.         if (!$data || empty($data['status'])) 
  201.         die('暂无回调信息'); 
  202.         $result = json_decode($data['msg'], true); // 进行签名验证 
  203.         // 判断签名是否正确  判断支付状态 
  204.         if ($result && $data['status']!='FAIL') { 
  205.             return $data; 
  206.         } else { 
  207.             return false
  208.         } 
  209.     } 
  210.     /** 
  211.     * [settle 分账请求] 
  212.     * @param  [type] $order [分账信息] 
  213.     * @return [type]        [description] 
  214.     * $order = array( 
  215.     *      'body'         => '', // 产品描述 
  216.     *      'total_amount' => '', // 订单金额(分) 
  217.     *      'order_sn'     => '', // 订单编号 
  218.     * ); 
  219.     */ 
  220.     public static function settle($order) 
  221.     { 
  222.         $config = self::$config; 
  223.         $params = [ 
  224.             'app_id'        => $config['app_id'], // 是 小程序 AppID 
  225.             'out_order_no'  => (string)$order['order_sn'], // 是 商户分配订单号,标识进行结算的订单 
  226.             'out_settle_no' => (string)$order['settle_sn'], // 是 开发者侧的结算号, 不可重复 
  227.             'settle_desc'   => $order['body'], // 是  结算描述,长度限制 80 个字符 
  228.             // 'cp_extra'      => '', // 否   开发者自定义字段,回调原样回传 
  229.             // 'notify_url'    => '', // 否   商户自定义回调地址 
  230.             // 'sign'          => '', // 是   开发者对核心字段签名, 签名方式见文档, 防止传输过程中出现意外 
  231.             // 'thirdparty_id' => '', // 否,服务商模式接入必传 第三方平台服务商 id,非服务商模式留空 
  232.             // 'settle_params' => '', // 否,其他分账方信息,分账分配参数 SettleParameter 数组序列化后生成的 json 格式字符串 
  233.         ]; 
  234.         !empty($order['cp_extra']) && $params['cp_extra'] = $order['cp_extra']; 
  235.         !empty($order['settle_params']) && $params['settle_params'] = $order['settle_params']; 
  236.         !empty($config['thirdparty_id']) && $params['thirdparty_id'] = $config['thirdparty_id']; 
  237.         $params['sign'] = self::makeSign($params); 
  238.         $url = self::$settleUrl; 
  239.         $response = Http::post($url, json_encode($params)); 
  240.         $result = json_decode($response, true); 
  241.         return $result; 
  242.     } 
  243.     /** 
  244.     * [querySettle 分账查询] 
  245.     * @param  [type] $settleSn [开发者侧的订单号, 不可重复] 
  246.     * @return [type]          [description] 
  247.     */ 
  248.     public static function querySettle($settleSn) 
  249.     { 
  250.         $config = self::$config; 
  251.         $params = [ 
  252.             'app_id' => $config['app_id'], // 小程序 AppID 
  253.             'out_settle_no' => $settleSn, // 开发者侧的分账号 
  254.             // 'sign' => '', // 开发者对核心字段签名, 签名方式见文档, 防止传输过程中出现意外 
  255.             // 'thirdparty_id' => '', // 服务商模式接入必传   第三方平台服务商 id,非服务商模式留空 
  256.         ]; 
  257.         !empty($config['thirdparty_id']) && $params['thirdparty_id'] = $config['thirdparty_id']; 
  258.         $params['sign'] = self::makeSign($params); 
  259.         $url = self::$querySettleUrl; 
  260.         $response = Http::post($url, json_encode($params)); 
  261.         $result = json_decode($response, true); 
  262.         return $result; 
  263.     } 
  264.     /** 
  265.     * [notifySettle 分账回调验证] 
  266.     * @return [array] [返回数组格式的notify数据] 
  267.     */ 
  268.     public static function notifySettle() 
  269.     { 
  270.         $data = $_POST; // 获取回调数据 
  271.         $config = self::$config; 
  272.         if (!$data || empty($data['status'])) 
  273.         die('暂无回调信息'); 
  274.         $result = json_decode($data['msg'], true); // 进行签名验证 
  275.         // 判断签名是否正确  判断支付状态 
  276.         if ($result && $data['status']!='FAIL') { 
  277.             return $data; 
  278.         } else { 
  279.             return false
  280.         } 
  281.     } 
  282.     /** 
  283.     * [addMerchant 服务商进件] 
  284.     * @param [type]  $accessToken [授权码兑换接口调用凭证] 
  285.     * @param [type]  $componentId [小程序第三方平台应用] 
  286.     * @param integer $urlType     [链接类型:1-进件页面 2-账户余额页] 
  287.     */ 
  288.     public static function addMerchant($accessToken, $componentId, $urlType=1) 
  289.     { 
  290.         $params = [ 
  291.             'component_access_token' => $accessToken, // 是   授权码兑换接口调用凭证 
  292.             'thirdparty_component_id' => $componentId, // 是  小程序第三方平台应用 id 
  293.             'url_type' => $urlType, // 是 链接类型:1-进件页面 2-账户余额页 
  294.         ]; 
  295.         $url = self::$addMerchantUrl; 
  296.         $response = Http::post($url, json_encode($params)); 
  297.         $result = json_decode($response, true); 
  298.         return $result; 
  299.     } 
  300.     /** 
  301.     * [addSubMerchant 分账方进件] 
  302.     * @param [type]  $thirdpartyId [小程序第三方平台应用] 
  303.     * @param [type]  $merchantId   [商户 id,用于接入方自行标识并管理进件方。由服务商自行分配管理] 
  304.     * @param integer $urlType      [链接类型:1-进件页面 2-账户余额页] 
  305.     */ 
  306.     public static function addSubMerchant($thirdpartyId, $merchantId, $urlType=1) 
  307.     { 
  308.         $params = [ 
  309.             'thirdparty_id' => $thirdpartyId, // 是   小程序第三方平台应用 id 
  310.             'sub_merchant_id' => $merchantId, // 是   商户 id,用于接入方自行标识并管理进件方。由服务商自行分配管理 
  311.             'url_type' => $urlType, // 是 链接类型:1-进件页面 2-账户余额页 
  312.             // 'sign' => '', // 开发者对核心字段签名, 签名方式见文档, 防止传输过程中出现意外 
  313.         ]; 
  314.         $params['sign'] = self::makeSign($params); 
  315.         $url = self::$addSubMerchantUrl; 
  316.         $response = Http::post($url, json_encode($params)); 
  317.         $result = json_decode($response, true); 
  318.         return $result; 
  319.     } 
  320.     /** 
  321.     * [success 通知状态] 
  322.     */ 
  323.     public static function success() 
  324.     { 
  325.         $array = ['err_no'=>0, 'err_tips'=>'success']; 
  326.         die(json_encode($array)); 
  327.     } 
  328.     /** 
  329.     * [makeSign 生成秘钥] 
  330.     * @param  [type] $data [加密数据] 
  331.     * @return [type]       [description] 
  332.     */ 
  333.     public static function makeSign($data) { 
  334.         $config = self::$config; 
  335.         $rList = array(); 
  336.         foreach($data as $k => $v) { 
  337.             if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id"
  338.                 continue
  339.             $value = trim(strval($v)); 
  340.             $len = strlen($value); 
  341.             if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\""
  342.                 $value = substr($value,1, $len-1); 
  343.             $value = trim($value); 
  344.             if ($value == "" || $value == "null"
  345.             continue
  346.             array_push($rList, $value); 
  347.         } 
  348.         array_push($rList, $config['salt']); 
  349.         sort($rList, 2); 
  350.         return md5(implode('&', $rList)); 
  351.     } 

我自己实际操作的代码分享:

下单:
  1. //新版本担保交易 
  2.         public function createOrder($orderid,$price,$subject,$body,$openid,$notifyUrl){ 
  3.             $time = time(); 
  4.             $price = $price*100; 
  5.             $data = [ 
  6.                 'app_id' => $this->ttAppid, 
  7.                 'out_order_no' =>  $orderid, //订单号 
  8.                 'total_amount' => $price,  //金额 单位:分 
  9.                 'body' => $body,           //支付的内容 
  10.                 'subject' => $subject,     //支付的标题 
  11.                 'valid_time' => 3600, 
  12.                 'notify_url'=> $notifyUrl, 
  13.             ]; 
  14.             $data['sign'] = $this->MakeSign($data); 
  15.             $url = "https://developer.toutiao.com/api/apps/ecpay/v1/create_order"; // 请求地址正式环境 
  16.             $result = $this->curlPost($url, $data); 
  17.             echo $result; 
  18.             exit(); 
  19.             return json_decode($result,true); 
  20.         } 
签名:
  1. /** 
  2.          * 生成签名 
  3.          *  @return 签名 
  4.          */ 
  5.         public function MakeSign( $params ){ 
  6.             $rList = array(); 
  7.             foreach($params as $k =>$v) { 
  8.                 if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id"
  9.                     continue
  10.                 $value = trim(strval($v)); 
  11.                 $len = strlen($value); 
  12.                 if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\""
  13.                     $value = substr($value,1, $len-1); 
  14.                 $value = trim($value); 
  15.                 if ($value == "" || $value == "null"
  16.                     continue
  17.                 array_push($rList, $value); 
  18.             } 
  19.             array_push($rList, $this->ttPayAppSecret); 
  20.             sort($rList, 2); 
  21.             return md5(implode('&', $rList)); 
  22.         } 
回调及验签:
  1. //回调 
  2.         public function notify($notify) 
  3.         { 
  4.             if ($notify['msg_signature'] !== $this->getNotifySign($notify)) { 
  5.                return false
  6.             }  
  7.             return true
  8.         } 
  9.  
  10.         /** 
  11.          * 回调验签 
  12.          */ 
  13.         public function getNotifySign($body){ 
  14.             $filtered = []; 
  15.             foreach ($body as $key => $value) { 
  16.                 if (in_array($key, ['msg_signature''type'])) { 
  17.                     continue
  18.                 } 
  19.                 $filtered[] = is_string($value) ? trim($value): $value; 
  20.             } 
  21.             $filtered[] = trim($this->ttPayAppSecret); 
  22.             sort($filtered, SORT_STRING); 
  23.             $filtered = trim(implode('', $filtered)); 
  24.             return sha1($filtered); 
  25.         } 

¥ 打赏
×
如果您觉得文章帮助了您就打赏一下吧
非常感谢你的打赏,我们将继续分享更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

本文《头条/抖音小程序担保支付》发布于石头博客文章,作者:admin,如若转载,请注明出处:https://www.pweb123.com/xiaocheng/douyin/840.html,否则禁止转载,谢谢配合!

您可能感兴趣

文章点评

我来说两句 已有1条评论
点击图片更换
石头博客网友 2022-06-02 10:19:33
回复 支持0
经过测试是可以直接使用的

添加微信好友

添加微信好友

微信小程序

百度小程序