实现 stripe 支付

/**

  • stripe支付 * @param Request $request
    • @return JsonResponse
    • @throws \Exception
    • /
      public function readyToPay(Request $request){
      $this->validate($request, [
      ‘orderGuid’ => ‘required|string|between:1,50’
      ]);
      // file_put_contents(‘readyToPay.txt’,json_encode($request->all()));
      //查询订单 $Order=Order::query()

->where(‘Deleted’,false)
->where(‘Guid’,$request->orderGuid)
->first();

if(empty($Order)){
return $this->respondWithError(‘订单不存在’);
} if (
$Order->PaymentStatus == Order::PAYMENTS_STATUS_PAID
|| $Order->Status == Order::STATUS_REFUND
) {
return $this->respondWithError(‘Order Already Paid’);
} $payment_data=[];

try {
//查询支付配置
$Stripe=Stripe::query()->where(‘StoreGuid’,$Order->StoreGuid)->first();
//新版stripe支付方式

$stripe_api_key = $Stripe->stripe_private_key;
if($stripe_api_key){
return $this->respondWithError(‘请配置stripe支付’);
}
\Stripe\Stripe::setApiKey($stripe_api_key);

$intent = \Stripe\PaymentIntent::create([
‘amount’ =>$Order->ActuallyPaidAmount*100,
‘currency’ => ‘EUR’,
]); $customer_id=null;

$payment_data[‘customerId’] = $customer_id ? $customer_id : null;

$payment_data[‘stripeClientSecret’] = $intent->client_secret;

} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
$data=array_merge(
[ ‘amount’ => $Order->ActuallyPaidAmount,
‘OrderId’ => $Order->Id,
‘OrderGuid’ => $Order->Guid
],
$payment_data
);
return $this->respond($data);
}

/**

  • 支付回调 * @param Request $request
    • @return JsonResponse
    • /
      public function ExecutePay(Request $request){
      $this->validate($request, [
      ‘orderGuid’ => ‘required|string|between:1,50’,
      ‘strip_payment_intent_id’ => ‘required’,
      ]); DB::beginTransaction();
      //查询订单
      $Order=Order::query()

->where(‘Deleted’,false)
->where(‘Guid’,$request->orderGuid)
->first();
try{
//查询支付配置
$Stripe=Stripe::query()->where(‘StoreGuid’,$Order->StoreGuid)->first();
\Stripe\Stripe::setApiKey($Stripe->stripe_private_key);
$intent = \Stripe\PaymentIntent::retrieve($request->strip_payment_intent_id);
$charges = $intent->charges->data;
$charge = $charges[0];

if($charge->paid){
$transaction_id = $charge->id;
//支付成功后修改订单支付状态
$Order->fill([
‘transaction_id’=>$transaction_id,
‘paymentStatus’=>Order::PAYMENTS_STATUS_PAID,
‘PaymentMethod’=>Order::PAYMENT_METHOD_VISA_STRIPE_ONLINE,
]); if ($Order->save()) {
// 发送打印事件
switch ($Order->Type) {
//订单类型
case Order::TYPE_DINE:
$scene = PrintScene::SCENE_DINE;
break;
case Order::TYPE_TAKEAWAY:
$scene = PrintScene::SCENE_TAKEAWAY;
break;
case Order::TYPE_Buffet:
$scene = PrintScene::SCENE_DINE;
break;
case Order::TYPE_QRCODE:
$scene = PrintScene::SCENE_QR_CODE;
break;
case Order::TYPE_QRCODE_S:
$scene = PrintScene::SCENE_QR_CODE;
break;
default:
$scene = PrintScene::SCENE_DINE;
} // 订单触发方式 : 下单打印
$trigger = PrintScene::TRIGGER_ORDER;
$event = new PrintEvent($Order->StoreGuid, $scene, $trigger, [], $Order, ‘’);
event($event);

DB::commit();
return $this->respond([
‘orderId’ => $Order->Id,
‘orderGuid’ => $Order->Guid,
]); } else {
DB::rollBack();
return $this->respondWithError(‘订单更新失败’);
} } else{
DB::rollBack();
return $this->respondWithError(‘订单更新失败’);
} }catch (\Exception $e){
DB::rollBack();
return $this->respondWithError(‘订单更新失败’.$e->getMessage());
}}

github文档地址:github.com/stripe/stripe-php

本作品采用《CC 协议》,转载必须注明作者和本文链接
lijizheng
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!