paypal php 退款,php – PayPal API:如何通过PayPal获得销售ID和退款?

我在

PHP中使用PayPal API来创建交易,包括信用卡和PayPal本身.此外,我需要能够退还这些交易.我正在使用的代码,大部分直接来自PayPal API示例,适用于信用卡交易,但PayPal交易失败.具体来说,我正在尝试向下钻取Payment对象并提取该Sale的ID.通过信用卡支付的对象包含一个RelatedResources对象,该对象又包含带有ID的Sale对象,但是通过PayPal创建的Payment对象似乎不包含它们.所以,我的问题是,如何从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));

}

相比之下,这是我如何进行PayPal付款.这是一个两步的过程.首先,用户被定向到PayPal的网站,然后,当他们返回我的网站时,付款被处理.

第1部分:

$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;

这是第2部分,当用户通过“成功”消息重定向到我的网站时:

$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);

以下是我退还交易的方式. API中的示例不讨论如何检索销售ID,因此我深入研究了这些对象.通过PayPal进行的付款没有RelatedResources对象,因此失败:

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;

}

有关如何检索销售ID的任何想法?谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值