使用easywechat调用微信支付

base.php

 public $app;
    public $payapp;
    public function initialize()
    {
        //小程序配置
        $config=array(
            'app_id' => Config('app.WeiXinAPPID'),
            'secret' => Config('app.WeiXinAPPSECRET'),
            // 下面为可选项
            // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
        );
        $this->app= Factory::miniProgram($config);
        //验证tocken
        //$this->CheckAppTocken();

        $payconfig = [
            // 必要配置
            'app_id'             =>Config('app.WeiXinAPPID'),
            'mch_id'             => Config('app.WeiXinMCHID'),
            'key'                =>Config('app.WeiXinKEY'),   // API 密钥

            // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
            'cert_path'          => Config('app.Domain').'/extend/cert/apiclient_cert.pem', // XXX: 绝对路径!!!!
            'key_path'           => Config('app.Domain').'/extend/cert/apiclient_key.pem',      // XXX: 绝对路径!!!!
            'notify_url'         =>'',     // 你也可以在下单时单独设置来想覆盖它
        ];
        $this->payapp = Factory::payment($payconfig);

    }

调微信支付参数:


                //easywechat调起微信支付
                $result = $this->payapp->order->unify([
                    'body' => '在线支付',
                    'out_trade_no' =>$order_frontnumber ,
                    'total_fee' =>(float)($ordermoney*100),
                    'spbill_create_ip' => '', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
                    'notify_url' => Config('app.Domain').'/api/pay/wxsuccess1.html', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                    'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
                    'openid' => $openid,
                ]);
                $jssdk = $this->payapp->jssdk;
                $config = $jssdk->bridgeConfig($result['prepay_id'],false); // 返回数组

                $back['payparam']=$config;               
                $back['totalorderid']=$totalorderid;

微信支付回调

namespace app\api\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\Config;

use EasyWeChat\Factory;
use EasyWeChat\Payment\Kernel\BaseClient;
class Pay extends BaseController
{
    public $payapp='';
    public function initialize()
    {
        $payconfig = [
            // 必要配置
            'app_id'             =>Config('app.WeiXinAPPID'),
            'mch_id'             => Config('app.WeiXinMCHID'),
            'key'                =>Config('app.WeiXinKEY'),   // API 密钥

            // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
            'cert_path'          => Config('app.Domain').'/extend/cert/apiclient_cert.pem', // XXX: 绝对路径!!!!
            'key_path'           => Config('app.Domain').'/extend/cert/apiclient_key.pem',      // XXX: 绝对路径!!!!
            'notify_url'         =>'',     // 你也可以在下单时单独设置来想覆盖它
        ];
        $this->payapp = Factory::payment($payconfig);

    }
    public function wxsuccess1() {
        BaseClient::setDefaultOptions([
            'curl' => [
                CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
            ],
            'verify'=>false               //不开启CURLOPT_SSL_VERIFYPEER, 这里后来线上ssl报错加的,原因忘了
        ]);
        $response = $this->payapp->handlePaidNotify(function ($message, $fail) {
            file_put_contents('./wx100.txt',var_export($message,true));
             
            $orderCode = $message['out_trade_no'];
            $orderinfo=DB::name('orderfront')->where(['order_frontnumber'=>$orderCode])->find();
            // echo '<pre>';
            // print_r($orderinfo);
            
            //如果订单不存在 或者 订单已经支付过了告诉微信,我已经处理完了,订单没找到,别再通知我了
            if(!$orderinfo ||$orderinfo['order_frontpaystate']==1){ return true; }
 
            DB::startTrans();
            if ($message['return_code'] === 'SUCCESS' && $message['result_code']=== 'SUCCESS') {

                PayNotify1($message);
                DB::commit();
                return true;
            } else {
                DB::rollBack();
                return true;
            }

        });
        return $response;
    }

common.php

function PayNotify1($back)
{
    //file_put_contents('./200.txt',var_export($back,true));
    $where = array();
    $where['order_frontnumber'] = $back['out_trade_no'];
    $info = DB::name('orderfront')->where($where)->find();
    $total_fee=round($back['total_fee']/100,2);
    
    $time=time();
    if(empty($info['order_frontnumber'])){ return false;}

    DB::name('orderfront')->where(['order_frontnumber'=>$info['order_frontnumber']])->update(['order_frontpaystate'=>1,'order_frontpaytime'=>$time]);

    //更新订单状态--已预约
    DB::name('order')->where(['order_totalordernumber'=>$info['order_totalordernumber']])->update(['order_state'=>1]);

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
移动端H5调用微信支付宝支付是一种简单方便的支付方式。用户在网页中选择微信或支付宝支付后,会被引导到微信或支付宝的支付页面,输入支付密码或确认支付即可完成支付。对于商家来说,只需在网页中添加相应的支付接口和配置相关信息,就可以实现H5调用微信支付宝支付功能。这种支付方式适用于各类移动端应用,如电商平台、小程序、线下商户等。 在实现H5调用微信支付宝支付的过程中,需要注意保障支付安全和用户隐私。商家需要通过微信支付宝的官方平台获得相关权限和密钥,确保支付接口的可信性和安全性。同时,还需要遵守相关的法律法规和隐私政策,保护用户的个人信息和支付数据。另外,商家还可以通过设置不同的支付方式、优惠券等方式来吸引用户,提高支付转化率和用户满意度。 在移动端H5调用微信支付宝支付的过程中,还需要考虑支付的实时性和用户体验。商家需要保证支付的及时性和可靠性,避免因网络延迟或其他原因导致支付失败或出现异常情况。同时,还需要考虑用户的支付习惯和支付环境,提供简洁明了的支付流程和友好的界面设计,提高用户支付的便捷性和舒适度。通过合理规划和技术实现,移动端H5调用微信支付宝支付可以成为一种便捷、安全、高效的支付方式,为用户和商家带来更好的支付体验和商业价值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值