www.lightcourse.com/app/home/controller/Ready.php

155 lines
4.8 KiB
PHP
Raw Normal View History

2022-10-24 02:38:36 +00:00
<?php
// This example sets up an endpoint using the Slim framework.
// Watch this video to get started: https://youtu.be/sGcNPFX1Ph4.
namespace app\home\controller;
use think\facade\View;
use think\facade\Lang;
use think\facade\Db;
use Slim\Http\Request;
use Slim\Http\Response;
use Stripe\Stripe;
require_once root_path() . 'vendor/autoload.php';
class Ready extends BaseMall {
private static $key;
public function initialize() {
parent::initialize(); // TODO: Change the autogenerated stub
self::$key = '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';
}
/**
* Stripe支付
* @access private
* @author super
* @date 2020-11-04
* @param array $product 商品信息
* @param int|float $amount 支付金额
* @param array $pay_type 支付类型
* @param string $currency 货币类型 小写
* @return array
*/
private static function stripePay($product, $amount, $pay_type = ['card'], $currency = 'aud')
{
require_once root_path() . 'vendor/autoload.php';
require_once root_path() . 'vendor/stripe/stripe-php/init.php';
\Stripe\Stripe::setApiKey(self::$key);
header('Content-Type: application/json');
$YOUR_DOMAIN = 'https://lianke.keyutest.com/payment/';
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => $pay_type,
'line_items' => [[
'price_data' => [
'currency' => $currency,
'unit_amount' => $amount,
'product_data' => $product
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/succ', // 支付成功后跳转url
'cancel_url' => $YOUR_DOMAIN . '/cancel', // 支付失败后跳转url
]);
var_dump($checkout_session);die;
return ['id' => $checkout_session->id, 'payment_intent' => $checkout_session->payment_intent]; // payment_intent是识别支付与通知关系的唯一凭证
}
public function pay()
{
$product = [
'name' => 'Order Number' . '123123123' ,
'images' => [
'https://rongsp.com/Super/favicon.ico'
]
];
$amount = 100;
$result = self::stripePay($product, $amount);
if (!isset($result['id']) || !isset($result['payment_intent'])) {
returnData(500, 'ERROR! BUSY NETWORK!');
}
// 将凭证保存,便于在回调时查询订单
// $update = Db::name('orders')->where('order_id', $order_info['order_id'])->update(['payment_intent' => $result['payment_intent']]);
/* if (!$update) {
returnData(500, 'ERROR! BUSY NETWORK!');
}*/
// returnData(200, 'SUCCESS!', ['id' => $result['id']]);
}
// 支付成功通知
public function successNotify()
{
// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey(self::$key);
$payload = @file_get_contents('php://input');
$event = null;
try {
$event = \Stripe\Event::constructFrom(
json_decode($payload, true)
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
}
// Handle the event
switch ($event->type) {
case 'charge.succeeded':
$succeeded = $event->data->object;
if ($succeeded->status == 'succeeded'){
$payment_intent = $succeeded->payment_intent;
// 查询订单详情
$order_info = Db::name('orders')->where('payment_intent', $payment_intent)->find();
if (!$order_info) {
http_response_code(200);exit;
}
if ($order_info['status'] != 0) {
return;
}
// 订单标记为处理中
Db::name('orders')->where('order_id', $order_info['order_id'])->update(['pay_time' => time(), 'status' => 3]);
// 订单处理
// ...
// 标记为处理完成
Db::name('orders')->where('order_id', $order_info['order_id'])->update(['status' => 1]);
}
break;
default:
echo 'Received unknown event type ' . $event->type;
}
http_response_code(200);
}
}