簡體   English   中英

訪問https://api.sandbox.paypal.com/v1/notifications/verify-webhook-signature時得到Http響應碼400

[英]Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/notifications/verify-webhook-signature

我一直在尋找這個幾個小時,我希望這里有人可以提供幫助。 我使用 PayPal PHP SDK 在 Laravel 和 PayPal 訂閱上建立了一個基於訂閱的網站。一切正常,除了一件事:我創建了一個 webhook,用於當用戶取消付款時。 我收到此錯誤:

Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/notifications/verify-webhook-signature.{"name":"VALIDATION_ERROR","message":"Invalid data provided","debug_id":"7225cebfec35a","information_link":"https://developer.paypal.com/docs/api/webhooks/#errors","details":[{"field":"webhook_id","location":"body","issue":"Required field cannot be blank"}],"links":[]}  

這是我的代碼:

public function webhook()
{
    

    /**
    * Receive the entire body that you received from PayPal webhook.
    */
    $bodyReceived = file_get_contents('php://input'); 

    // Receive HTTP headers that you received from PayPal webhook.
    $headers = getallheaders(); 

    /**
    * Uppercase all the headers for consistency
    */
    $headers = array_change_key_case($headers, CASE_UPPER); 

    $signatureVerification = new \PayPal\Api\VerifyWebhookSignature(); 
    $signatureVerification->setWebhookId(env('PAYPAL_WEBHOOK_ID')); 
    $signatureVerification->setAuthAlgo($headers['PAYPAL-AUTH-ALGO']); 
    $signatureVerification->setTransmissionId($headers['PAYPAL-TRANSMISSION-ID']); 
    $signatureVerification->setCertUrl($headers['PAYPAL-CERT-URL']); 
    $signatureVerification->setTransmissionSig($headers['PAYPAL-TRANSMISSION-SIG']); 
    $signatureVerification->setTransmissionTime($headers['PAYPAL-TRANSMISSION-TIME']); 

    $webhookEvent = new \PayPal\Api\WebhookEvent(); 
    $webhookEvent->fromJson($bodyReceived); 
    $signatureVerification->setWebhookEvent($webhookEvent); 
    $request = clone $signatureVerification; 

    try {
        $output = $signatureVerification->post($this->apiContext);
        
    } catch(\Exception $ex) {
        //This is where it fails
        print_r($ex->getMessage());
        exit(1);
    }

    $verificationStatus = $output->getVerificationStatus();
    $responseArray = json_decode($request->toJSON(), true);

    $event = $responseArray['webhook_event']['event_type'];

    if ($verificationStatus == 'SUCCESS')
    { 

        switch($event)
        {
            case 'BILLING.SUBSCRIPTION.CANCELLED':
            case 'BILLING.SUBSCRIPTION.SUSPENDED':
            case 'BILLING.SUBSCRIPTION.EXPIRED':
            case 'BILLING_AGREEMENTS.AGREEMENT.CANCELLED':

            // $user = User::where('payer_id',$responseArray['webhook_event']['resource']['payer']['payer_info']['payer_id'])->first();
            $this->updateStatus($responseArray['webhook_event']['resource']['payer']['payer_info']['payer_id'], 0,1);
            
            break;
        }
    }
    echo $verificationStatus;
    
    exit(0);
}

這是$this->apiContext

trait PayPalApiCredentialsTrait {

private $apiContext;

public function setCredentials()
{
    $this->apiContext = new \PayPal\Rest\ApiContext(
        new \PayPal\Auth\OAuthTokenCredential(
            env('PAYPAL_CLIENT_ID'),     // ClientID
            env('PAYPAL_CLIENT_SECRET')      // ClientSecret
        )
    );

    $this->apiContext->setConfig(
        array(
            'mode' => env("PAYPAL_MODE"),
            'log.LogEnabled' => true,
            'log.FileName' => '../PayPal.log',
            'log.LogLevel' => 'INFO', // PLEASE USE `INFO` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS
        )
    );
}

}

這是我從 paypal.log 得到的錯誤:

        [01-09-2020 15:54:18] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.sandbox.paypal.com/v1/oauth2/token
    [01-09-2020 15:54:18] PayPal\Core\PayPalHttpConnection : INFO: Response Status  : 200
    [01-09-2020 15:54:18] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.sandbox.paypal.com/v1/notifications/verify-webhook-signature
    [01-09-2020 15:54:19] PayPal\Core\PayPalHttpConnection : INFO: Response Status  : 400
[01-09-2020 15:54:19] PayPal\Core\PayPalHttpConnection : ERROR: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/notifications/verify-webhook-signature. {"name":"VALIDATION_ERROR","message":"Invalid data provided","debug_id":"26b12ee43cddd","information_link":"https://developer.paypal.com/docs/api/webhooks/#errors","details":[{"field":"webhook_id","location":"body","issue":"Required field cannot be blank"}],"links":[]}

我必須提一下,其他一切都很好。 創建計划、協議、取消兩者、顯示有效計划等等……一切都進行得很順利。 這是我似乎無法解決的唯一問題。 如果有人能幫我解決這個問題,我將不勝感激。 謝謝!

PayPal-PHP-SDK 已棄用,不再維護; 它不應該用於新的集成。 它的計費計划和訂閱的實施是舊的,並且與當前版本的訂閱 API 不兼容。

取而代之的是,直接 HTTPS 集成(無 SDK)應該用於訂閱和驗證 webhook。

(對於一次性支付用例,有一個新的 v2 Checkout-PHP-SDK https://developer.paypal.com/docs/api/rest-sdks/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM