Paypal IPN 不执行许可证创建代码

问题描述

让我们详细点……因为我很难过。

服务器文件结构:

/home/name/public_html/CryptlexApi.PHP
/home/name/public_html/generate-license.PHP see below
/home/name/public_html/generate-license-IPN_combined.PHP
/home/name/public_html/paypalIPN.PHP 

source

generate-license.PHP:

<?PHP
    require('CryptlexApi.PHP');

    // pass this secret as query param in the url e.g. https://yourserver.com/generate-license.PHP?cryptlex_secret=SOME_RANDOM_STRING
    $CRYPTLEX_SECRET = "SOME_RANDOM_STRING";

    // access token must have following permissions (scope): license:write,user:read,user:write
    $PERSONAL_ACCESS_TOKEN = "Yes,I have my PAT here";

    // utility functions
    function IsNullOrEmptyString($str){
        return (!isset($str) || trim($str) === '');
    }

    function ForbiddenRequest() {
        http_response_code(403);
        $message['error'] = 'You are not authorized to perform this action!';
        echo json_encode($message);
    }

    function BadRequest($error) {
        http_response_code(400);
        $message['error'] = $error;
        echo json_encode($message);
    }

    function VerifySecret($secret) {
        if($secret == $GLOBALS['CRYPTLEX_SECRET']) {
            return true;
        }
        return false;
    }

    function parsePayPalPostData() {
        $postBody['company'] = $_POST['payer_email'];
        if(IsNullOrEmptyString($postBody['email'])) {
            $postBody['company'] = "";
        }

        $postBody['quantity'] = $_POST['quantity'];
        if(IsNullOrEmptyString($postBody['quantity'])) {
            $postBody['quantity'] = NULL;
        }


        $postBody['email'] = $_POST['payer_email'];
        if(IsNullOrEmptyString($postBody['email'])) {
            BadRequest('email is missing!');
            return NULL;
        }

        $postBody['last_name'] = $_POST['last_name'];
        if(IsNullOrEmptyString($_POST['last_name'])) {
            BadRequest('last name is missing!');
            return NULL;
        }
        $postBody['first_name'] = $_POST['first_name'];
        if(IsNullOrEmptyString($_POST['first_name'])) {
            BadRequest('first name is missing!');
            return NULL;
        }

        $postBody['order_id'] = $_POST['txn_id'];
        if(IsNullOrEmptyString($postBody['order_id'])) {
            BadRequest('reference is missing!');
            return NULL;
        }
        return $postBody;
    }

    try {

        if(VerifySecret($_GET['cryptlex_secret']) == false) {
            return ForbiddenRequest();
        }

        CryptlexApi::SetAccesstoken($GLOBALS['PERSONAL_ACCESS_TOKEN']);

        $product_id = "this is my product id";

        $postBody = parsePayPalPostData();

        if($postBody == NULL) {
            echo "no data \n";
            return;
        }


        $email = $postBody['email'];
        $first_name = $postBody['first_name'];
        $last_name = $postBody['last_name'];
        $quantity = $postBody['quantity'];

        // required for renewing the license subscription
        $order_id = $postBody['order_id'];

        // creating user is optional
        $user_exists = false;
        $user = CryptlexApi::GetUser($email);
        if($user == NULL) {
            $user_body["email"] = $email;
            $user_body["firstName"] = $first_name;
            $user_body["lastName"] = $last_name;
            $user_body["company"] = $last_name;
            // generate a random 8 character password
            $user_body["password"] = substr(md5(uniqid()),8);
            $user_body["role"] = "user";
            $user = CryptlexApi::createuser($user_body);
        } else {
            $user_exists = true;
        }

        echo "Quantity = $quantity \n";
        // creating license
        if($quantity != NULL) {
            $license_body["allowedActivations"] = (int)$quantity;
        }
        $license_body["productId"] = $product_id;
        $license_body["userId"] = $user->id;
        $Metadata["key"] = "order_id";
        $Metadata["value"] = $order_id;
        $Metadata["visible"] = false;
        $license_body["Metadata"] = array($Metadata);

        $license = CryptlexApi::CreateLicense($license_body);

        http_response_code(200);
        echo $license->key;

    } catch(Exception $e) {
        http_response_code(500);
        echo 'message: ' .$e->getMessage();
    }

好的,如果我在终端中执行以下操作,我将成功创建用户/许可证

curl -d "payer_email=emailaddress%40gmail.com&quantity=1&last_name=smith&first_name=bob&txn_id=ordernumber" -X POST https://mywebsite.com/generate-license.PHP?cryptlex_secret=SOME_RANDOM_STRING

所以,我把那个代码放在 paypalIPN.PHP 中并重命名为 generate-license-IPN_combined.PHP 在paypalIPN.PHP文件中,我在这里插入了上面的代码

// Check if PayPal verifies the IPN data,and if so,return true.
if ($res == self::VALID) {
    ######## I put all of my code above right here #########
    return true;
} else {
    return false;
}

IPN 代码似乎有效,因为 Paypal IPN 模拟器说它有效。但是,数据库端没有任何反应。我已经删除了检查,甚至将这段代码放在 IPN 之前,但它不起作用。请帮忙。

解决方法

在沙箱中生成测试 IPN 的一种快速方法是使用以下形式的链接进行支付:

https://www.sandbox.paypal.com/webscr?cmd=_xclick&item_name=test&amount=100&currency_code=USD&business=sandboxbusinessemail@domain.com&notify_url={URL_ENCODE_YOUR_IPN_LISTENER_URL}

(参考:HTML Variables for PayPal Payments Standard

通过以下方式获取一个沙盒企业帐户以进行上述操作,并使用一个沙盒个人帐户进行支付:https://www.paypal.com/signin?intent=developer&returnUri=https%3A%2F%2Fdeveloper.paypal.com%2Fdeveloper%2Faccounts%2F

通过以下方式查看企业帐户的 IPN 历史记录:https://www.sandbox.paypal.com/webscr?cmd=_display-ipns-history