【问题标题】:Paypal IPN not executing license creation codePaypal IPN 未执行许可证创建代码
【发布时间】:2023-04-02 01:34:01
【问题描述】:

让我们详细一点……因为我被难住了。

服务器文件结构:

/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

生成许可证.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()), 0, 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,它不会告诉你你的代码是否能够回发IPN并得到有效的响应。那是你自己决定的。
  • 你确定。模拟器以成功或失败的握手响应。握手告诉我它收到了我的代码的有效响应。
  • 握手是否成功取决于他们是否收到来自发布 IPN 的 HTTP 2xx 响应。验证 IPN 是一个单独的步骤,涉及您的服务器将 IPN 发布回 PayPal。
  • 对,PaypalIPN.php 中的代码就是这样做的。此代码:github.com/paypal/ipn-code-samples/blob/master/php/…
  • 当然,这是一些代码。它是否成功验证了您服务器上的特定 IPN 仍有待观察。这就是您需要添加完整日志记录和调试的内容。

标签: php paypal-ipn


【解决方案1】:

在沙盒中生成测试 IPN 的一种快速方法是使用以下表单的链接付款:

https://www.sandbox.paypal.com/webscr?cmd=_xclick&amp;item_name=test&amp;amount=100&amp;currency_code=USD&amp;business=sandboxbusinessemail@domain.com&amp;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

【讨论】:

  • 首先,这非常有帮助,谢谢。可悲的是,它仍然不起作用。我设置了一个沙盒买家/卖家。在我的网站上制作了沙盒按钮,并更改了卖家 IPN 以匹配我的。交易成功,但未生成我的许可证。不能为我的生活弄清楚我做错了什么。
  • 以上不是修复,它是为了您可以生成测试事件并调试问题。您需要全面检测您的代码并记录所有内容以找出问题所在。
  • 不要忘记为您链接到的上述示例致电useSandbox()。您可以根据您是否收到带有 test_ipn=1 的 IPN 来使其成为动态的,但还要确保沙盒事件实际上不会生成和发送许可证,而是记录事情并进行许可证创建的“试运行”过程。
  • 如您所知,我不是软件工程师(对 PHP 完全陌生),因此感谢您的耐心等待。我没有调用 useSandbox() (因为我是个白痴)。我尝试过创建日志和日志文件,但它们并没有像我期望的那样显示在服务器上。无法调试代码/交互是一个巨大的 PIA。
  • 试着给他们一个像/tmp/mylog.txt这样的路径
猜你喜欢
  • 1970-01-01
  • 2016-05-27
  • 2012-03-09
  • 2016-06-26
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多