【问题标题】:multiple charging with stripe and laravel使用 stripe 和 laravel 进行多次充电
【发布时间】:2021-09-01 15:14:33
【问题描述】:

我试图第一次向客户收取条纹作为押金,第二次是在 5 分钟后,这是剩余的金额,这由 Laravel Queue 处理。但由于某种原因,第二次收费没有发生

这是我的控制器

public function StripeCharge(Request $request) {

        $product_name = $request->product_name;
        $product_price = $request->product_price;

 

    Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
        $customer = \Stripe\Customer::create(array(
            "email" => $email = Auth::user()->email,
            "source" => $request->stripeToken
        ));

        $mailInfo = \Stripe\Charge::create(array(
             "amount" => $product_price * 100,
             "currency" => "gbp",
             "customer" => $customer->id,
             "description" => "Deposit" . "-" . $product_name 
        ));

        $customerId = $customer->id;
        $amount = $product_price;


             
  
        $email = Auth::user()->email;
            
          Mail::to($email)->send(new WelcomeMail($mailInfo));

        
        $fullamount =  New ChargeCustomer($customerId,$amount); 


          dispatch(New ChargeCustomer( $fullamount) )->delay(now()->addMinutes(5));
        

        
          
        return view('confirmation');

    }


这是我的工作 CustomerCharge

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Stripe;

class ChargeCustomer implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $customerId;
    public $amount;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(string $customerId, int $amount)
    {
       $this->customerId = $customerId;
       $this->amount = $amount;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        \Stripe\Charge::create([
            'amount' => $this->amount,
            'currency'=> 'gbp',
            'customer' => $this->customerId

        ]);

        ChargeCustomer::dispatch('cus_test',1000);
    }
}



任何帮助将不胜感激

【问题讨论】:

    标签: laravel queue stripe-payments


    【解决方案1】:

    我认为主要问题是您在发送第二次付款时没有指定客户:

    New ChargeCustomer( $fullamount)
    

    我相信应该是 New ChargeCustomer($customerId, $fullamount)

    但是,在更高级别上,不建议以这种方式一个接一个地创建两个 Charge。两项单独的费用会比使用一项费用产生两倍的费用,并且在持卡人查看他们的账单时会显得不寻常。

    如果您想确保您的客户能够并且愿意在实际收款前付款,您可以暂缓使用银行卡。这样做可以保证资金可用,保留它们以供您以后获取,并且只使用一次 Charge。

    以下是 Stripe 的文档,用于使用您当前使用的较旧的 Charges API 来保留卡片:https://stripe.com/docs/charges/placing-a-hold

    这是 Stripe 关于更新的 Payment Intents API 的更新文档,他们建议您改用:https://stripe.com/docs/payments/capture-later

    【讨论】:

    • 您好,感谢您的回复。我进行了更改,现在我正在传递客户 ID,但由于以下原因,队列失败:未提供 API 密钥。 (提示:使用“Stripe::setApiKey()”设置您的 API 密钥。您可以从 Stripe Web 界面生成 API 密钥。有关详细信息,请参阅stripe.com/api,或发送电子邮件至 support@stripe.com,如果您有任何问题。
    • 我不确定 Laravel 的调度队列是如何工作的,但是这些队列完全有可能使用一个完全独立的 Stripe 实例,而不是您在创建排队作业时使用 API 密钥初始化的实例。您可能需要在作业本身中使用您的密钥初始化 Stripe。
    • @DonchoBorisov 您可以将字符串Stripe\Stripe::setApiKey(env('STRIPE_SECRET')); 添加到ServiceProvider。这就是您将 API 密钥分配给系统所有模块的方式。控制器和 CLI 模块(在您的情况下是队列工作者)是不同的入口点,它们彼此一无所知。
    猜你喜欢
    • 2012-12-21
    • 1970-01-01
    • 2014-06-06
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多