当前位置: 首页 > 知识库问答 >
问题:

PayPal API:如何获得销售ID和退款付款通过PayPal?

赏梓
2023-03-14

我正在使用PHP中的PayPal API来创建交易,既可以使用信用卡,也可以通过PayPal本身。此外,我需要能够退款这些交易。我使用的代码(大部分是直接来自PayPal API示例)对于信用卡交易很好,但对于PayPal交易却失败了。具体地说,我正在尝试通过Payment对象向下钻取该销售的ID。通过信用卡支付的对象包含一个RelatedResources对象,该对象又包含带有ID的Sale对象,但通过PayPal支付的对象似乎不包含这些对象。所以,我的问题是,我如何从支付通过贝宝取回销售ID?

    $creditCardToken = new CreditCardToken();
$creditCardToken->setCreditCardId('CARD-2WG5320481993380UKI5FSFI');

// ### FundingInstrument
// A resource representing a Payer's funding instrument.
// For stored credit card payments, set the CreditCardToken
// field on this object.
$fi = new FundingInstrument();
$fi->setCreditCardToken($creditCardToken);

// ### Payer
// A resource representing a Payer that funds a payment
// For stored credit card payments, set payment method
// to 'credit_card'.
$payer = new Payer();
$payer->setPaymentMethod("credit_card")
    ->setFundingInstruments(array($fi));

// ### Amount
// Lets you specify a payment amount.
// You can also specify additional details
// such as shipping, tax.
$amount = new Amount();
$amount->setCurrency("USD")
    ->setTotal('1.00');

// ### Transaction
// A transaction defines the contract of a
// payment - what is the payment for and who
// is fulfilling it. 
$transaction = new Transaction();
$transaction->setAmount($amount)
    ->setDescription("Payment description");

// ### Payment
// A Payment Resource; create one using
// the above types and intent set to 'sale'
$payment = new Payment();
$payment->setIntent("sale")
    ->setPayer($payer)
    ->setTransactions(array($transaction));

// ###Create Payment
// Create a payment by calling the 'create' method
// passing it a valid apiContext.
// (See bootstrap.php for more on `ApiContext`)
// The return object contains the state.
try {
    $payment->create($apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
    error_log($ex->getMessage());
    error_log(print_r($ex->getData(), true));
}
$payer = new Payer();
$payer->setPaymentMethod("paypal");

$amount = new Amount();
$amount->setCurrency("USD")
    ->setTotal($userInfo['amount']);

$transaction = new Transaction();
$transaction->setAmount($amount)
    ->setDescription("Payment description");

// ### Redirect urls
// Set the urls that the buyer must be redirected to after 
// payment approval/ cancellation.
$baseUrl = 'http://example.com';
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/?success=true")
    ->setCancelUrl("$baseUrl/?success=false");

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

try {
    $payment->create($apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
    error_log($ex->getMessage());
    error_log(print_r($ex->getData(), true));
    return;
}

// ### Get redirect url
// The API response provides the url that you must redirect
// the buyer to. Retrieve the url from the $payment->getLinks()
// method
foreach($payment->getLinks() as $link) {
    if($link->getRel() == 'approval_url') {
        $redirectUrl = $link->getHref();
        break;
    }
}

// ### Redirect buyer to PayPal website
// Save payment id so that you can 'complete' the payment
// once the buyer approves the payment and is redirected
// bacl to your website.
//
// It is not really a great idea to store the payment id
// in the session. In a real world app, you may want to 
// store the payment id in a database.
$_SESSION['paymentId'] = $payment->getId();

if(isset($redirectUrl)) {
    $response->redirectUrl = $redirectUrl;
}
return $response;
$payment = Payment::get($lineitem->paypal_payment_ID, $apiContext);

// 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->setPayer_id($_GET['PayerID']);

//Execute the payment
// (See bootstrap.php for more on `ApiContext`)
$payment->execute($execution, $apiContext);
    try {
    $payment = Payment::get('PAY-8TB50937RV8840649KI6N33Y', $apiContext);
    $transactions = $payment->getTransactions();
    $resources = $transactions[0]->getRelatedResources();//This DOESN'T work for PayPal transactions.

    $sale = $resources[0]->getSale();
    $saleID = $sale->getId();

    // ### Refund amount
    // Includes both the refunded amount (to Payer) 
    // and refunded fee (to Payee). Use the $amt->details
    // field to mention fees refund details.
    $amt = new Amount();
    $amt->setCurrency('USD')
        ->setTotal($lineitem->cost);

    // ### Refund object
    $refund = new Refund();
    $refund->setAmount($amt);

    // ###Sale
    // A sale transaction.
    // Create a Sale object with the
    // given sale transaction id.
    $sale = new Sale();
    $sale->setId($saleID);
    try {   
        // Refund the sale
        // (See bootstrap.php for more on `ApiContext`)
        $sale->refund($refund, $apiContext);
    } catch (PayPal\Exception\PPConnectionException $ex) {
        error_log($ex->getMessage());
        error_log(print_r($ex->getData(), true));
        return;
    }
} catch (PayPal\Exception\PPConnectionException $ex) {
    error_log($ex->getMessage());
    error_log(print_r($ex->getData(), true));
    return;
}

共有1个答案

华坚成
2023-03-14

我已经成功退款了一笔交易,没有找到一个容易的方法。所以,我用了另一种方法。请尝试以下代码:

$apiContext = new ApiContext(new OAuthTokenCredential(
            "<CLIENT_ID>", "<CLIENT_SECRET>")
    );
    $payments = Payment::get("PAY-44674747470TKNYKRLI", $apiContext);
    $payments->getTransactions();
    $obj = $payments->toJSON();//I wanted to look into the object
    $paypal_obj = json_decode($obj);//I wanted to look into the object
    $transaction_id = $paypal_obj->transactions[0]->related_resources[0]->sale->id;
    $this->refund($transaction_id);//Call your custom refund method

干杯!

 类似资料:
  • {“ID”:“Pay-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,“Create_Time”:“2016-06-20T11:45:28Z”,“Update_Time”:“2016-06-20T11:45:28Z”,“State”:“Created”,“Intent”:“Sale”,“Payment_Method”:“Credit_

  • 在支付完成后,从支付宝服务器请求我们的异步通知地址,这个才应该是做支付成功处理的时机。 // SDK实例化,传入公共配置 $sdk = new \Yurun\PaySDK\AlipayCrossBorder\SDK($params); class PayNotify extends \Yurun\PaySDK\AlipayCrossBorder\Online\Notify\Pay { /

  • 我正在使用PayPal的REST SDK for Java:https://github.com/paypal/paypal-java-sdk 首先,我使用方法获取支付的详细信息。 然后,要执行退款,我需要销售ID。因此,我通过以下调用从前一步中检索的对象中检索此内容: 对于这两个列表,是否有方法确保列表中的元素是与您相关的元素?在什么情况下,返回的列表中有多个元素? 谢谢

  • 退款后会发送通知。 官方文档: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16&index=9 // SDK实例化,传入公共配置 $sdk = new \Yurun\PaySDK\Weixin\SDK($params); class RefundNotify extends \Yurun\PaySDK\Weixin\No

  • 我正在尝试将PayPal REST API集成到我的Symfony2 web应用程序中,但我发现很难理解完整的工作流程到底是什么样子的: PayPal文档描述了以下接受付款的步骤。可以使用PayPal Playground模拟以下步骤: 获取访问令牌 通过查询API创建对象 将用户重定向到响应中接收的 用户在PayPal页面上批准支付后,将使用对象中定义的成功链接将其重定向回我的页面。使用接收到的

  • 说明 用于在线支付单退款操作,支持微信、支付宝支付 微信支付特约商户账号退款需要额外签“退款授权证明”,详细请咨询。 请求地址 http://api.dc78.cn/Api/cash_refund 请求方式 GET 请求参数 参数 参数名称 必填 描述 范例 payid 支付流水号 522888 amt 退款金额 不传默认为全部退款 28 支付URL例: http://wx.dc78.cn/Api