Wechat.php 1.9 KB
<?php

namespace app\server;
use EasyWeChat\Factory;
use think\Db;

/**
 * easyWechat 插件
 使用
 $app = app\server\Wechat::getInstance()->initWechat();
 $app 为 easyWechat的对象 根据官网接口调用或二开
 */
class Wechat {
	static public $app = null;
	static public $appid= '';
	static public $appsecret= '';
	
	static private $inc = null;
	
	function __construct(){
		
	}
	
	//静态函数
	static public function getInstance($store_id){
		if(self::$inc == null){
			$storeInfo = Db::name('verification_store')->where(['id'=>$store_id])->find();
			self::$appid = $storeInfo['appId'];
			self::$appsecret = $storeInfo['appSecret'];
			self::$inc = new self();
		}
		return self::$inc;
	}

	
	//初始化 easywechat app
    static public function initWechat(){
		$appid= self::$appid;
		$appsecret= self::$appsecret;
		//easyWechat配置
		$config = [
			'app_id' => $appid,
			'secret' => $appsecret,
			// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
			'response_type' => 'array',
		];
		self::$app = Factory::officialAccount($config);
		return self::$app;
	}
	
	//初始化 jssdk支付
	static public function initWechatPay(){
		//测试公众号
		$appid= self::$appid;
		
		$pay_key = Db::name('config')->where("id=21")->find();
		$pay_mch_id = Db::name('config')->where("id=22")->find();
		
		$notify_url = 'https://coupon.xp.yn.cn'.url('/api/v1/wechat/notify');
		
		$config = [
			'app_id'             => $appid,
			'mch_id'             => $pay_mch_id['value'],
			'key'                => $pay_key['value'],
			'cert_path'          => '../extend/cert/apiclient_cert.pem', // XXX: 绝对路径!!!!
			'key_path'           => '../extend/cert/apiclient_key.pem',      // XXX: 绝对路径!!!!
			'notify_url'         => $notify_url,     // 你也可以在下单时单独设置来想覆盖它
			
		];
		
		$app = Factory::payment($config);
		return $app;
	} 
}