woocommerce主题对接易支付插件

woocommerce主题对接易支付插件

admin
8月7日发布

写这个插件是五年前的事了,最近陆陆续续有人通过各种渠道联系我询问使用方法,让我觉得更新一下插件也是值得的事情。更新后的插件更名为 freepay-woocommerce,意为Woocommerce下的免签约支付,本次更新除了增加功能、修复bug、精简代码外,还支持了目前市面上流行的易支付,本页文档我也就重写了。
1729060247-e04efd90cb41f31-768x451.png

更新

  1. 支持码支付和易支付两种免签约方式。 默认开
  2. 启码支付的支付宝、微信、QQ钱包支付,易支付默认关闭,可以后台手动开启。
  3. 一个插件就能开启所有的支付方式,也可以自行选择开启/关闭任意一种支付方式,更灵活。例如你可以选择开启/关闭码支付的支付宝和微信 + 易支付的QQ钱包。
  4. 管理员的订单详情页内自动添加备注,用户何时准备支付、以何种方式支付、何时支付等一目了然。针对码支付,还能知道用户是否足额支付,多支付或者少支付多少金额。
  5. 判断订单状态,避免重复业务。
  6. 其他。。。

使用方法

使用方法跟码支付相似,使用可以参考码支付。这里说重要的东西:网关地址。市面上有很多易支付平台,只是改了一个主页取了个名字,插件提供通用的易支付对接接口,只要填写平台的网关地址即可对接成功。举个例子:如果本站做了易支付,网关域名为pay.mebi.me,那么网关地址为:https://pay.mebi.me/,不要忘了结尾的“/”。

代码片段:

<?php 
class WC_Gateway_Easypay_zfb extends WC_Payment_Gateway
{
    public function __construct() {
        // 必要的字段
        $this->id = 'easypay_zfb';
        $this->icon = WC_Freepay_URL.'/assets/logo/zfb-logo.jpg';
        $this->has_fields = false;
        $this->method_title = '易支付——支付宝';
        $this->method_description = '易支付——支付宝网关设置';
        
        // 必须调用的方法
        $this->init_form_fields();
        $this->init_settings();
        
        // 设置request或者response对象会用到的变量
        $this->title            = $this->get_option('title');
        $this->description      = $this->get_option('description');
        
        // 保存后台设置的数据
        add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );

        // 实例化异步通知的处理 
        new WC_Gateway_Easypay_Notify($this);
    }


     //配置列表项
    public function init_form_fields() {
        $this->form_fields = array(
            'enabled' => array(
                    'title'   => "启用/禁用",
                    'type'    => 'checkbox',
                    'label'   => "是否启用支付宝网关,默认为禁用。",
                    'default' => 'no',
            ),
            'title' => array(
                    'title'       => "标题",
                    'type'        => 'text',
                    'description' => '在结算时看到的当前支付方式的名称',
                    'default'     => '支付宝支付',
                    'desc_tip'    => true,
            ),
            'description' => array(
                    'title'       => '描述',
                    'type'        => 'text',
                    'desc_tip'    => true,
                    'description' => '结算时当前支付方式的描述',
                    'default'     => '本次交易将使用支付宝付款',
            )
            
        );
    }


    //核心订单处理
    public function process_payment($order_id)
    {
        global $woocommerce;
        $order = wc_get_order($order_id);
        $price = $order -> order_total;
        $order_items = $order->get_items();
        $product_num = count($order_items);
        foreach ($order_items as $item_id => $item_data) {
            $product = $item_data->get_product();
            $product_name = $product->get_name();
            break;
        }
        if ($product_num > 1)
        {
            $product_name .= "等".$product_num."件商品";
        }
        $type = "alipay";
        require("inc/easypay.config.php");
        require_once("lib/epay_submit.class.php");
        $parameter = array(
            "pid" => trim($alipay_config['partner']),
            "type" => $type,
            "notify_url"    => plugin_dir_url( __FILE__ )."inc/easypay_return.php",
            "return_url"    => home_url()."/wc-api/wc_gateway_easypay",
            "out_trade_no"    => $order_id,
            "name" => $product_name,
            "money"    => $price,
    );

        $alipaySubmit = new AlipaySubmit($alipay_config);
        // add note for payment
        $msg = '顾客正在使用易支付-支付宝方式进行支付';
        $order->add_order_note($msg);
        $woocommerce->cart->empty_cart();      //清空购物车
        $para = urlencode(json_encode($alipaySubmit->payment_redirect($parameter)));
        return array(
            'result'    =>  'success',
            'redirect'  =>  plugin_dir_url( __FILE__ ).'payment_redirect.php?para='.$para
        );
    }
}
?>

下载地址:

© 版权声明
THE END
喜欢就支持一下吧
点赞 0 分享 收藏
评论 抢沙发
OωO
取消 登录评论
SSL