【问题标题】:PayPal REST API (PHP) not completing transactionPayPal REST API (PHP) 未完成交易
【发布时间】:2014-10-05 23:58:51
【问题描述】:

我对 PayPal 的 REST API 比较陌生,所以我可能在这里遗漏了一个关键元素,但我所做的是在 GitHub 中设置了一个类似于他们的示例:

https://github.com/paypal/rest-api-sdk-php/blob/master/sample/payments/CreatePaymentUsingPayPal.php

我非常接近这一点,因为我在 PayPal 的开发者网站上创建的应用程序中将 API 上下文设置为客户端 ID 和机密(首先使用沙盒,现在使用实时沙盒)。这是我创建 apiContext 后的部分:

            $payer = new Payer();
            $payer->setPayment_method("paypal");

            $item = new Item();
            $item -> setName($itemDescription)
                    -> setCurrency("USD")
                    -> setQuantity(1)
                    -> setPrice($itemPrice);

            $itemList = new ItemList();
            $itemList->setItems(array($item));

            $amount = new Amount();
            $amount->setCurrency("USD")
                    ->setTotal($itemPrice);

            $transaction = new Transaction();
            $transaction->setAmount($amount)
                    ->setItemList($itemList)
                    ->setDescription($itemDescription);

            $redirectURLs = new RedirectUrls();
            $redirectURLs->setReturnUrl($successURL);
            $redirectURLs->setCancelUrl($cancelURL);

            $payment = new Payment();
            $payment->setIntent("sale");
            $payment->setPayer($payer);
            $payment->setRedirectUrls($redirectURLs);
            $payment->setTransactions(array($transaction));

            try {
                    $payment->create($apiContext);
            }
            catch (PayPal\Exception\PPConnectionException $ex) {

                    print "Exception:  " . $ex->getMessage() . PHP_EOL;
                    var_dump($ex->getData());
                    exit(1);
            }

            # get the redirect URL to pass the user on to PayPal for payment processing
            $redirectURL = "";
            foreach ( $payment->getLinks() as $link ) {
                    if ( $link->getRel() == "approval_url" ) {
                            $redirectURL = $link->getHref();
                            break;
                    }
            }

            // if we get a URL, redirect the user to PayPal's website
            if ( $redirectURL != "" ) {

                    $ppPayID = $payment->getId();

                    # write the ID to the DB
                    $dbQuery = "update members set ppPayID = '" . dbAccess("sanitize", $ppPayID) . "' where memberID = $memberID;";

                    # update the DB
                    $dbResults = dbAccess("query", $dbQuery);

                    //header("Location: $redirectURL");
                    print "<script language='JavaScript'>location.href = '$redirectURL';</script>";
                    exit;
            }
            else {
                    return false;
            }

一切似乎都正常:我在传递到 PayPal 时看到了商品信息,我可以取消交易并通过取消 URL 返回我的 PHP 应用程序,我什至能够使用真实账户提交付款并通过成功 URL 退回到我的应用程序。这是它的样子;

http://example.com/scripts.php?pp=success&memberID=1&token=EC-7G861271R3854####&PayerID=77PAUYJCFJ###

(其中 #s 也是字母数字字符,以防万一)

我遇到的问题是,虽然看起来很成功,但交易并没有出现在我的商家帐户的任何地方,也没有出现在我的个人帐户中。知道这里发生了什么吗?

【问题讨论】:

    标签: php api rest paypal


    【解决方案1】:

    因此,该示例缺少交易流程的一个关键部分:执行付款。对于其他为此苦苦挣扎的人,这里是实现这一点的必要代码:

    https://developer.paypal.com/docs/api/#execute-an-approved-paypal-payment

    注意:您需要像我在数据库中那样捕获 $payment->getID,然后将 URL PayerID 发送回来。

    【讨论】:

      【解决方案2】:

      这个例子有两个部分。第二部分是接收成功返回的 URL,并处理请求。

      正如您在https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/payments/ExecutePayment.php 中看到的那样,您需要使用请求返回的 ID 执行付款。

      根据您的用例,我会继续查看是否可以将该页面与此合并,以便人们可以清楚地看到它。如果没有,我想在示例代码中以某种方式非常清楚地说明,在该示例中还有更多的 php 文件。

      【讨论】:

        猜你喜欢
        • 2020-08-06
        • 1970-01-01
        • 2020-10-01
        • 1970-01-01
        • 2015-02-13
        • 2016-04-17
        • 2012-08-03
        • 2015-07-14
        • 2011-12-18
        相关资源
        最近更新 更多