账户中心 -> API安全 直接查询到 $platformCertificateSerialOrPublicKeyId = 'PUB_KEY_ID_0116121113552024111800218500000334'; // 构造一个 APIv3 客户端实例 $instance = Builder::factory([ 'mchid' => Config('wechatpay.mchid'), 'serial' => $merchantCertificateSerial, 'privateKey' => $merchantPrivateKeyInstance, 'certs' => [ $platformCertificateSerialOrPublicKeyId => $platformPublicKeyInstance, ], ]); $this->instance = $instance; $this->appid = Config('wechatpay.appid'); $this->mchid = Config('wechatpay.mchid'); $this->merchantPrivateKeyInstance = $merchantPrivateKeyInstance; $this->merchantCertificateSerial = Config('wechat.certificate'); return; } /** * 拉起小程序支付 */ public function pay($params) { $notify_url = Config('wechatpay.notify_url'); //调用微信JSAPI下单获取预支付交易会话标识 try { $resp = $this->instance ->chain('v3/pay/transactions/jsapi') ->post(['json' => [ 'mchid' => $this->mchid, 'out_trade_no' => (string)$params['out_trade_no'], 'appid' => $this->appid, 'description' => $params['description'], /*'time_expire' => $this->timestampToRfc3339(time() + env('ORDER_OUT_TIME')*60),*/ 'notify_url' => env('APP_URL').'/api/wechat_pay/notify', 'amount' => [ 'total' => 1, //'total' => $params['total_price']*100, 'currency' => 'CNY' ], 'payer' => [ 'openid' => $params['openid'] ] ]]); $result = json_decode($resp->getBody(),true); $prepay_id = $result['prepay_id']; } catch (\Exception $e) { if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { $r = $e->getResponse(); // 记录错误信息 Log::error('wechat/Payment','拉起小程序支付,出现错误'.$r->getBody().'参数'.json_encode($params)); } return response()->json(['error' => '拉起支付失败'], 500); } //根据返回的预支付交易会话标识,组装调起小程序支付参数 $data = [ 'appId' => $this->appid, 'timeStamp' => (string)Formatter::timestamp(), 'nonceStr' => Formatter::nonce(), 'package' => 'prepay_id='.$prepay_id, ]; $data += ['paySign' => Rsa::sign( Formatter::joinedByLineFeed(...array_values($data)), $this->merchantPrivateKeyInstance ), 'signType' => 'RSA']; return json_send(['code'=>'success','msg'=>'成功','data'=>$data]); } //查询订单 public function query($params) { try { $promise = $this->instance ->chain('v3/pay/transactions/id/'.$params['']) ->get(['json' => [ 'mchid' => $this->mchid, ]]); } catch (\Exception $e) { if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { $r = $e->getResponse(); // 记录错误信息 Log::error('wechat/Payment','查询订单,出现错误'.$r->getBody().'参数'.json_encode($params)); } return response()->json(['error' => '查询订单支付失败'], 500); } return response()->json($promise); } /** * 订单微信退款 */ public function refund($params) { $notify_url = Config('wechat.course_refund_notify_url'); try { $resp = $this->instance ->chain('v3/refund/domestic/refunds') ->post(['json' => [ 'transaction_id' => $params['transaction_id'], 'out_refund_no' => $params['out_refund_no'], 'reason' => $params['reason'] ?? '', 'notify_url' => $notify_url, 'amount' => [ 'refund' => $params['refund'], 'total' => $params['total'], 'currency' => 'CNY' ], ]]); } catch (\Exception $e) { if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { $r = $e->getResponse(); Log::error('wechat/Payment','订单微信退款,出现错误'.$r->getBody()); } return response()->json(['error' => '订单微信退款失败'], 500); } return response()->json($resp); } //Rfc3339时间日期格式 public function timestampToRfc3339($timestamp) { // 转换为DateTime对象 $dateTime = new DateTime(); $dateTime->setTimestamp($timestamp); // 设置时区,默认为UTC $dateTime->setTimezone(new DateTimeZone('UTC')); // 格式化为RFC3339 return $dateTime->format(DateTime::RFC3339); } }