我正在使用Paypal .NET SDK尝试执行付款和退款。我有支付功能,但我无法退款。我一直在查看Paypal的文档和代码示例,例如:
我的退款流程基于Paypal的代码示例(上面的第一个链接),并在我自己的类库中进行了轻微修改:
public class RefundTransaction
{
private readonly string _paypalUniqueTransactionId;
private readonly CurrencyEnum _currency;
private readonly decimal _orderTotal;
/// <summary>
/// Creates a refund with basic key fields.
/// </summary>
/// <param name="paypalUniqueTransactionId"></param>
/// <param name="currency"></param>
/// <param name="orderTotal"></param>
public RefundTransaction(string paypalUniqueTransactionId, CurrencyEnum currency, decimal orderTotal)
{
_paypalUniqueTransactionId = paypalUniqueTransactionId;
_currency = currency;
_orderTotal = orderTotal;
}
/// <summary>
/// Processes and completes a refund on a sale transaction.
/// </summary>
public void RefundWholeSale()
{
#region Set the refund details
Refund processRefund = new Refund
{
amount = new Amount()
{
currency = _currency.ToString(),
total = _orderTotal.ToString("N")
}
};
#endregion
#region Action the refund
Sale refundSale = new Sale()
{
id = _paypalUniqueTransactionId
};
Refund refundResponse = refundSale.Refund(Configuration.GetAPIContext(), processRefund);
#endregion
}
}
我收到的回复是404错误。据我了解,此API调用使用/v1/payments/sale/{sale-id}/refund
端点。我使用的销售ID值是我在用户付款后存储的内容:PAY-XXXXXXXXXXXXXXXXXXXXXXX。
我是否使用了正确的&#34;销售ID&#34;? Paypal帐户是否需要特殊权限以允许通过REST API退款?
对此有任何帮助将不胜感激。
答案 0 :(得分:3)
我终于开始工作了。我认为是销售ID,实际上是不正确的。销售ID隐藏在related_resources
部分的Paypal .NET SDK中。
在代码中创建付款时,我现在正在存储销售ID,您可以按如下方式检索:
// Create a payment using a valid APIContext.
Payment createdPayment = payment.Create(Configuration.GetAPIContext());
string id = createdPayment.id; // This is no the Sale ID you're looking for...
string saleId = createdPayment.transactions[0].related_resources[0].sale.id;
我希望这可以帮助其他人解决同样的问题。
答案 1 :(得分:0)
https://api.sandbox.paypal.com/v1/payments/sale/<Transaction-Id>/refund
application/json
Bearer <Access-Token>
数据:
{
"amount":
{
"total": "2.34",
"currency": "USD"
}
}