阅读 48

PHP微信三方平台-代公众号发起微信支付(jsAPI)

一、前期准备工作

1、微信公众号需要开通微信支付认证将获取的秘钥给三方平台

2、添加支付回调域名地址:填写三方平台域名地址即可(最多5个)

 

二、代码demo

1、完成支付类

php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/20 0020
 * Time: 17:25
 */

namespace app\models;


class Payment
{

    public static function WxPayInit($body, $trade_no, $total_fee, $open_id, $notify_url, $attach = [])
    {

        $post_params = [
            ‘body‘ => $body,
            ‘trade_no‘ => $trade_no,
            ‘total_fee‘ => $total_fee * 100,
            ‘open_id‘ => $open_id,
            ‘attach‘ => $attach,
            ‘notify_url‘ => \Yii::$app->request->hostInfo . \Yii::$app->request->scriptUrl . $notify_url
        ];
        return self::WxPay($post_params);
    }

    public static function WxPay($data)
    {
        $hywx_test = \Yii::$app->params[‘hywx_test‘];
        $pay_config = [
            ‘APPID‘ => $hywx_test[‘app_id‘],
            ‘MCHID‘ => $hywx_test[‘mch_id‘],
            ‘MCHKEY‘ => $hywx_test[‘mch_key‘]
        ];

        $attach = json_encode($data[‘attach‘]);
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($data[‘body‘]);
        $input->SetAttach($attach);
        $input->SetOut_trade_no($data[‘trade_no‘]);
        $input->SetTotal_fee($data[‘total_fee‘]);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetNotify_url($data[‘notify_url‘]);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($data[‘open_id‘]);
        $result = \WxPayApi::unifiedOrder($input, $pay_config);
        if ($result[‘result_code‘] == ‘SUCCESS‘ && $result[‘return_code‘] == ‘SUCCESS‘) {

            $jsapi = new  \WxPayJsApiPay();
            $jsapi->SetAppid($result["appid"]);
            $timeStamp = time();
            $jsapi->SetTimeStamp("$timeStamp");
            $jsapi->SetNonceStr(\WxPayApi::getNonceStr());
            $jsapi->SetPackage("prepay_id=" . $result[‘prepay_id‘]);
            $jsapi->SetSignType("MD5");
            $jsapi->SetPaySign($jsapi->MakeSign( $hywx_test[‘mch_key‘]));
            return $parameters = $jsapi->GetValues();
        }
    }
}

 

2、支付请求方法

require_once dirname(dirname(__FILE__)) . ‘/librarys/wx/wx_pay/lib/WxPay.Api.php‘;
require_once dirname(dirname(__FILE__)) . ‘/librarys/wx/wx_pay/lib/WxPay.Config.php‘;
require_once dirname(dirname(__FILE__)) . ‘/librarys/wx/wx_pay/lib/WxPay.NativePay.php‘;

    public function actionTest()
    {
        $body = ‘欧式body‘;
        $order_code = ‘HY‘ . date(‘YmdHis‘);
        $total_fee = 0.01;
        $open_id = ‘or-9g0kq0_p4FxCLMAWPg4ZPYX0k‘;//用户open_id
        $notify_url = ‘/site/test-call-back‘;//回调地址
        $jsApiParameters = Payment::WxPayInit($body, $order_code, $total_fee, $open_id, $notify_url);
        $return_arr = [];
        $return_arr[‘trade_no‘] = $order_code;
        $return_arr[‘jsApiParameters‘] = $jsApiParameters;
        return $this->renderJson($return_arr);
    }

3、支付页面

php
/**
 * Created by PhpStorm.
 * User: LiDingLie
 * Date: 2017/10/18 0018
 * Time: 11:28
 */

use yii\helpers\Url;

$web = Url::base();
$this->title = ‘订单支付‘;
$url = \app\librarys\Tools::urlBase();
?>

class="zf_btn">确定支付


 

原文:https://www.cnblogs.com/lt-com/p/12912130.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐