访问https://api.sandbox.paypal.com/v1/notifications/verify-webhook-signature时获得Http响应代码400

问题描述

我已经搜索了好几个小时,希望这里有人能提供帮助。 我使用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'],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","debug_id":"26b12ee43cddd","links":[]}

我必须提到,其他所有功能都可以正常工作。 创建计划,协议,同时取消两者,显示有效计划等... 一切顺利。 这是我似乎无法解决的唯一问题。 如果有人能为我解决这个问题,我将非常感激。 谢谢!

解决方法

不建议使用PayPal-PHP-SDK,并且不再对其进行维护;它不应用于新的集成。它的计费计划和订阅的实现较旧,并且与当前版本的Subscriptions API不兼容。

应使用直接HTTPS集成(无SDK)来进行订阅和验证Webhook。

(对于一次性付款用例,有一个新的v2 Checkout-PHP-SDK https://developer.paypal.com/docs/api/rest-sdks/