如何使用Paypal的-PHP-SDK REST API取消Laravel中的订阅?

时间:2019-09-01 17:09:17

标签: php mysql laravel paypal-rest-sdk paypal-subscriptions

当用户取消订阅时,我试图将subscription_status表中的0列从1更改为Users(从true到false)。我购买了在线课程,并且正在使用讲师的代码,但只有这一部分对我不起作用。当我使用沙盒帐户测试所有内容时,它会显示“ 您的订阅已取消”,并且一切看起来都不错,除了数据库中的列未更新为0。我正在Sandbox上以LIVE server模式进行测试。我正在使用 PHP Laravel Mysql 数据库。

这是我的 UpdateSubscriptionTrait.php 文件中的代码:

trait UpdateSubscriptionTrait {

    private function updateStatus($payer_id, $value)
    {
        DB::table('users')
            ->where('payer_id', $payer_id)
            ->update(['subscription_status' => $value]);
    }

}

这是我的 PayPalAdminController.php 文件中的webhook函数的代码:

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) {
            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);
                break;
            }
        }
        echo $verificationStatus;
        exit(0);
    }

你知道怎么了吗?

0 个答案:

没有答案