最近在开发一个项目包含web端、H5端和App端,在开发支付功能时每个端的情况略有不同在这里做一下记录,支付宝支付使用Yangsongda,微信支付使用EasyWeChat,如有不对之处欢迎指正。
安装Yansongda
composer require yansongda/pay -vvv
安装EasyWeChat
composer require w7corp/easywechat
下面上代码,可以直接使用
use EasyWeChat\Factory;
use Yansongda\LaravelPay\Facades\Pay;
class PayController extends BaseController
{
public function buyVip(Request $request)
{
$user_id = $this->getUserId();
$type = $request->input('type', '');
$id = (int)$request->input('id', 0);
$device_type = $this->deviceType;
if (!$type) {
return $this->error('支付类型不能为空');
}
if (!$id) {
return $this->error('商品ID不能为空');
}
$vip = Vip::where('id', $id)->first();
if (!$vip) {
return $this->error('充值金额不存在');
}
if ($type == 'wxpay') {
$order = OrderService::createOrder($user_id, $vip, 'alipay');
$config = config('wechat.payment.default');
$app = Factory::payment($config);
if ($device_type == 'app') {
// APP支付
$result = $app->order->unify([
'body' => $order['subject'],
'out_trade_no' => $order['order_sn'],
'total_fee' => $order['amount'] * 100,
//'notify_url' => 'https://pay.weixin.qq.com/wxpay/pay.action', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'APP', // 请对应换成你的支付方式对应的值类型
]);
} else {