laravel条纹织带

时间:2020-03-18 22:16:46

标签: php laravel

我有一个laravel应用,我想启用Stripe付款,尤其是使用webhooks的异步流程!我还没有使用过Webhooks,所以我很困惑并且有几个问题:

首先,我在服务器中创建一个PaymentIntent,并将PI机密发送给客户端。

public function createPaymentIntent(Request $request)
    {

            $intent = PaymentIntent::create([
                'setup_future_usage' => 'off_session',
                'amount' => 1099,
                'currency' => 'usd',
            ]);

            return response()->json([
                'intent_secret' => $intent->client_secret,
            ], 200); 


    }

现在在客户端中,我执行ConfirmCardPayment,这将返回一个Promise

this.stripe.confirmCardPayment(
        this.intent_secret,
        {
            payment_method: {card: this.cardNumberElement}
        }
        )
        .then(function(result)
        {
            if (result.error) 
            {
            // Display error.message in your UI.
            }
            else
            {
            // The payment has succeeded
            // Display a success message
            }
        });

现在这是一个令人困惑的时刻,显然我必须设置webhooks来监听身份验证/付款完成后的情况,我检查了文档,然后将其PHP示例保存到我的laravel代码中,但是我有很多问题:

public function stripeWebhook(Request $request)
    {

        // You can find your endpoint's secret in your webhook settings
        $endpoint_secret = config('services.stripe.webhooksecret');

        $payload = @file_get_contents('php://input');
        $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
        $event = null;

        try
        {
            $event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
        } 
        catch(\UnexpectedValueException $e)
        {
                // Invalid payload
                return response()->json([
                    'message' => 'Invalid payload',
                ], 200);
        }
        catch(\Stripe\Exception\SignatureVerificationException $e)
        {
            // Invalid signature
            return response()->json([
                'message' => 'Invalid signature',
            ], 200);
        }

        if ($event->type == "payment_intent.succeeded")
        {
            //As I understand here is where I should do things like send order info by mail and deplete stock accordingly

            $intent = $event->data->object;

            //$this->completeOrderInDatabase()
            //$this->sendMail();

            return response()->json([
                'intentId' => $intent->id,
                'message' => 'Payment succeded'
            ], 200); 
        } 
        elseif ($event->type == "payment_intent.payment_failed")
        {
            //Payment failed to be completed

            $intent = $event->data->object;
            $error_message = $intent->last_payment_error ? $intent->last_payment_error->message : "";

            return response()->json([
                'intentId' => $intent->id,
                'message' => 'Payment failed: '.$error_message
            ], 400); 
        }


    }

那么首先,我是否应该在其中创建一个名为domain.com/webhook的POST路由,并从该路由中调用我的webhookController方法?

第二,什么是有效载荷和签名?这些似乎是必需的参数,但我不知道它们到底是什么...

我应该在哪里向客户附加付款方式?在文档中,我想从PaymentIntent对象获取付款方式,但是在客户端或服务器中都是这样。

https://stripe.com/docs/payments/payment-intents/migration#saving-cards-checkout

从文档中

$payment_method = \Stripe\PaymentMethod::retrieve('{{PAYMENT_METHOD_ID}}');
$payment_method->attach(['customer' => '{{CUSTOMER_ID}}']);

0 个答案:

没有答案