Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用paypal sdk成功购买后,如何向客户回传付款金额?_Php_Paypal_Paypal Sandbox_Paypal Rest Sdk - Fatal编程技术网

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

Php 使用paypal sdk成功购买后,如何向客户回传付款金额?,php,paypal,paypal-sandbox,paypal-rest-sdk,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

我可以使用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;
}
打印此文件:

>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....
所以,我真的很接近得到总数。我需要一点帮助


提前谢谢

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

$result->transactions[0]->related_resources[0]->sale->amount->total; //echo: 25.00
到目前为止,这个代码解决了我的问题

>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....
$result->transactions[0]->related_resources[0]->sale->amount->total; //echo: 25.00