123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace App\Http\Controllers\Guest;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use App\Models\Order;
- use Omnipay\Omnipay;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Cache;
- use Library\BitpayX;
- class OrderController extends Controller
- {
- 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()) {
- /**
- * Payment is successful
- */
- if (!$this->handle($_POST['out_trade_no'], $_POST['trade_no'])) {
- abort(500, 'fail');
- }
- die('success'); //The response should be 'success' only
- } else {
- /**
- * Payment is not successful
- */
- die('fail');
- }
- } catch (Exception $e) {
- /**
- * Payment is not successful
- */
- die('fail');
- }
- }
- 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);
- }
- switch ($event->type) {
- case 'source.chargeable':
- $source = $event->data->object;
- $charge = \Stripe\Charge::create([
- 'amount' => $source['amount'],
- 'currency' => $source['currency'],
- 'source' => $source['id'],
- ]);
- if ($charge['status'] == 'succeeded') {
- $trade_no = Cache::get($source['id']);
- if (!$trade_no) {
- abort(500, 'redis is not found trade no by stripe source id');
- }
- if (!$this->handle($trade_no, $source['id'])) {
- abort(500, 'fail');
- }
- Cache::forget($source['id']);
- die('success');
- }
- break;
- default:
- abort(500, 'event is not support');
- }
- }
- public function bitpayXNotify(Request $request)
- {
- $inputString = file_get_contents('php://input', 'r');
- Log::info('bitpayXNotifyData: ' . $inputString);
- $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 = [
- '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']
- ];
- $strToSign = $bitpayX->prepareSignId($inputJSON['merchant_order_id']);
- if (!$bitpayX->verify($strToSign, $inputJSON['token'])) {
- die([
- 'status' => 400,
- 'error' => 'sign error'
- ]);
- }
- if ($params['status'] !== 'PAID') {
- die([
- 'status' => 400,
- 'error' => 'order is not paid'
- ]);
- }
- if (!$this->handle($params['merchant_order_id'], $params['order_id'])) {
- die([
- 'status' => 400,
- 'error' => 'order process fail'
- ]);
- }
- die([
- 'status' => 200
- ]);
- }
- private function handle($tradeNo, $callbackNo)
- {
- $order = Order::where('trade_no', $tradeNo)->first();
- if (!$order) {
- abort(500, 'order is not found');
- }
- if ($order->status !== 0) {
- abort(500, 'order is paid');
- }
- $order->status = 1;
- $order->callback_no = $callbackNo;
- return $order->save();
- }
- }
|