使用paypal sdk成功购买后,如何向客户回复付款金额?

时间:2016-02-17 19:22:54

标签: php paypal paypal-sandbox paypal-rest-sdk

我可以使用paypal rest sdk成功创建付款。

以下代码属于第I页重定向成功客户。

success.php

// #Execute Payment Sample
// This is the second step required to complete
// PayPal checkout. Once user completes the payment, paypal
// redirects the browser to "redirectUrl" provided in the request.

require "app/start.php";

use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\ExecutePayment;
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;
use PayPal\Api\Transaction;
// ### Approval Status
// Determine if the user approved the payment or not
if (isset($_GET['paymentId']) && $_GET['token'] && $_GET['PayerID']) {
    // Get the payment Object by passing paymentId
    // payment id was previously stored in session in
    // CreatePaymentUsingPayPal.php
    $paymentId = $_GET['paymentId'];
    $payment = Payment::get($paymentId, $paypal);
    // ### Payment Execute
    // PaymentExecution object includes information necessary
    // to execute a PayPal account payment.
    // The payer_id is added to the request query parameters
    // when the user is redirected from paypal back to your site
    $execution = new PaymentExecution();
    $execution->setPayerId($_GET['PayerID']);

    try {
        // Execute the payment
        // (See bootstrap.php for more on `ApiContext`)
        $result = $payment->execute($execution, $paypal);
        // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
        //ResultPrinter::printResult("Executed Payment", "Payment", $payment->getId(), $execution, $result);
        echo $payment->getId();
        echo "<br>";
        var_dump($result);

        try {
            $payment = Payment::get($paymentId, $paypal);
        } catch (Exception $ex) {
            // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
            //ResultPrinter::printError("Get Payment", "Payment", null, null, $ex);
            echo "error 1";
            exit(1);
        }
    } catch (Exception $ex) {
        // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
        //ResultPrinter::printError("Executed Payment", "Payment", null, null, $ex);
        echo "error 2";
        exit(1);
    }
    // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
    //ResultPrinter::printResult("Get Payment", "Payment", $payment->getId(), null, $payment);

    //return $payment;
} else {
    // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
    //ResultPrinter::printResult("User Cancelled the Approval", null);
    echo "cancelled";
    exit;
}

以下是我对成功人士的回应:

echo $payment->getId();
echo "<br>";
var_dump($result);

无论如何,我想回应一下像你已成功支付$ X.00。

但我不知道如何从我所获得的总金额中获取金额。

var_dump($result);

打印出来:

>object(stdClass)#36 (10) {
  ["id"]=>
  string(28) "PAY-XXXXXXXXXX"
  ["intent"]=>
  string(4) "sale"
  ["state"]=>
  string(8) "approved"
  ["cart"]=>
  string(17) "XXXXXXXXXXX"
  ["payer"]=>
  object(stdClass)#35 (2) {
    //payer details was here
  }
  ["transactions"]=>
  array(1) {
    [0]=>
    object(stdClass)#29 (4) {
      ["amount"]=>
      object(stdClass)#27 (3) {
        ["total"]=>
        string(5) "25.00"
        ["currency"]=>
        string(3) "USD"
        ["details"]=>
        object(stdClass)#23 (3) {
          ["subtotal"]=>
          string(5) "25.00"
          ["tax"]=>
          string(4) "0.00"
          ["shipping"]=>
          string(4) "0.00"
        }
      }

  //and goes on....

所以,我真的非常接近总金额。我需要一点帮助。

提前致谢

1 个答案:

答案 0 :(得分:3)

我不确定这是最好的做法,但

$result->transactions[0]->related_resources[0]->sale->amount->total; //echo: 25.00

此代码到目前为止解决了我的问题。