2019-10-29 15:33:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Guest;
|
|
|
|
|
2020-06-04 18:00:33 +08:00
|
|
|
use App\Services\OrderService;
|
2020-07-31 15:22:27 +08:00
|
|
|
use App\Services\TelegramService;
|
2019-10-29 15:33:36 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Order;
|
2020-07-20 19:56:39 +08:00
|
|
|
use Library\Epay;
|
2019-11-04 13:40:10 +08:00
|
|
|
use Omnipay\Omnipay;
|
2019-10-29 15:33:36 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-01-12 01:27:36 +08:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2019-12-27 14:25:25 +08:00
|
|
|
use Library\BitpayX;
|
2020-07-20 19:56:39 +08:00
|
|
|
use Library\MGate;
|
2019-10-29 15:33:36 +08:00
|
|
|
|
|
|
|
class OrderController extends Controller
|
|
|
|
{
|
2020-01-11 13:36:52 +08:00
|
|
|
public function alipayNotify(Request $request)
|
|
|
|
{
|
2020-03-07 11:37:39 +08:00
|
|
|
// Log::info('alipayNotifyData: ' . json_encode($_POST));
|
2019-11-04 13:40:10 +08:00
|
|
|
$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();
|
2020-01-11 13:36:52 +08:00
|
|
|
|
|
|
|
if ($response->isPaid()) {
|
2019-11-04 13:40:10 +08:00
|
|
|
/**
|
|
|
|
* Payment is successful
|
|
|
|
*/
|
2019-12-27 14:25:25 +08:00
|
|
|
if (!$this->handle($_POST['out_trade_no'], $_POST['trade_no'])) {
|
|
|
|
abort(500, 'fail');
|
|
|
|
}
|
|
|
|
|
2019-11-04 13:40:10 +08:00
|
|
|
die('success'); //The response should be 'success' only
|
2020-01-11 13:36:52 +08:00
|
|
|
} else {
|
2019-11-04 13:40:10 +08:00
|
|
|
/**
|
|
|
|
* Payment is not successful
|
|
|
|
*/
|
|
|
|
die('fail');
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
/**
|
|
|
|
* Payment is not successful
|
|
|
|
*/
|
|
|
|
die('fail');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function stripeNotify(Request $request)
|
|
|
|
{
|
2020-03-07 11:37:39 +08:00
|
|
|
// Log::info('stripeNotifyData: ' . json_encode($request->input()));
|
2019-10-29 15:33:36 +08:00
|
|
|
|
|
|
|
\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);
|
|
|
|
}
|
2019-11-30 12:31:11 +08:00
|
|
|
switch ($event->type) {
|
|
|
|
case 'source.chargeable':
|
2020-06-04 18:33:44 +08:00
|
|
|
$object = $event->data->object;
|
2020-06-04 18:00:33 +08:00
|
|
|
\Stripe\Charge::create([
|
2020-06-04 18:33:44 +08:00
|
|
|
'amount' => $object->amount,
|
|
|
|
'currency' => $object->currency,
|
|
|
|
'source' => $object->id,
|
|
|
|
'metadata' => json_decode($object->metadata, true)
|
2019-11-30 12:31:11 +08:00
|
|
|
]);
|
2020-06-04 18:00:33 +08:00
|
|
|
die('success');
|
|
|
|
break;
|
2020-06-04 18:12:53 +08:00
|
|
|
case 'charge.succeeded':
|
2020-06-04 18:33:44 +08:00
|
|
|
$object = $event->data->object;
|
|
|
|
if ($object->status === 'succeeded') {
|
2020-06-04 18:39:54 +08:00
|
|
|
$metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
|
2020-06-04 18:29:41 +08:00
|
|
|
$tradeNo = $metaData->out_trade_no;
|
|
|
|
if (!$tradeNo) {
|
|
|
|
abort(500, 'trade no is not found in metadata');
|
2019-11-30 12:31:11 +08:00
|
|
|
}
|
2020-06-04 18:33:44 +08:00
|
|
|
if (!$this->handle($tradeNo, $object->balance_transaction)) {
|
2019-11-30 12:31:11 +08:00
|
|
|
abort(500, 'fail');
|
|
|
|
}
|
|
|
|
die('success');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort(500, 'event is not support');
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-27 14:25:25 +08:00
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function bitpayXNotify(Request $request)
|
|
|
|
{
|
2019-12-27 14:25:25 +08:00
|
|
|
$inputString = file_get_contents('php://input', 'r');
|
2020-03-07 11:37:39 +08:00
|
|
|
// Log::info('bitpayXNotifyData: ' . $inputString);
|
2019-12-27 14:25:25 +08:00
|
|
|
$inputStripped = str_replace(array("\r", "\n", "\t", "\v"), '', $inputString);
|
|
|
|
$inputJSON = json_decode($inputStripped, true); //convert JSON into array
|
|
|
|
|
|
|
|
$bitpayX = new BitpayX(config('v2board.bitpayx_appsecret'));
|
|
|
|
$params = [
|
2020-01-11 13:36:52 +08:00
|
|
|
'status' => $inputJSON['status'],
|
|
|
|
'order_id' => $inputJSON['order_id'],
|
|
|
|
'merchant_order_id' => $inputJSON['merchant_order_id'],
|
|
|
|
'price_amount' => $inputJSON['price_amount'],
|
|
|
|
'price_currency' => $inputJSON['price_currency'],
|
|
|
|
'pay_amount' => $inputJSON['pay_amount'],
|
|
|
|
'pay_currency' => $inputJSON['pay_currency'],
|
|
|
|
'created_at_t' => $inputJSON['created_at_t']
|
2019-12-27 14:25:25 +08:00
|
|
|
];
|
|
|
|
$strToSign = $bitpayX->prepareSignId($inputJSON['merchant_order_id']);
|
|
|
|
if (!$bitpayX->verify($strToSign, $inputJSON['token'])) {
|
2020-01-20 23:34:53 +08:00
|
|
|
abort(500, 'sign error');
|
2019-12-27 14:25:25 +08:00
|
|
|
}
|
|
|
|
if ($params['status'] !== 'PAID') {
|
2020-01-20 23:34:53 +08:00
|
|
|
abort(500, 'order is not paid');
|
2019-12-27 14:25:25 +08:00
|
|
|
}
|
|
|
|
if (!$this->handle($params['merchant_order_id'], $params['order_id'])) {
|
2020-01-20 23:34:53 +08:00
|
|
|
abort(500, 'order process fail');
|
2019-12-27 14:25:25 +08:00
|
|
|
}
|
2020-03-05 16:28:20 +08:00
|
|
|
die(json_encode([
|
|
|
|
'status' => 200
|
|
|
|
]));
|
2019-12-27 14:25:25 +08:00
|
|
|
}
|
|
|
|
|
2020-07-20 19:56:39 +08:00
|
|
|
public function mgateNotify(Request $request)
|
2020-01-14 19:45:19 +08:00
|
|
|
{
|
2020-07-20 19:56:39 +08:00
|
|
|
$mgate = new MGate(config('v2board.mgate_url'), config('v2board.mgate_app_id'), config('v2board.mgate_app_secret'));
|
|
|
|
if (!$mgate->verify($request->input())) {
|
|
|
|
abort(500, 'fail');
|
|
|
|
}
|
|
|
|
if (!$this->handle($request->input('out_trade_no'), $request->input('trade_no'))) {
|
|
|
|
abort(500, 'fail');
|
|
|
|
}
|
|
|
|
die('success');
|
|
|
|
}
|
2020-01-14 19:45:19 +08:00
|
|
|
|
2020-07-20 19:56:39 +08:00
|
|
|
public function epayNotify(Request $request)
|
|
|
|
{
|
|
|
|
$epay = new Epay(config('v2board.epay_url'), config('v2board.epay_pid'), config('v2board.epay_key'));
|
|
|
|
if (!$epay->verify($request->input())) {
|
2020-01-18 18:41:02 +08:00
|
|
|
abort(500, 'fail');
|
2020-01-14 19:45:19 +08:00
|
|
|
}
|
|
|
|
if (!$this->handle($request->input('out_trade_no'), $request->input('trade_no'))) {
|
2020-01-18 18:41:02 +08:00
|
|
|
abort(500, 'fail');
|
2020-01-14 19:45:19 +08:00
|
|
|
}
|
|
|
|
die('success');
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
private function handle($tradeNo, $callbackNo)
|
|
|
|
{
|
2019-12-27 14:25:25 +08:00
|
|
|
$order = Order::where('trade_no', $tradeNo)->first();
|
|
|
|
if (!$order) {
|
|
|
|
abort(500, 'order is not found');
|
|
|
|
}
|
2020-06-04 18:00:33 +08:00
|
|
|
$orderService = new OrderService($order);
|
2020-07-31 15:22:27 +08:00
|
|
|
if (!$orderService->success($callbackNo)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$telegramService = new TelegramService();
|
|
|
|
$message = sprintf(
|
|
|
|
"💰成功收款%s元\n———————————————\n订单号:%s",
|
|
|
|
$order->total_amount / 100,
|
|
|
|
$order->trade_no
|
|
|
|
);
|
|
|
|
$telegramService->sendMessageWithAdmin($message);
|
|
|
|
return true;
|
2019-12-27 14:25:25 +08:00
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|