You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.1 KiB
61 lines
2.1 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
use App\Common\OrderStatus as Status;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Order;
|
|
use App\Models\User;
|
|
use EasyWeChat\Factory;
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
|
|
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
|
|
/**
|
|
* 跳转支付
|
|
* Class SharePay
|
|
* @package App\Http\Controllers\Api
|
|
*/
|
|
class SharePayController extends Controller
|
|
{
|
|
public function pay()
|
|
{
|
|
$order_id = request()->input('id');
|
|
if (!$order_id) {
|
|
return $this->error('无效的ID');
|
|
}
|
|
//用户openid
|
|
$openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG
|
|
|
|
$order = Order::query()->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])->find($order_id);
|
|
|
|
$config = config('wechat.payment.default');
|
|
$config = array_merge($config, [
|
|
'app_id' => 'wxb35ef055a4dd8ad4',
|
|
'mch_id' => 'c782df1e6d3a03bb4a12e9d41c89cf9c',
|
|
'key' => 'lfyyhyz8888888888888888888888888',
|
|
]);
|
|
|
|
$app = Factory::payment($config);
|
|
try {
|
|
$result = $app->order->unify([
|
|
'body' => $order->title,
|
|
'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
|
|
'total_fee' => 1, //TODO 测试暂时注释 round($price * 100), //支付金额单位为分
|
|
'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
|
'trade_type' => 'JSAPI',
|
|
'openid' => $openid,
|
|
// 'profit_sharing' => 'Y', //Y分账,N不分账,默认不分账,Y大写
|
|
]);
|
|
} catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) {
|
|
return ['error' => $e->getMessage(), 'line' => $e->getLine()];
|
|
}
|
|
|
|
if (empty($result['prepay_id'])) {
|
|
return $result;
|
|
}
|
|
|
|
$jssdk = $app->jssdk;
|
|
$payConfig = $jssdk->bridgeConfig($result['prepay_id'], false) + ['id' => $order->id, 'order_no' => $order->order_no]; // 返回数组
|
|
return $this->success($payConfig);
|
|
}
|
|
}
|