Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

集成-StripeCheckout~~~ #394

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions app/Http/Controllers/Pay/StripeCheckoutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace App\Http\Controllers\Pay;

use AmrShawky\LaravelCurrency\Facade\Currency;
use App\Exceptions\RuleValidationException;
use App\Http\Controllers\PayController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Stripe\Checkout\Session;
use Stripe\Exception\UnexpectedValueException;
use Stripe\Exception\SignatureVerificationException;
use Stripe\Webhook;

class StripeCheckoutController extends PayController
{
public function gateway(string $payway, string $orderSN)
{
try{
$this->loadGateWay($orderSN, $payway);
\Stripe\Stripe::setApiKey($this->payGateway->merchant_id);
$price = Currency::convert()
->from('CNY')
->to('HKD')
->amount($this->order->actual_price)
->round(2)
->get();
$TotalAmount = $price * 100;
$data = [
'success_url' => url('detail-order-sn', ['orderSN' => $this->order->order_sn]),
'cancel_url' => url('/'),
'client_reference_id' => $this->order->order_sn,
'line_items' => [[
'price_data' => [
'currency' => 'HKD',
'product_data' => [
'name' => $this->order->order_sn
],
'unit_amount' => $TotalAmount
],
'quantity' => 1
]],
'mode' => 'payment',
'customer_email' => $this->order->email
];
$session = Session::create($data);
return redirect()->away($session->url);//可以使用自定义域名
}catch (\Exception $e) {
return $this->err(__('dujiaoka.prompt.abnormal_payment_channel') . $e->getMessage());
}
}
//webhook地址:https://shop.liner77.xyz/pay/stripecheckout/webhook 自行替换域名
//侦听的事件: 'checkout.session.completed' 'checkout.session.async_payment_succeeded'
public function webhook(Request $request)
{
$payload = file_get_contents('php://input');
$data = json_decode($payload, true);
if(!$this->orderService->detailOrderSN($data['data']['object']['client_reference_id'])){
return 'order error';
}
$order = $this->orderService->detailOrderSN($data['data']['object']['client_reference_id']);
if(!$this->payService->detail($order->pay_id)){
return 'order error';
}
$payGateway = $this->payService->detail($order->pay_id);
$endpoint_secret = $payGateway->merchant_pem;
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
try{
$event = Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
}catch(UnexpectedValueException $e) {
http_response_code(400);
exit();
}catch(SignatureVerificationException $e) {
http_response_code(400);
exit();
}
switch($event->type){
case 'checkout.session.completed':
$session = $event->data->object;
if ($session->payment_status == 'paid') {
$this->orderProcessService->completedOrder($session->client_reference_id,$order->actual_price,$session->payment_intent);
}
break;
case 'checkout.session.async_payment_succeeded':
$session = $event->data->object;
$this->orderProcessService->completedOrder($session->client_reference_id,$order->actual_price,$session->payment_intent);
break;
}
http_response_code(200);
exit();
}
}
2 changes: 1 addition & 1 deletion database/sql/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ INSERT INTO `pays` VALUES (20, 'V 免签微信', 'vwx', 1, 1, 'V 免签通讯密
INSERT INTO `pays` VALUES (21, 'Stripe[微信支付宝]', 'stripe', 1, 1, 'pk开头的可发布密钥', NULL, 'sk开头的密钥', 'pay/stripe', 1, '2020-10-29 13:15:56', '2020-10-29 13:18:29', NULL);
INSERT INTO `pays` VALUES (22, 'Coinbase[加密货币]', 'coinbase', 1, 3, '费率', 'API密钥', '共享密钥', 'pay/coinbase', 0, '2021-08-15 13:15:56', '2021-10-12 13:15:56', NULL);
INSERT INTO `pays` VALUES (23, 'Epusdt[trc20]', 'epusdt', 1, 3, 'API密钥', '不填即可', 'api请求地址', 'pay/epusdt', 0, '2022-03-22 13:15:56', '2022-03-22 13:15:56', NULL);

INSERT INTO `pays` VALUES (24, 'stripeCheckout', 'stripeCheckout', '1', '3', 'sk_live_xxxxxxxxxxxxxxxxxxx', '//自行替换k_live_xx和 whsec_xxx开头的密钥\r\n//webhook地址:https://www.baidu.com/pay/stripecheckout/webhook 自行替换域名 \r\n//侦听的事件: \'checkout.session.completed\' \'checkout.session.async_payment_succeeded\'\r\n//默认cny转hkd', 'whsec_xxxxxxxxxxxxxxxxxxx', 'pay/stripecheckout', '0', '2022-04-26 19:27:31', '2022-09-29 21:21:55', NULL);
-- ----------------------------
COMMIT;

Expand Down
3 changes: 3 additions & 0 deletions routes/common/pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@
Route::get('epusdt/{payway}/{orderSN}', 'EpusdtController@gateway');
Route::post('epusdt/notify_url', 'EpusdtController@notifyUrl');
Route::get('epusdt/return_url', 'EpusdtController@returnUrl')->name('epusdt-return');
// StripeCheckout
Route::get('stripecheckout/{payway}/{orderSN}','StripeCheckoutController@gateway');
Route::post('stripecheckout/webhook', 'StripeCheckoutController@webhook');

});