繁体   English   中英

PayPal REST API(PHP)未完成交易

[英]PayPal REST API (PHP) not completing transaction

我对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应用程序,我什至能够使用真实帐户提交付款并被退回通过成功网址返回我的应用。 这是它的样子;

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

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

我的问题是,尽管交易看起来很成功,但交易没有显示在我的商人帐户的任何地方,也没有显示在我的个人帐户上。 知道这里发生了什么吗?

因此,该示例缺少交易过程的关键部分:执行付款。 对于其他为此苦苦挣扎的人,以下是实现此目的的必要代码:

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

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

该示例分为两部分。 第二部分是接收成功返回的URL,并处理请求。

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

根据您的用例,我将继续进行下去,看看是否可以将此页面与该页面合并,以使它对人们来说变得显而易见。 如果没有,我想在示例代码中以某种方式非常清楚地表明,在该示例中,有比该php文件更多的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM