新增创建订单发起支付
This commit is contained in:
+113
-4
@@ -2,15 +2,45 @@
|
||||
|
||||
namespace Extend\Wechat;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use App\Exception\BusinessException;
|
||||
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
|
||||
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
|
||||
use EasyWeChat\Pay\Application;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
|
||||
/**
|
||||
* 微信支付类
|
||||
*/
|
||||
class WechatPay
|
||||
{
|
||||
protected $app;
|
||||
protected array $config;// 系统配置
|
||||
|
||||
// 创建工厂类
|
||||
public function createApp(){
|
||||
/**
|
||||
* @param string $user_type
|
||||
*/
|
||||
public function __construct(string $user_type)
|
||||
{
|
||||
if ($user_type == 1){
|
||||
$this->config = config("we_chat.applets.patient");
|
||||
}elseif ($user_type == 2){
|
||||
$this->config = config("we_chat.applets.doctor");
|
||||
}elseif ($user_type == 3){
|
||||
$this->config = config("we_chat.applets.pharmacist");
|
||||
}
|
||||
|
||||
if (empty($this->config)){
|
||||
throw new BusinessException("系统配置错误", HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建工厂类
|
||||
* @return Application
|
||||
*/
|
||||
public function createApp(): Application
|
||||
{
|
||||
$config = [
|
||||
'mch_id' => config("we_chat.pay.mch_id"),
|
||||
|
||||
@@ -28,7 +58,7 @@ class WechatPay
|
||||
// 下载工具:https://github.com/wechatpay-apiv3/CertificateDownloader
|
||||
'platform_certs' => [
|
||||
// 请使用绝对路径
|
||||
__DIR__ . '/certs/wechatpay_112FCCD1B9ECC8292703AB7363C73D74B6AFDC1A.pem',
|
||||
__DIR__ . '/certs/' . config("we_chat.pay.mch_id") . '/wechatpay_112FCCD1B9ECC8292703AB7363C73D74B6AFDC1A.pem',
|
||||
],
|
||||
|
||||
/**
|
||||
@@ -41,5 +71,84 @@ class WechatPay
|
||||
// 'base_uri' => 'https://api.mch.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
|
||||
],
|
||||
];
|
||||
|
||||
try {
|
||||
return new Application($config);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw new BusinessException('实例化EasyWeChat类失败:' . $e->getMessage(), HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取jsapi预支付交易会话标识
|
||||
* @param string $out_trade_no 商户系统内部订单号
|
||||
* @param int $total 支付金额(实际金额x100)
|
||||
* @param string $openid
|
||||
* @return array
|
||||
*/
|
||||
public function getJsapiPrepayId(string $out_trade_no,int $total,string $openid): array
|
||||
{
|
||||
$app = $this->createApp();
|
||||
|
||||
$options = [
|
||||
"mchid" => config("we_chat.pay.mch_id"), // <---- 商户号
|
||||
"out_trade_no" => $out_trade_no, // 商户系统内部订单号
|
||||
"appid" => $this->config['app_id'],
|
||||
"description" => "问诊服务",
|
||||
"notify_url" => "https://dev.hospital.applets.igandanyiyuan.com/pay/wx/callback",
|
||||
"amount" => [
|
||||
"total" => $total,//订单总金额,单位为分。
|
||||
"currency" => "CNY"
|
||||
],
|
||||
"payer" => [
|
||||
"openid" => $openid // 下单用户的 openid
|
||||
]
|
||||
];
|
||||
|
||||
try {
|
||||
$response = $app->getClient()->postJson("v3/pay/transactions/jsapi", $options);
|
||||
dump($response->toArray(false));
|
||||
if ($response->isFailed()) {
|
||||
// 出错了,处理异常
|
||||
$result = $response->toArray(false);
|
||||
if(empty($result)){
|
||||
// 返回值为空
|
||||
throw new BusinessException("发起支付失败");
|
||||
}
|
||||
if (!empty($result['code'])){
|
||||
throw new BusinessException($result['message']);
|
||||
}
|
||||
throw new BusinessException("发起支付失败");
|
||||
}
|
||||
|
||||
return $response->toArray(false);
|
||||
} catch (\Exception $e) {
|
||||
throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取小程序支付配置
|
||||
* @param string $prepay_id 预支付交易会话标识
|
||||
* @return array
|
||||
*/
|
||||
public function getAppletsPayConfig(string $prepay_id): array
|
||||
{
|
||||
|
||||
try {
|
||||
$app = $this->createApp();
|
||||
$utils = $app->getUtils();
|
||||
|
||||
$appId = $this->config['app_id'];
|
||||
$signType = 'RSA'; // 默认RSA,v2要传MD5
|
||||
|
||||
$config = $utils->buildMiniAppConfig($prepay_id, $appId, $signType);
|
||||
if (empty($config)){
|
||||
throw new BusinessException("发起支付失败");
|
||||
}
|
||||
return $config;
|
||||
} catch (\Exception $e) {
|
||||
throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user