账户中心 -> 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) { //调用微信JSAPI下单获取预支付交易会话标识 try { $resp = $this->instance ->chain('/v3/fund-app/mch-transfer/transfer-bills') ->post(['json' => [ 'appid' => $this->appid, 'out_bill_no' => (string)$params['out_bill_no'], 'transfer_scene_id' => '1000', 'openid' => $params['openid'], 'transfer_amount' => 10, 'transfer_remark' => '余额提现', 'user_recv_perception' => '现金奖励', 'transfer_scene_report_infos' => [ [ 'info_type' => '活动名称', 'info_content' => '下单红包', ], [ 'info_type' => '奖励说明', 'info_content' => '预约下单得现金红包', ], ], 'notify_url' => env('APP_URL').'/api/wechat_transfer/notify' /*'time_expire' => $this->timestampToRfc3339(time() + env('ORDER_OUT_TIME')*60),*/ ]]); $result = json_decode($resp->getBody(),true); } catch (\Exception $e) { if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { $r = $e->getResponse(); // 记录错误信息 Log::error('wechat/transfer','微信商户转账失败,出现错误'.$r->getBody().'参数'.json_encode($params)); } return false; } return $result; } //查询订单 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); } }