v2board/app/Http/Controllers/Guest/OrderController.php

99 lines
3.3 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Http\Controllers\Guest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Order;
2019-11-04 13:40:10 +08:00
use Omnipay\Omnipay;
2019-10-29 15:33:36 +08:00
use Illuminate\Support\Facades\Log;
2019-11-29 19:43:01 +08:00
use Illuminate\Support\Facades\Redis;
2019-10-29 15:33:36 +08:00
class OrderController extends Controller
{
2019-11-04 13:40:10 +08:00
public function alipayNotify (Request $request) {
Log::info('alipayNotifyData: ' . json_encode($_POST));
$gateway = Omnipay::create('Alipay_AopF2F');
$gateway->setSignType('RSA2'); //RSA/RSA2
$gateway->setAppId(config('v2board.alipay_appid'));
$gateway->setPrivateKey(config('v2board.alipay_privkey')); // 可以是路径,也可以是密钥内容
$gateway->setAlipayPublicKey(config('v2board.alipay_pubkey')); // 可以是路径,也可以是密钥内容
$request = $gateway->completePurchase();
$request->setParams($_POST); //Optional
try {
/** @var \Omnipay\Alipay\Responses\AopCompletePurchaseResponse $response */
$response = $request->send();
if($response->isPaid()){
$order = Order::where('trade_no', $_POST['out_trade_no'])->first();
if (!$order) {
2019-11-04 13:48:28 +08:00
abort(500, 'fail');
2019-11-04 13:40:10 +08:00
}
2019-11-28 14:05:16 +08:00
if ($order->status == 1) {
2019-11-04 13:48:28 +08:00
die('success');
2019-11-04 13:40:10 +08:00
}
$order->status = 1;
2019-11-04 13:47:12 +08:00
$order->callback_no = $_POST['trade_no'];
2019-11-04 13:40:10 +08:00
if (!$order->save()) {
2019-11-04 13:48:28 +08:00
abort(500, 'fail');
2019-11-04 13:40:10 +08:00
}
/**
* Payment is successful
*/
die('success'); //The response should be 'success' only
}else{
/**
* Payment is not successful
*/
die('fail');
}
} catch (Exception $e) {
/**
* Payment is not successful
*/
die('fail');
}
}
2019-10-29 15:33:36 +08:00
public function stripeNotify (Request $request) {
Log::info('stripeNotifyData: ' . json_encode($request->input()));
\Stripe\Stripe::setApiKey(config('v2board.stripe_sk_live'));
try {
$event = \Stripe\Webhook::constructEvent(
file_get_contents('php://input'),
$_SERVER['HTTP_STRIPE_SIGNATURE'],
config('v2board.stripe_webhook_key')
);
} catch (\Stripe\Error\SignatureVerification $e) {
abort(400);
}
$obj = $event->data->object;
2019-11-29 14:26:45 +08:00
if ($obj['status'] == 'chargeable') {
2019-11-29 19:43:01 +08:00
$trade_no = Redis::get($obj['id']);
if (!$trade_no) {
abort(500, 'redis is not found trade no by stripe source id.');
}
$order = Order::where('trade_no', $trade_no)->first();
2019-10-29 15:33:36 +08:00
if (!$order) {
2019-11-29 19:43:01 +08:00
abort(500, 'order is not found');
2019-10-29 15:33:36 +08:00
}
if ($order->status !== 0) {
2019-11-29 14:26:45 +08:00
die('order is paid');
2019-10-29 15:33:36 +08:00
}
$order->status = 1;
if (!$order->save()) {
2019-11-04 13:48:28 +08:00
abort(500, 'fail');
2019-10-29 15:33:36 +08:00
}
2019-11-29 19:43:01 +08:00
Redis::del($obj['id']);
2019-11-04 13:48:28 +08:00
die('success');
2019-10-29 15:33:36 +08:00
}
}
public function stripeReturn (Request $request) {
Log::info('stripeReturnData: ' . json_encode($request->input()));
header('Location:' . '/#/dashboard');
}
}