php 实现stripe支付流程

1.申请账号获取密钥key

2.申请创建商品,创建价格,创建支付,

复制代码
//创建商品
public function create_product(){  
    $_key = self::STRIPE_KEY;
    $stripe = new \Stripe\StripeClient($_key);
    $arr =  $stripe->products->create([
        'name' => $goods_name,
    ]);
    //  print_r($arr->id);die;
    $this->create_price($arr->id,$order_id,$is_source);
}
复制代码
//创建价格
public function create_price($product_id,$order_id,$is_source){
    //获取订单对应的商品价格
    $goods_price = \db('order')->where('id',$order_id)->value('pay_money');
  
    $_key = self::STRIPE_KEY;
    $stripe = new \Stripe\StripeClient($_key);
    $price_arr = $stripe->prices->create([
       'unit_amount' => $goods_price*100,
        //'unit_amount' => 1*100,
        'currency' => 'usd',
        'tax_behavior' => 'exclusive',
        //'recurring' => ['interval' => 'day'],
        'product' => $product_id,
    ]);
    //print_r($price_arr->id);die;
    $this->actionStripe($price_arr->id,$order_id,$is_source);
}
复制代码
/**
 * 创建stripe支付
 */
public function actionStripe($price_id,$order_id,$is_source)
{
    $_key = self::STRIPE_KEY;

    $domain = $this->request->domain();

    //如果是AI订阅和AI作品打样的话
     if ($is_source == 8 || $is_source == 9){
         $cancel_url = 'https://ai.jewelryhunt.net/index/aimobile/mobile_list';
         $success_url = 'https://ai.jewelryhunt.net/index/aimobile/order';
     }else{
         $cancel_url = $domain.'/index/user/orders';
         $success_url = $domain . '/index/stripepay/success_info';
     }
    // stripe 生成订单

    \Stripe\Stripe::setApiKey($_key);
    $checkout_session = \Stripe\Checkout\Session::create([
        'line_items' => [[
            'price' => $price_id, // 产品id
            'quantity' => 1,
        ]],
        'mode' => 'payment',
        'success_url' => $success_url,
        'cancel_url' => $cancel_url,
        'automatic_tax' => [
            'enabled' => true,
        ],
        'metadata' => [
            'order_id' => $order_id,
        ],
    ]);
    header("HTTP/1.1 303 See Other");
    header("Location: " . $checkout_session->url);

}

4.配置回调事件

复制代码
/**
 * stripe支付回调
 */
public function actionNotify()
{
    \think\Log::record('支付进来了', 'info');
    $_key = self::STRIPE_KEY;
    \Stripe\Stripe::setApiKey($_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
    \think\Log::record('event12333333' . var_export($event->type, true), 'info');
    switch ($event->type) {
        case 'checkout.session.completed':
            $succeeded = $event->data->object;
            $content = "=========".date('Y-m-d H:i:s',time())."==========\r\n";
            $content .= json_encode($succeeded);
            \think\Log::record('content=======' . var_export($content, true), 'info');
            $token = input('token', '');

            if ($succeeded->status == 'complete') {
                $order_id = $succeeded->metadata->order_id;
                $pay_type = input('pay_type', 1);
      
            }
            break;
        case 'checkout.session.async_payment_failed':
            \think\Log::record('pay is failed', 'info');
            break;
        default:
            echo 'Received unknown event type ' . $event->type;
            break;
    }
    \think\Log::record('done', 'info');
    return true;
}
相关推荐
qydz1115 分钟前
杰理开发板做TWS耳机类型方案分享(1)
开发语言·pcb工艺·嵌入式开发·杰理科技
浩风祭月34 分钟前
我用 AI 辅助重构了遗留项目的认证模块:从明文存储到 OAuth 2.0 的安全升级
后端·php·ai编程
Cloud_Shy6181 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第六章 Item 40 - 43)
android·开发语言·人工智能·笔记·python·学习方法
半只小闲鱼1 小时前
配置计划模块通用办公设备家具批复数合计计算
开发语言·python
qq_422152571 小时前
Word 文件太大怎么压缩?2026 年文档瘦身方案对比
开发语言·c#·word
charliedev1 小时前
Jedi:Python 自动补全与静态分析的实用工具
开发语言·python·其他
宋拾壹1 小时前
fastadmin列表中查看列表,并且添加增加相应的数据
javascript·php·fastadmin
AFinalStone1 小时前
Android12 U盘插拔链路源码全解析(五):Framework层(下) StorageManagerService
android·frameworks
ji198594432 小时前
MATLAB 求散点曲线斜率
开发语言·算法·matlab
kaikaile19952 小时前
MATLAB 实现:Koch & Zhao 图像水印算法(DCT域)
开发语言·算法·matlab