stripe test

This commit is contained in:
Tokumeikoi 2020-06-04 18:00:33 +08:00
parent ca04634537
commit 136cffcf13
7 changed files with 98 additions and 25 deletions

View File

@ -2,7 +2,12 @@
namespace App\Console\Commands;
use App\Utils\Helper;
use Illuminate\Console\Command;
use Stripe\Source;
use Stripe\Stripe;
use Stripe\StripeClient;
use Omnipay\Omnipay;
class Test extends Command
{
@ -37,5 +42,21 @@ class Test extends Command
*/
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();
}
}
}

View File

@ -76,6 +76,7 @@ class ConfigController extends Controller
// stripe
'stripe_alipay_enable' => (int)config('v2board.stripe_alipay_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_pk_live' => config('v2board.stripe_pk_live'),
'stripe_webhook_key' => config('v2board.stripe_webhook_key'),

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Guest;
use App\Services\OrderService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Order;
@ -67,21 +68,25 @@ class OrderController extends Controller
switch ($event->type) {
case 'source.chargeable':
$source = $event->data->object;
$charge = \Stripe\Charge::create([
\Stripe\Charge::create([
'amount' => $source['amount'],
'currency' => $source['currency'],
'source' => $source['id'],
'description' => config('v2board.app_name', 'V2Board') . $source['metadata']['invoice_id'],
'metadata' => $source->metadata
]);
if ($charge['status'] == 'succeeded') {
$trade_no = Cache::get($source['id']);
die('success');
break;
case 'source.succeeded':
$source = $event->data->object;
if ($source->status === 'succeeded') {
$metaData = $source->metadata;
$trade_no = $metaData->out_trade_no;
if (!$trade_no) {
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');
}
Cache::forget($source['id']);
die('success');
}
break;
@ -143,11 +148,7 @@ class OrderController extends Controller
if (!$order) {
abort(500, 'order is not found');
}
if ($order->status !== 0) {
return true;
}
$order->status = 1;
$order->callback_no = $callbackNo;
return $order->save();
$orderService = new OrderService($order);
return $orderService->success($callbackNo);
}
}

View File

@ -9,12 +9,10 @@ use App\Services\OrderService;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use App\Models\Order;
use App\Models\Plan;
use App\Models\User;
use App\Models\Coupon;
use App\Utils\Helper;
use Omnipay\Omnipay;
use Stripe\Stripe;
@ -173,7 +171,7 @@ class OrderController extends Controller
]);
}
switch ($method) {
// return type => 0: QRCode / 1: URL
// return type => 0: QRCode / 1: URL / 2: No action
case 0:
// alipayF2F
if (!(int)config('v2board.alipay_enable')) {
@ -218,6 +216,14 @@ class OrderController extends Controller
'type' => 1,
'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:
abort(500, '支付方式不存在');
}
@ -280,6 +286,14 @@ class OrderController extends Controller
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([
'data' => $data
]);
@ -347,7 +361,7 @@ class OrderController extends Controller
'statement_descriptor' => $order->trade_no,
'metadata' => [
'user_id' => $order->user_id,
'invoice_id' => $order->trade_no,
'out_trade_no' => $order->trade_no,
'identifier' => ''
],
'redirect' => [
@ -357,10 +371,6 @@ class OrderController extends Controller
if (!$source['redirect']['url']) {
abort(500, '支付网关请求失败');
}
if (!Cache::put($source['id'], $order->trade_no, 3600)) {
abort(500, '订单创建失败');
}
return $source['redirect']['url'];
}
@ -378,7 +388,7 @@ class OrderController extends Controller
'type' => 'wechat',
'metadata' => [
'user_id' => $order->user_id,
'invoice_id' => $order->trade_no,
'out_trade_no' => $order->trade_no,
'identifier' => ''
],
'redirect' => [
@ -388,12 +398,38 @@ class OrderController extends Controller
if (!$source['wechat']['qr_code_url']) {
abort(500, '支付网关请求失败');
}
if (!Cache::put($source['id'], $order->trade_no, 3600)) {
abort(500, '订单创建失败');
}
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)
{
$bitpayX = new BitpayX(config('v2board.bitpayx_appsecret'));

View File

@ -44,6 +44,7 @@ class ConfigSave extends FormRequest
// stripe
'stripe_alipay_enable' => 'in:0,1',
'stripe_wepay_enable' => 'in:0,1',
'stripe_card_enable' => 'in:0,1',
'stripe_sk_live' => '',
'stripe_pk_live' => '',
'stripe_webhook_key' => '',

View File

@ -141,4 +141,15 @@ class OrderService
$order->surplus_amount = $surplusAmount > 0 ? $surplusAmount : 0;
$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();
}
}

View File

@ -13,9 +13,11 @@
"fideloper/proxy": "^4.0",
"laravel/framework": "^6.0",
"laravel/tinker": "^1.0",
"league/omnipay": "^3.0",
"lokielse/omnipay-alipay": "3.0.6",
"omnipay/stripe": "^3.1",
"php-curl-class/php-curl-class": "^8.6",
"stripe/stripe-php": "^7.5",
"stripe/stripe-php": "^7.36.1",
"symfony/yaml": "^4.3"
},
"require-dev": {