- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一个集成到 PayPal 的 REST API 中的 PHP 应用程序。我已正确处理事务并将事务 ID 保存到 MySQL 数据库中。我现在正尝试退款,但无法让它停止给出“传入的 JSON 请求未映射到 API 请求”错误。有没有人对如何让它工作有任何建议?
我在代码中留下了我的一些尝试,并对它们的结果进行了评论。我对让这个工作的方向感到有点迷茫。任何帮助将不胜感激。
我一直在关注位于此处的文档: https://developer.paypal.com/docs/integration/direct/payments/refund-payment/
我可能很难将该请求适应 PHP。
我找到了这段代码: https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/payments/RefundCapture.php
但它“需要”一棵文件树,对于一个简单的退款请求来说似乎有点过分。
<?php
require_once(dirname(__FILE__) . '/paypalSDK/autoload.php'); // require paypal files
require_once(dirname(__FILE__) . '/paypalSDK/paypal/rest-api-sdk-php/sample/common.php'); // require paypal files
// call required stuff
use PayPal\Api\Address;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\ExecutePayment;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\PaymentCard;
use PayPal\Api\PaymentExecution;
use PayPal\Api\Transaction;
use PayPal\Api\Capture;
use PayPal\Api\Refund;
use PayPal\Api\RefundRequest;
use PayPal\Api\Sale;
$orderID = $_GET['order_id']; // order ID we wish to refund
// pull the details from the database so we have the transaction refund ID
$refQ = mysql_query("SELECT * FROM orders WHERE orders_ID = $orderID", $db);
$refC = mysql_num_rows($refQ);
if(!empty($refC)){
$refR = mysql_fetch_assoc($refQ);
$refNumber = $refR['orders_transaction_ref']; // paypal transaction ID
$FinalTotal = $refR['orders_order_total']; // order total
// paypal credentials
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'abc',
'xyz'
)
);
$apiContext->setConfig(array('mode' => 'live',)); // live use, not sandbox
//$amount = new Amount(); // commenting these out seemingly has no effect
//$amount->setCurrency("USD") // commenting these out seemingly has no effect
// ->setTotal($FinalTotal); // commenting these out seemingly has no effect
$refund = new Refund(); // if I comment these 2 lines out, I get "refund cannot be null"
$refund->setAmount($FinalTotal); // if I comment these 2 lines out, I get "refund cannot be null"
$sale = new Capture(); // if I comment these 2 lines out, I get "Call to a member function refund() on a non-object"
$sale->setId($refNumber); // if I comment these 2 lines out, I get "Call to a member function refund() on a non-object"
$refundRequest = new RefundRequest(); // commenting these out seemingly has no effect
$refundRequest->setAmount($FinalTotal); // commenting these out seemingly has no effect
//$captureRefund = $sale->refundCapturedPayment($refundRequest, $apiContext); // failure, error 400
try {
$sale->refund($refund, $apiContext);
//$sale->refundCapturedPayment($refund, $apiContext); // failure, error 400
} catch (Exception $ex) {
echo '<pre style="font-size:16px;">';
var_dump($ex);
echo '</pre>';
exit();
die;
}
}
2017 年 3 月 8 日下午 12:23 更新
我似乎取得了“一些”进步,尽管我不严格地使用这个词。状态返回为“已完成”,但退款实际上并未发生。它也不会像示例所示那样返回父支付 ID。对此有什么想法吗?我仍在努力让它发挥作用。
这是我目前从 PayPal 获得的结果:
object(PayPal\Api\Refund)#3 (1) {
["_propMap":"PayPal\Common\PayPalModel":private]=>
array(5) {
["id"]=>
string(17) "123456789123456789"
["create_time"]=>
string(20) "2017-03-06T20:56:32Z"
["state"]=>
string(9) "completed"
["amount"]=>
object(PayPal\Api\Amount)#9 (1) {
["_propMap":"PayPal\Common\PayPalModel":private]=>
array(3) {
["total"]=>
string(4) "0.02"
["currency"]=>
string(3) "USD"
["details"]=>
object(PayPal\Api\Details)#13 (1) {
["_propMap":"PayPal\Common\PayPalModel":private]=>
array(1) {
["subtotal"]=>
string(4) "0.02"
}
}
}
}
["links"]=>
array(1) {
[0]=>
object(PayPal\Api\Links)#17 (1) {
["_propMap":"PayPal\Common\PayPalModel":private]=>
array(3) {
["href"]=>
string(59) "https://api.paypal.com/v1/payments/refund/123456789123456789"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
}
}
}
下面是目前返回上述响应的代码...
require_once(dirname(__FILE__) . '/paypalSDK/autoload.php'); // require paypal files
require_once(dirname(__FILE__) . '/paypalSDK/paypal/rest-api-sdk-php/sample/bootstrap.php'); // require paypal files
require_once(dirname(__FILE__) . '/paypalSDK/paypal/rest-api-sdk-php/sample/common.php'); // require paypal files
// call required stuff
use PayPal\Api\Address;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\ExecutePayment;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\PaymentCard;
use PayPal\Api\PaymentExecution;
use PayPal\Api\Transaction;
use PayPal\Api\Capture;
use PayPal\Api\Refund;
use PayPal\Api\RefundRequest;
use PayPal\Api\Sale;
$orderID = $_GET['order_id']; // internal order ID we wish to refund
// pull the details from database so we have the PayPal transction ID
$refQ = mysql_query("SELECT * FROM orders WHERE orders_ID = $orderID", $db);
$refC = mysql_num_rows($refQ);
if(!empty($refC)){
$refR = mysql_fetch_assoc($refQ);
$refNumber = $refR['orders_transaction_ref']; // PayPal transaction ID
// attempted 'PAY-' ID, results in "Requested resource ID was not found."
// $refNumber = $refR['orders_payment_ref']; // ex. PAY-123456789012345678901234
$FinalTotal = $refR['orders_order_total']; // order total
// paypal credentials
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'abc',
'xyz'
)
);
$apiContext->setConfig(array('mode' => 'live',)); // live use, not sandbox
//$amount = new Amount(); // commenting these out seemingly has no effect
//$amount->setCurrency("USD") // commenting these out seemingly has no effect
// ->setTotal($FinalTotal); // commenting these out seemingly has no effect
/*
// attempted code
// ### Refund object
$refund = new Refund();
//$refund = new RefundRequest();
$refund->setAmount($FinalTotal);
*/
$refund = new Refund();
$refund->setId($refNumber);
$refund->setSaleId($refNumber);
$refund->setAmount($FinalTotal);
$refund->setReason("Testing refund code");
//$refund->get($refNumber, $apiContext);
// Attempted code, didn't work
// https://stackoverflow.com/questions/18927591/paypal-api-how-to-get-sale-id-and-refund-payment-made-via-paypal
// Fatal error: Uncaught exception 'InvalidArgumentException' with message 'paymentId cannot be null' in /home/cartridgeworkspl/public_html/cfx_controllers/paypalSDK/paypal/rest-api-sdk-php/lib/PayPal/Validation/ArgumentValidator.php:25 Stack trace: #0
// $payments = Payment::get($refNumber, $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
try {
//var_dump($refund);
$refundResponse = Refund::get($refNumber, $apiContext);
echo '<pre>';
var_dump($refundResponse);
echo '</pre>';
echo $refundResponse->getState();
echo $refundResponse->getReasonCode();
} 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);
exit(1);
}
}
如有任何帮助,我们将不胜感激。
最佳答案
文档:https://developer.paypal.com/docs/api/quickstart/refund-payment/#set-the-refund-object
要进行付款和退款,请使用:paypal/rest-api-sdk-php
首先,您付款时,必须是“sale”。
[...]
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirect_urls)
->setTransactions(array($transaction));
[...]
PayPal 将返回带有“paymentId”值的付款,您需要保存它。
退款示例。
//Previosly saved payments ID.
$paymentId = 'PI_Fv54vfd45';
//Create API connection
$apiContext = new ApiContext(new OAuthTokenCredential($paypalConf['client_id'], $paypalConf['secret']));
$apiContext->setConfig($paypalConf['settings']);
//Get sale from payment
$paypalPayment = new Payment();
$paymentInfo = $paypalPayment->get($paymentId, $apiContext);
$transactions = $paymentInfo->getTransactions();
if(empty($transactions[0])){
return false;
}
$relatedResources = $transactions[0]->getRelatedResources();
if(empty($relatedResources[0])){
return false;
}
$sale = $relatedResources[0]->getSale();
$refund = new Refund();
$amt = (new Amount())->setTotal(100)->setCurrency('EUR');
$refund->setAmount($amt);
$refund->setReason('Sale refund');
$refundedSale = $sale->refund($refund, $apiContext);
return ($refundedSale->getState() === 'completed');
关于php - 使用 PHP PayPal REST API 退款?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42588772/
我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使
我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.
我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几
下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name)
我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见
我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入
我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观
我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_
我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php
我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET
首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id
我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va
我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、
我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){
我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l
我是一名优秀的程序员,十分优秀!