mirror of
https://github.com/v2board/v2board.git
synced 2024-11-14 07:29:13 +08:00
stripe test
This commit is contained in:
parent
ca04634537
commit
136cffcf13
@ -2,7 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Utils\Helper;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
use Stripe\Source;
|
||||||
|
use Stripe\Stripe;
|
||||||
|
use Stripe\StripeClient;
|
||||||
|
use Omnipay\Omnipay;
|
||||||
|
|
||||||
class Test extends Command
|
class Test extends Command
|
||||||
{
|
{
|
||||||
@ -37,5 +42,21 @@ class Test extends Command
|
|||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
|
$gateway = Omnipay::create('Stripe');
|
||||||
|
$gateway->setApiKey('sk_test_gDIIPtUgWZHbnTR7CUWZS8k500NsLS9SYB');
|
||||||
|
|
||||||
|
$formData = array('number' => '4242424242424242', 'expiryMonth' => '6', 'expiryYear' => '2030', 'cvv' => '333');
|
||||||
|
$response = $gateway->purchase(array('amount' => '10.00', 'currency' => 'USD', 'card' => $formData))->send();
|
||||||
|
|
||||||
|
if ($response->isRedirect()) {
|
||||||
|
// redirect to offsite payment gateway
|
||||||
|
$response->redirect();
|
||||||
|
} elseif ($response->isSuccessful()) {
|
||||||
|
// payment was successful: update database
|
||||||
|
print_r($response);
|
||||||
|
} else {
|
||||||
|
// payment failed: display message to customer
|
||||||
|
echo $response->getMessage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,6 +76,7 @@ class ConfigController extends Controller
|
|||||||
// stripe
|
// stripe
|
||||||
'stripe_alipay_enable' => (int)config('v2board.stripe_alipay_enable', 0),
|
'stripe_alipay_enable' => (int)config('v2board.stripe_alipay_enable', 0),
|
||||||
'stripe_wepay_enable' => (int)config('v2board.stripe_wepay_enable', 0),
|
'stripe_wepay_enable' => (int)config('v2board.stripe_wepay_enable', 0),
|
||||||
|
'stripe_card_enable' => (int)config('v2board.stripe_card_enable', 0),
|
||||||
'stripe_sk_live' => config('v2board.stripe_sk_live'),
|
'stripe_sk_live' => config('v2board.stripe_sk_live'),
|
||||||
'stripe_pk_live' => config('v2board.stripe_pk_live'),
|
'stripe_pk_live' => config('v2board.stripe_pk_live'),
|
||||||
'stripe_webhook_key' => config('v2board.stripe_webhook_key'),
|
'stripe_webhook_key' => config('v2board.stripe_webhook_key'),
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Guest;
|
namespace App\Http\Controllers\Guest;
|
||||||
|
|
||||||
|
use App\Services\OrderService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\Order;
|
use App\Models\Order;
|
||||||
@ -67,21 +68,25 @@ class OrderController extends Controller
|
|||||||
switch ($event->type) {
|
switch ($event->type) {
|
||||||
case 'source.chargeable':
|
case 'source.chargeable':
|
||||||
$source = $event->data->object;
|
$source = $event->data->object;
|
||||||
$charge = \Stripe\Charge::create([
|
\Stripe\Charge::create([
|
||||||
'amount' => $source['amount'],
|
'amount' => $source['amount'],
|
||||||
'currency' => $source['currency'],
|
'currency' => $source['currency'],
|
||||||
'source' => $source['id'],
|
'source' => $source['id'],
|
||||||
'description' => config('v2board.app_name', 'V2Board') . $source['metadata']['invoice_id'],
|
'metadata' => $source->metadata
|
||||||
]);
|
]);
|
||||||
if ($charge['status'] == 'succeeded') {
|
die('success');
|
||||||
$trade_no = Cache::get($source['id']);
|
break;
|
||||||
|
case 'source.succeeded':
|
||||||
|
$source = $event->data->object;
|
||||||
|
if ($source->status === 'succeeded') {
|
||||||
|
$metaData = $source->metadata;
|
||||||
|
$trade_no = $metaData->out_trade_no;
|
||||||
if (!$trade_no) {
|
if (!$trade_no) {
|
||||||
abort(500, 'redis is not found trade no by stripe source id');
|
abort(500, 'redis is not found trade no by stripe source id');
|
||||||
}
|
}
|
||||||
if (!$this->handle($trade_no, $source['id'])) {
|
if (!$this->handle($trade_no, $source->balance_transaction)) {
|
||||||
abort(500, 'fail');
|
abort(500, 'fail');
|
||||||
}
|
}
|
||||||
Cache::forget($source['id']);
|
|
||||||
die('success');
|
die('success');
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -143,11 +148,7 @@ class OrderController extends Controller
|
|||||||
if (!$order) {
|
if (!$order) {
|
||||||
abort(500, 'order is not found');
|
abort(500, 'order is not found');
|
||||||
}
|
}
|
||||||
if ($order->status !== 0) {
|
$orderService = new OrderService($order);
|
||||||
return true;
|
return $orderService->success($callbackNo);
|
||||||
}
|
|
||||||
$order->status = 1;
|
|
||||||
$order->callback_no = $callbackNo;
|
|
||||||
return $order->save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,12 +9,10 @@ use App\Services\OrderService;
|
|||||||
use App\Services\UserService;
|
use App\Services\UserService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use App\Models\Order;
|
use App\Models\Order;
|
||||||
use App\Models\Plan;
|
use App\Models\Plan;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Models\Coupon;
|
|
||||||
use App\Utils\Helper;
|
use App\Utils\Helper;
|
||||||
use Omnipay\Omnipay;
|
use Omnipay\Omnipay;
|
||||||
use Stripe\Stripe;
|
use Stripe\Stripe;
|
||||||
@ -173,7 +171,7 @@ class OrderController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
switch ($method) {
|
switch ($method) {
|
||||||
// return type => 0: QRCode / 1: URL
|
// return type => 0: QRCode / 1: URL / 2: No action
|
||||||
case 0:
|
case 0:
|
||||||
// alipayF2F
|
// alipayF2F
|
||||||
if (!(int)config('v2board.alipay_enable')) {
|
if (!(int)config('v2board.alipay_enable')) {
|
||||||
@ -218,6 +216,14 @@ class OrderController extends Controller
|
|||||||
'type' => 1,
|
'type' => 1,
|
||||||
'data' => $this->payTaro($order)
|
'data' => $this->payTaro($order)
|
||||||
]);
|
]);
|
||||||
|
case 6:
|
||||||
|
if (!(int)config('v2board.stripe_card_enable')) {
|
||||||
|
abort(500, '支付方式不可用');
|
||||||
|
}
|
||||||
|
return response([
|
||||||
|
'type' => 2,
|
||||||
|
'data' => $this->stripeCard($order, $request->input('token'))
|
||||||
|
]);
|
||||||
default:
|
default:
|
||||||
abort(500, '支付方式不存在');
|
abort(500, '支付方式不存在');
|
||||||
}
|
}
|
||||||
@ -280,6 +286,14 @@ class OrderController extends Controller
|
|||||||
array_push($data, $obj);
|
array_push($data, $obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((int)config('v2board.stripe_card_enable')) {
|
||||||
|
$obj = new \StdClass();
|
||||||
|
$obj->name = '信用卡';
|
||||||
|
$obj->method = 6;
|
||||||
|
$obj->icon = 'card';
|
||||||
|
array_push($data, $obj);
|
||||||
|
}
|
||||||
|
|
||||||
return response([
|
return response([
|
||||||
'data' => $data
|
'data' => $data
|
||||||
]);
|
]);
|
||||||
@ -347,7 +361,7 @@ class OrderController extends Controller
|
|||||||
'statement_descriptor' => $order->trade_no,
|
'statement_descriptor' => $order->trade_no,
|
||||||
'metadata' => [
|
'metadata' => [
|
||||||
'user_id' => $order->user_id,
|
'user_id' => $order->user_id,
|
||||||
'invoice_id' => $order->trade_no,
|
'out_trade_no' => $order->trade_no,
|
||||||
'identifier' => ''
|
'identifier' => ''
|
||||||
],
|
],
|
||||||
'redirect' => [
|
'redirect' => [
|
||||||
@ -357,10 +371,6 @@ class OrderController extends Controller
|
|||||||
if (!$source['redirect']['url']) {
|
if (!$source['redirect']['url']) {
|
||||||
abort(500, '支付网关请求失败');
|
abort(500, '支付网关请求失败');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Cache::put($source['id'], $order->trade_no, 3600)) {
|
|
||||||
abort(500, '订单创建失败');
|
|
||||||
}
|
|
||||||
return $source['redirect']['url'];
|
return $source['redirect']['url'];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,7 +388,7 @@ class OrderController extends Controller
|
|||||||
'type' => 'wechat',
|
'type' => 'wechat',
|
||||||
'metadata' => [
|
'metadata' => [
|
||||||
'user_id' => $order->user_id,
|
'user_id' => $order->user_id,
|
||||||
'invoice_id' => $order->trade_no,
|
'out_trade_no' => $order->trade_no,
|
||||||
'identifier' => ''
|
'identifier' => ''
|
||||||
],
|
],
|
||||||
'redirect' => [
|
'redirect' => [
|
||||||
@ -388,12 +398,38 @@ class OrderController extends Controller
|
|||||||
if (!$source['wechat']['qr_code_url']) {
|
if (!$source['wechat']['qr_code_url']) {
|
||||||
abort(500, '支付网关请求失败');
|
abort(500, '支付网关请求失败');
|
||||||
}
|
}
|
||||||
if (!Cache::put($source['id'], $order->trade_no, 3600)) {
|
|
||||||
abort(500, '订单创建失败');
|
|
||||||
}
|
|
||||||
return $source['wechat']['qr_code_url'];
|
return $source['wechat']['qr_code_url'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function stripeCard($order, string $token)
|
||||||
|
{
|
||||||
|
$currency = config('v2board.stripe_currency', 'hkd');
|
||||||
|
$exchange = Helper::exchange('CNY', strtoupper($currency));
|
||||||
|
if (!$exchange) {
|
||||||
|
abort(500, '货币转换超时,请稍后再试');
|
||||||
|
}
|
||||||
|
Stripe::setApiKey(config('v2board.stripe_sk_live'));
|
||||||
|
try {
|
||||||
|
$charge = \Stripe\Charge::create([
|
||||||
|
'amount' => floor($order->total_amount * $exchange),
|
||||||
|
'currency' => $currency,
|
||||||
|
'source' => $token,
|
||||||
|
'metadata' => [
|
||||||
|
'user_id' => $order->user_id,
|
||||||
|
'out_trade_no' => $order->trade_no,
|
||||||
|
'identifier' => ''
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
abort(500, '遇到了点问题,请刷新页面稍后再试');
|
||||||
|
}
|
||||||
|
info($charge);
|
||||||
|
if (!$charge->paid) {
|
||||||
|
abort(500, '扣款失败,请检查信用卡信息');
|
||||||
|
}
|
||||||
|
return $charge->paid;
|
||||||
|
}
|
||||||
|
|
||||||
private function bitpayX($order)
|
private function bitpayX($order)
|
||||||
{
|
{
|
||||||
$bitpayX = new BitpayX(config('v2board.bitpayx_appsecret'));
|
$bitpayX = new BitpayX(config('v2board.bitpayx_appsecret'));
|
||||||
|
@ -44,6 +44,7 @@ class ConfigSave extends FormRequest
|
|||||||
// stripe
|
// stripe
|
||||||
'stripe_alipay_enable' => 'in:0,1',
|
'stripe_alipay_enable' => 'in:0,1',
|
||||||
'stripe_wepay_enable' => 'in:0,1',
|
'stripe_wepay_enable' => 'in:0,1',
|
||||||
|
'stripe_card_enable' => 'in:0,1',
|
||||||
'stripe_sk_live' => '',
|
'stripe_sk_live' => '',
|
||||||
'stripe_pk_live' => '',
|
'stripe_pk_live' => '',
|
||||||
'stripe_webhook_key' => '',
|
'stripe_webhook_key' => '',
|
||||||
|
@ -141,4 +141,15 @@ class OrderService
|
|||||||
$order->surplus_amount = $surplusAmount > 0 ? $surplusAmount : 0;
|
$order->surplus_amount = $surplusAmount > 0 ? $surplusAmount : 0;
|
||||||
$order->surplus_order_ids = json_encode(array_map(function ($v) { return $v['id'];}, $orderModel->get()->toArray()));
|
$order->surplus_order_ids = json_encode(array_map(function ($v) { return $v['id'];}, $orderModel->get()->toArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function success(string $callbackNo)
|
||||||
|
{
|
||||||
|
$order = $this->order;
|
||||||
|
if ($order->status !== 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
$order->status = 1;
|
||||||
|
$order->callback_no = $callbackNo;
|
||||||
|
return $order->save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,11 @@
|
|||||||
"fideloper/proxy": "^4.0",
|
"fideloper/proxy": "^4.0",
|
||||||
"laravel/framework": "^6.0",
|
"laravel/framework": "^6.0",
|
||||||
"laravel/tinker": "^1.0",
|
"laravel/tinker": "^1.0",
|
||||||
|
"league/omnipay": "^3.0",
|
||||||
"lokielse/omnipay-alipay": "3.0.6",
|
"lokielse/omnipay-alipay": "3.0.6",
|
||||||
|
"omnipay/stripe": "^3.1",
|
||||||
"php-curl-class/php-curl-class": "^8.6",
|
"php-curl-class/php-curl-class": "^8.6",
|
||||||
"stripe/stripe-php": "^7.5",
|
"stripe/stripe-php": "^7.36.1",
|
||||||
"symfony/yaml": "^4.3"
|
"symfony/yaml": "^4.3"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
Loading…
Reference in New Issue
Block a user