• 中文
    • English
  • 注册
    • 查看作者
    • wordpress Ripro子比主题对接《元宝支付》第三方聚合支付平台教程

      导读:

      对接之前,请先认真阅读元宝支付接口文档:https://www.18pay.net/doc/pay.html

      Ripro相关文件:

      后台配置类 wp-content\themes\ripro\inc\codestar-framework\options\options.theme.php

      //
      // 商城-支付配置
      //
      CSF::createSection($prefix, array(
          'parent' => 'shop_fields',
          'title'  => '支付配置(NEW)',
          'icon'   => 'fa fa-circle',
          'fields' => array(
      
              // 元宝支付配置
              array(
                  'id'      => 'is_yuanbaopay',
                  'type'    => 'switcher',
                  'title'   => '元宝支付(18pay.net)',
                  'label'   => '是否启用元宝支付,个人免签约支付和第三方聚合支付,支持多种支付方式混合使用。',
                  'default' => false,
              ),
              
              array(
                  'id'         => 'yuanbaopay',
                  'type'       => 'fieldset',
                  'title'      => '配置详情',
                  'fields'     => array(
                      array(
                          'type'    => 'notice',
                          'style'   => 'success',
                          'content' => '元宝支付官网:https://18pay.net <br/>异步通知地址为:' . get_template_directory_uri() . '/shop/yuanbaopay/notify.php',
                      ),
                      array(
                          'id'         => 'yb_payurl',
                          'type'       => 'text',
                          'title'      => '提交地址',
                          'desc'       => 'POST接口提交地址',
                          'default'    => 'https://www.18pay.net/pay.html',
                      ),
                      array(
                        'id'          => 'pay_mode',
                        'type'        => 'select',
                        'title'       => '支付通道',
                        'options'     => array(
                          'all'     => '全部',
                          'alipay'     => '支付宝',
                          'weixin'     => '微信支付',
                        ),
                      ),
                      array(
                          'id'         => 'yb_appid',
                          'type'       => 'text',
                          'title'      => '商户编号',
                          'desc'       => '必填,登录元宝支付平台,商务信息 → 基本资料里查看',
                          'default'    => '',
                      ),
                      array(
                          'id'         => 'yb_appkey',
                          'type'       => 'text',
                          'title'      => '商户密钥',
                          'desc'       => '必填,登录元宝支付平台,商务信息 → 基本资料里查看',
                          'default'    => '',
                          'attributes'  => array(
                              'type'      => 'password',
                              'autocomplete' => 'off',
                          ),
                      ),
                  ),
                  'dependency' => array( 'is_yuanbaopay', '==', 'true' ),
              ),
      
          ),
      ));

      公共方法函数类 wp-content\themes\ripro\inc\theme-functions.php

      /**
       * [_cao_get_pay_type_html 获取后台设置的支付方式HTML]
       * @Author   三七
       * @DateTime 2024-01-01 T10:14:19+0800
       */
      function _cao_get_pay_type_html(){
          $alipay = false;
          $weixinpay = false;
          $alipay_type = 0;
          $wxpay_type = 0;
          if (_cao('is_alipay')) {
              $alipay = true;
              $alipay_type = 1;
          }
          if (_cao('is_weixinpay')) {
              $weixinpay = true;
              $wxpay_type = 2;
          }
          //元宝支付配置支付类型
          if (_cao('is_yuanbaopay')) {
              $yuanbaopay = _cao('yuanbaopay');
              switch ($yuanbaopay['pay_mode']) {
                  case 'all':
                      $alipay = true;
                      $alipay_type = 4;
                      $weixinpay = true;
                      $wxpay_type = 5;
                      break;
                  case 'alipay':
                      $alipay = true;
                      $alipay_type = 4;
                      break;
                  case 'weixin':
                      $weixinpay = true;
                      $wxpay_type = 5;
                      break;
              }
          }
      
      }

      Ajax调用接口类 wp-content\themes\ripro\inc\core-ajax.php

      /**
       * [charge_pay 在线付款支付]
       * @Author   三七
       * @DateTime 2024-01-01 T22:28:59+0800
       */
      function charge_pay(){
      
          //元宝支付:支付宝4 微信支付5
          if ($pay_type == 4 || $pay_type == 5) {
              // 获取后台支付配置
              $config = _cao('yuanbaopay');
               // 添加订单 ShopOrder
              if (!$ShopOrder->add($post_id,$uid, $order_trade_no, $order_type, $order_price, $pay_type)) {
                  echo json_encode(array('status' => '0', 'msg' => '订单创建失败'));exit;
              }
      
              //判断支付方式
              switch ($pay_type) {
                  case '4':
                      $paymethod = 1; // 支付宝
                      break;
                  case '5':
                      $paymethod = 2; // 微信支付
                      break;
              }
              try{
          	    //构造需要传递的参数
                  $params = array(
                      "appid" => $config['yb_appid'],
                      "payid" => $order_trade_no, //唯一标识
                      "type" => $paymethod,//1支付宝 2微信支付 3QQ钱包
                      "money" => $order_price,//金额
          			"subject" => $order_name,//商品名称
                      "notify_url" => get_template_directory_uri() . '/shop/yuanbaopay/notify.php',//异步通知地址
          			"return_url" => get_template_directory_uri() . '/shop/yuanbaopay/return.php',//同步跳转地址
          			"hidetitle" => true
                  ); 
                   if(!isset($_POST['yuanbao'])){
                       $params["native"] = true;
                   }
          		//计算签名
          		$stringTemp = ASCII($params);
          		$params["sign"] = md5($stringTemp . $config['yb_appkey']);
          		//直接跳转到元宝支付
          		if(isset($_POST['yuanbao'])){
          		    $pay_url = $config['yb_payurl']."?".http_build_query($params);
                      header("Location:".$pay_url);
          		}else{
              		//获取扫码支付结果
              		$result = file_post($config['yb_payurl'],$params);
              		$resultData = json_decode($result,true);
              		if ($resultData && $resultData['status'] == 1) {
                  		if ($paymethod == 1) {
                  		    $iconstr = '<img src="'.get_template_directory_uri() . '/assets/icons/alipay.png" class="qr-pay">';
                              $html_str = '<div class="qrcon"> <h5> '.$iconstr.' </h5> <div class="title">支付宝扫码支付 '.$order_price.' 元</div> <div align="center" class="qrcode"> <img src="'.$resultData['code_img'].'"/> </div> <div class="bottom alipay"> 请使用支付宝扫一扫<br>扫描二维码支付</br> </div> </div>';
                              // type 1 = 扫码支付  2 跳转支付
                              echo json_encode(array('status' => '1', 'type' => '1', 'msg' => $html_str , 'img' => $resultData['code_img'], 'num' => $order_trade_no));
                              exit;
                          }else{
                              $iconstr = '<img src="'.get_template_directory_uri() . '/assets/icons/weixin.png" class="qr-pay">';
                              $html_str = '<div class="qrcon"> <h5> '.$iconstr.' </h5> <div class="title">微信扫码支付 '.$order_price.' 元</div> <div align="center" class="qrcode"> <img src="'.$resultData['code_img'].'"/> </div> <div class="bottom weixinpay"> 请使用微信扫一扫<br>扫描二维码支付</br> </div> </div>';
                              // type 1 = 扫码支付  2 跳转支付
                              echo json_encode(array('status' => '1', 'type' => '1', 'msg' => $html_str, 'img' => $resultData['code_img'], 'num' => $order_trade_no));
                              exit;
                          }
              		}else{
                          echo json_encode(array('status' => '0', 'msg' => $resultData['msg']));exit;
                      }
          		}
              } catch (\Exception $e) {
                  echo json_encode(array('status' => '0', 'msg' => "支付异常,Error:".$e->getMessage()));exit;
      	}
          }
      }
      add_action('wp_ajax_charge_pay', 'charge_pay');
      add_action('wp_ajax_nopriv_charge_pay', 'charge_pay');
      /**
       * [go_post_pay 支付模式购买文章]
       * @Author   三七
       * @DateTime 2024-01-01 T22:28:59+0800
       */
      function go_post_pay(){
       
          //元宝支付:支付宝4 微信支付5
          if ($pay_type == 4 || $pay_type == 5) {
              // 获取后台支付配置
              $config = _cao('yuanbaopay');
               // 添加订单 ShopOrder
              if (!$ShopOrder->add($post_id,$uid, $order_trade_no, $order_type, $order_price, $pay_type)) {
                  echo json_encode(array('status' => '0', 'msg' => '订单创建失败'));exit;
              }
      
              //判断支付方式
              switch ($pay_type) {
                  case '4':
                      $paymethod = 1; // 支付宝
                      break;
                  case '5':
                      $paymethod = 2; // 微信支付
                      break;
              }
              try{
          		//构造需要传递的参数
                  $params = array(
                      "appid" => $config['yb_appid'],
                      "payid" => $order_trade_no, //唯一标识
                      "type" => $paymethod,//1支付宝 2微信支付 3QQ钱包
                      "money" => $order_price,//金额
          			"subject" => $order_name,//商品名称
                      "notify_url"=> get_template_directory_uri() . '/shop/yuanbaopay/notify.php',//异步通知地址
          			"return_url" => get_template_directory_uri() . '/shop/yuanbaopay/return.php',//同步跳转地址
          			"hidetitle" => true
                  ); 
                  if(!isset($_POST['yuanbao'])){
                      $params["native"] = true;
                  }
                  //计算签名
          		$stringTemp = ASCII($params);
          		$params["sign"] = md5($stringTemp . $config['yb_appkey']);
          		//直接跳转到元宝支付
          		if(isset($_POST['yuanbao'])){
          		    $pay_url = $config['yb_payurl']."?".http_build_query($params);
                      header("Location:".$pay_url);
          		}else{
              		//获取扫码支付结果
              		$result = file_post($config['yb_payurl'],$params);
              		$resultData = json_decode($result,true);
              		if ($resultData && $resultData['status'] == 1) {
                  		if ($paymethod == 1) {
                  		    $iconstr = '<img src="'.get_template_directory_uri() . '/assets/icons/alipay.png" class="qr-pay">';
                              $html_str = '<div class="qrcon"> <h5> '.$iconstr.' </h5> <div class="title">支付宝扫码支付 '.$order_price.' 元</div> <div align="center" class="qrcode"> <img src="'.$resultData['code_img'].'"/> </div> <div class="bottom alipay"> 请使用支付宝扫一扫<br>扫描二维码支付</br> </div> </div>';
                              // type 1 = 扫码支付  2 跳转支付
                              echo json_encode(array('status' => '1', 'type' => '1', 'msg' => $html_str , 'img' => $resultData['code_img'], 'num' => $order_trade_no));
                              exit;
                          }else{
                              $iconstr = '<img src="'.get_template_directory_uri() . '/assets/icons/weixin.png" class="qr-pay">';
                              $html_str = '<div class="qrcon"> <h5> '.$iconstr.' </h5> <div class="title">微信扫码支付 '.$order_price.' 元</div> <div align="center" class="qrcode"> <img src="'.$resultData['code_img'].'"/> </div> <div class="bottom weixinpay"> 请使用微信扫一扫<br>扫描二维码支付</br> </div> </div>';
                              // type 1 = 扫码支付  2 跳转支付
                              echo json_encode(array('status' => '1', 'type' => '1', 'msg' => $html_str, 'img' => $resultData['code_img'], 'num' => $order_trade_no));
                              exit;
                          }
              		}else{
                          echo json_encode(array('status' => '0', 'msg' => $resultData['msg']));exit;
                      }
                  }
              } catch (\Exception $e) {
                  echo json_encode(array('status' => '0', 'msg' => "支付异常,Error:".$e->getMessage()));exit;
      		}
          }
      }
      add_action('wp_ajax_go_post_pay', 'go_post_pay');
      add_action('wp_ajax_nopriv_go_post_pay', 'go_post_pay');
      /**
      /* 1.将数组内非空参数值的参数按照参数名从小到大排序(ASCII码字典序)
       * 2.然后使URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串
       */
      function ASCII($params = array()){
      	//ksort()对数组按照键名进行升序排序
      	ksort($params);
      	//reset()内部指针指向数组中的第一个元素
      	reset($params);
      	//$sign = http_build_query($params, '', '&amp');
      	$sign = '';//初始化
      	foreach ($params AS $key => $val) { //遍历POST参数
      		if ($val == ''||$key == 'sign') continue; //跳过这些不签名
      		if ($sign) $sign .= '&'; //第一个字符串签名不加& 其他加&连接起来参数
      		$sign .= "$key=$val"; //拼接为url参数形式
      	}
      	return $sign;
      }
      /**
       * file_get_contents发送post请求
       * @param url       请求地址
       * @param postData  要传递的post数据
       */
      function file_post($url, $post_data) {
      	try{
      		$postdata = http_build_query($post_data);
      		$options = array('http' => array(
      		    'method' => 'POST', 
      		    'header' => 'Content-type:application/x-www-form-urlencoded', 
      		    'content' => $postdata, 
      		    'timeout' => 300// 超时时间(单位:s)
      		));
      		$context = stream_context_create($options);
      		$result = file_get_contents($url, false, $context);
      		//去空格
      		$result = trim($result);
      		//转换字符编码
      		$result = mb_convert_encoding($result, 'utf-8', 'UTF-8,GBK,GB2312,BIG5');
      		//解决返回的json字符串中返回了BOM头的不可见字符(某些编辑器默认会加上BOM头)
      		$result = trim($result,chr(239).chr(187).chr(191));
      		return $result;
      	} catch (\Exception $e) {
      		return null;
      	}
      }

      wp-content\themes\ripro\shop\yuanbaopay\notify.php

      <?php
      header('Content-type:text/html; Charset=utf-8');
      date_default_timezone_set('Asia/Shanghai');
      ob_start();
      require_once dirname(__FILE__) . "../../../../../../wp-load.php";
      ob_end_clean();
      
       /**
       * 元宝支付 https://18pay.net/
       * 功能:异步通知页面
       * 版本:1.0
       * 日期:2024-01-01
       */
      
      // 获取后台支付配置
      $config = _cao('yuanbaopay');
      if (empty($_POST)) $_POST = $_GET; //如果为GET方式访问
      $sign = ASCII($_POST);
      if (!$_POST['pay_no'] || md5($sign . $config['yb_appkey']) != $_POST['sign']) { //不合法的数据
      	exit('fail');  //返回失败 继续补单
      } else { //合法的数据
      	//商户本地订单号
      	$out_trade_no = $_POST['pay_id'];
      	//交易号
      	$trade_no = $_POST['pay_no'];
      	//发送支付成功回调用
      	$RiProPay = new RiProPay;
      	$RiProPay->send_order_trade_success($out_trade_no,$trade_no,'ripropaysucc');
      	exit('success'); //返回成功,业务处理完成,下面不再执行了
      }

      wp-content\themes\ripro\shop\yuanbaopay\return.php

      <?php
      header('Content-type:text/html; Charset=utf-8');
      date_default_timezone_set('Asia/Shanghai');
      ob_start();
      require_once dirname(__FILE__) . "../../../../../../wp-load.php";
      ob_end_clean();
      
       /**
       * 元宝支付 https://18pay.net/
       * 功能:同步跳转页面
       * 版本:1.0
       * 日期:2024-01-01
       */
      
      if (empty($_POST)) $_POST = $_GET; //如果为GET方式访问
      // 获取后台支付配置
      $config = _cao('yuanbaopay');
      // 验证签名
      $sign = ASCII($_POST);
      if (!$_POST['pay_no'] || md5($sign . $config['yb_appkey']) != $_POST['sign']) { //不合法的数据
      $pay_id='';
      $pay_money=0;
      $pay_no='';
      $pay_time=date("Y-m-d H:i:s",time());
      $param='';
      $typeName='';   
          $result="支付失败";
      } else { //合法的数据
      $out_trade_no = $_POST['pay_id'];
      $_post_id = 0;
      // 查询本地订单
      $shopOrder = new ShopOrder;
      $postData = $shopOrder->get($out_trade_no);
      if ($postData && $postData['status'] == 1) {
      $_post_id = $postData['post_id'];
      $RiProPay = new RiProPay;
      $RiProPay->AddPayPostCookie($postData['user_id'],$_post_id,$postData['order_trade_no']);
      }
      /*
      if ($_post_id>0) {
      wp_safe_redirect( get_the_permalink( $_post_id ) );
      }else{
      wp_safe_redirect(home_url('/user'));
      }
      */
      $pay_id=$_POST['pay_id'];
      $pay_money=(float)$_POST['pay_money'];
      $pay_no=$_POST['pay_no'];
      $pay_time=date("Y-m-d H:i:s",$_POST['pay_time']);
      $pay_type=(int)$_POST['pay_type'];
      switch ((int)$pay_type) {
      case 1:
      $typeName = '支付宝';
      break;
      case 2:
      $typeName = '微信支付';
      break;
      default:
      $typeName = 'QQ钱包';
      }
      $result="支付成功";
      }
      ?>
      <!DOCTYPE html>
      <html>
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
          <title>支付结果</title>
          <link rel="stylesheet" href="/wp-content/themes/ripro/assets/weui/weui.min.css">
          <link rel="stylesheet" href="/wp-content/themes/ripro/assets/weui/base.css">
      </head>
      <body>
      <div>
          <div><i class="weui-icon-success weui-icon_msg"></i></div>
          <div>
              <h2><?php echo $result?></h2>
              <p>支付金额:<?php echo $pay_money?>元</p>
          </div>
          <div>
      <div>
        <label>订单编号</label>
        <span><?php echo $pay_id?></span>
      </div>
      <div>
        <label>流水编号</label>
        <span><?php echo $pay_no?></span>
      </div>
      <div>
        <label>金额</label>
        <span style="color:red;"><?php echo $pay_money?></span>
      </div>
      <div>
        <label>支付方式</label>
        <span><?php echo $typeName?></span>
      </div>
      <div>
        <label>付款时间</label>
        <span><?php echo $pay_time?></span>
      </div>
      </div>
          <div>
              <div>
                  <p>
                      <a href="javascript:void(0);"></a>
                  </p>
                  <p></p>
              </div>
          </div>
      </div>
      </body>
      </html>

    • 2
    • 1
    • 0
    • 306
    • 似水流年quweisu

      请登录之后再进行评论

      登录
    • 0
      似水流年少侠青铜会员青铜王者
      打赏了10金币
    • 返回顶部
    • 做任务
    • 实时动态
    • 偏好设置
    • 到底部
    • 单栏布局 侧栏位置: