您的当前位置:首页正文

PHP小程序汇聚支付简单使用

2024-11-07 来源:个人技术集锦

简单的封装了一下在小程序使用汇聚支付

<?php


class JoinPay
{
    public $join_mch_id = '';

    public $join_mch_secret = '';

    public $join_mch_no = '';

    public $wxapp_appid = '';

    // 支付url
    public $pay_url = 'https://www.joinpay.com/trade/uniPayApi.action';

    // 退款url
    public $refund_url = 'https://www.joinpay.com/trade/refund.action';


    /**
     * join_mch_id      商户编号
     * join_mch_secret  商户秘钥
     * join_mch_no      商户报备号
     * wxapp_appid      小程序appid
     */
    public function __construct($join_mch_id, $join_mch_secret, $join_mch_no = '', $wxapp_appid = '')
    {
        $this->join_mch_id     = $join_mch_id;
        $this->join_mch_secret = $join_mch_secret;
        $this->join_mch_no     = $join_mch_no;
        $this->wxapp_appid     = $wxapp_appid;
    }


    /**
     * order_sn    订单号
     * price       订单价格
     * openid      用户openid
     * goods_name  商品名称
     * goods_desc  商品描述
     * notice_url  回调响应地址
     */
    public function pay($order_sn, $price, $openid, $goods_name, $goods_desc, $notice_url)
    {
        $params["p0_Version"] = "1.0";
        $params["p1_MerchantNo"] = $this->join_mch_id;
        $params["p2_OrderNo"] = $order_sn;
        $params["p3_Amount"] = $price;
        $params["p4_Cur"] = "1";
        $params["p5_ProductName"] = $goods_name;
        $params["p6_ProductDesc"] = $goods_desc;
        $params["p9_NotifyUrl"] = $notice_url;
        $params["q1_FrpCode"] = "WEIXIN_XCX";
        $params["q5_OpenId"] = $openid;
        $params["q7_AppId"] = $this->wxapp_appid;
        $params['qa_TradeMerchantNo'] = $this->join_mch_no;

        $hmacVal = urlencode($this->hmacRequest($params, $this->join_mch_secret));
        $params['hmac'] = $hmacVal;

        $result = app_curl($this->pay_url, $params);

        if ($result['ra_Code'] != 100) {
            return ['code'=>500,'msg'=>$result['rb_CodeMsg'],'data'=>[]];
        }

        $pay_info = json_decode($result['rc_Result'], true);

        return ['code'=>0,'msg'=>'success','data'=>$pay_info];
    }


    /**
     * order_sn      订单号
     * refund_sn     退款号
     * refund_price  退款金额
     * refund_reason 退款理由
     * notice_url    回调响应地址
     */
    public function refund($order_sn, $refund_sn, $refund_price, $refund_reason, $notice_url)
    {
        
        $params["p1_MerchantNo"] = $this->join_mch_id;
        $params["p2_OrderNo"] = $order_sn;
        $params["p3_RefundOrderNo"] = $refund_sn;
        $params["p4_RefundAmount"] = $refund_price;
        $params["p5_RefundReason"] = $refund_reason ?? '原料不足退款';
        $params["p6_NotifyUrl"] = $notice_url;
        $hmacVal = urlencode($this->hmacRequest($params, $this->join_mch_secret));
        $params['hmac'] = $hmacVal;

        $result = app_curl($this->refund_url, $params);

        $return = [
            'code' => 500,
            'msg' => '退款失败',
            'data' => []
        ];

        if ($result['ra_Status'] == 100) {
            $return['code'] = 0;
            $return['msg'] = '退款成功';
        }

        return $return;
    }


    public function hmacRequest($params, $key, $encryptType = "1")
    {
        if ("1" == $encryptType) {
            return md5(implode("", $params) . $key);
        } else {
            $private_key = openssl_pkey_get_private($key);
            $params = implode("", $params);
            openssl_sign($params, $sign, $private_key, OPENSSL_ALGO_MD5);
            openssl_free_key($private_key);
            $sign = base64_encode($sign);
            return $sign;
        }

    }

}

显示全文