asp.net-mvc - 如何使用 c# asp.net 在贝宝中向自定义收款人发送付款?
问题描述
我正在构建一个电子商务 c# mvc Asp.net 应用程序,我希望付款人可以在他/她购买商品的不同帐户上付款。
我想将付款从付款人发送到自定义收款人我使用了 paypal.api sdk 我在其中添加了收款人,但它给出了错误,即“异常详细信息:{“name”:“MALFORMED_REQUEST”,“message”:“传入的 JSON 请求没有映射到 API"
//// Pyapal.api sdk use
public PaypalHelper CreatePayment(CreatePaymentHelper helper)
{
var apiContext = GetApiContext();
string ProfileId = GetWebProfile(apiContext, helper.ExperienceProfileName).id;
//ItemList itemList = new ItemList();
//foreach (var i in helper.Items)
//{
// Item item = new Item()
// {
// description = i.Description,
// currency = i.Currency,
// quantity = i.Quantity.ToString(),
// price = i.Price.ToString(), /// decimal strng
// };
// itemList.items.Add(item);
//}
var payment = new Payment()
{
experience_profile_id = ProfileId,
intent = "order",
payer = new Payer()
{
payment_method = "paypal"
},
transactions = new List<Transaction>()
{
new Transaction()
{
description = helper.TransactionDescription,
amount = new Amount()
{
currency = helper.CurrencyName,
total = helper.TransactionAmount.ToString()/// decimal and must be string
},
// item_list = itemList,
item_list = new ItemList()
{
items = helper.Items.Select(x=> new Item()
{
description = x.Description,
currency = x.Currency,
quantity = x.Quantity.ToString(),
price = x.Price.ToString()
}).ToList()
},
},
},
redirect_urls = new RedirectUrls()
{
return_url = helper.ReturnUrl.ToString(),
cancel_url = helper.CancelUrl.ToString()
//Url.Action("action", "controller", "routeVaslues", Request.Url.Scheme);
},
payee = new Payee { email = "RecieverEmail"}
};
/// send payment to paypal
var createdPayment = payment.Create(apiContext);
///get approval url where we need to send our user
var ApprovalUrl = createdPayment.links.FirstOrDefault(x => x.rel.Equals("approval_url", StringComparison.OrdinalIgnoreCase));
return new PaypalHelper
{
CreatedPaymentId = createdPayment.id,
RedirectUrl = ApprovalUrl.href
};
}
然后我尝试了链式方法,但无法得到它!请求以某种方式成功,但无法获得付款,也无法授权付款人!
public void AdaptiveChainedMethod()
{
ReceiverList receivers = new ReceiverList();
Receiver receiver = new Receiver();
receiver.email = "ReCIEVEREMAIL";
receiver.amount = Convert.ToDecimal(22.00);
receivers.receiver.Add(receiver);
ReceiverList receiverList = new ReceiverList(receivers.receiver);
PayRequest payRequest = new PayRequest();
payRequest.actionType = "CREATE";
payRequest.receiverList = receiverList;
payRequest.currencyCode = "USD";
payRequest.cancelUrl = "http://localhost:44382/cont/Act";
payRequest.returnUrl= "http://localhost:44382/cont/Act";
payRequest.requestEnvelope = new RequestEnvelope("en_US");
APIContext apiContext = GetApiContext();
Dictionary<string, string> config = new Dictionary<string, string>();
config.Add("mode", "sandbox");
config.Add("clientId", "id");
config.Add("clientSecret", "seceret");
config.Add("account1.apiUsername", "username");
config.Add("account1.apiPassword", "pass");
config.Add("account1.apiSignature", "signature");
config.Add("account1.applicationId", "APP-80W284485P519543T"); // static account id
AdaptivePaymentsService service = new AdaptivePaymentsService(config);
PayResponse response = service.Pay(payRequest);
}
然后我尝试了 PayPalCheckoutSdk.Core PayPalCheckoutSdk.Orders sdk,但是每次我点击它时它的 API 请求都会卡住并且永远不会回答!
public async static Task<string> test()
{
OrderRequest orderRequest = new OrderRequest()
{
CheckoutPaymentIntent = "CAPTURE",
ApplicationContext = new ApplicationContext
{
ReturnUrl = "http://localhost:44382/cont/act",
CancelUrl = "http://localhost:44382/cont/act"
},
PurchaseUnits = new List<PurchaseUnitRequest>
{
new PurchaseUnitRequest {
AmountWithBreakdown = new AmountWithBreakdown
{
CurrencyCode = "USD",
Value = "220.00"
},
Payee = new Payee
{
Email = "RecieverEmail"
}
}
}
};
var request = new OrdersCreateRequest();
// request.Prefer("return=representation");
request.RequestBody(orderRequest);
HttpResponse response = await client().Execute(request);
var statusCode = response.StatusCode;
Order result = response.Result<Order>();
return "string";
}
public static PayPalHttpClient client()
{
string clientId = "clientid";
string clientSecret = "secret";
// Creating a sandbox environment
PayPalEnvironment environment = new SandboxEnvironment(clientId, clientSecret);
// Creating a client for the environment
PayPalHttpClient client = new PayPalHttpClient(environment);
return client;
}
解决方案
推荐阅读
- unit-testing - 在单元测试控制器 NestJs 中上传模拟文件
- ios - 如何将嵌套节点从 Firebase 实时数据库检索到 iOS 应用程序
- c++ - 为什么我的程序在使用 -fsanitize=leak 时不会停止?
- sql - 等待 x 秒后重试 SQL UPDATE 查询
- java - java - 如何在Java Swing中为非空布局组件设置绝对位置?
- spring - Spring Data JPA,如何将一个属性映射到两列?
- yaml - OpenAPI YAML 描述中的 |、> 和 >- 有什么区别?
- elasticsearch - 在 SSR 应用程序中使用 `@elastic/app-search-javascript` 会引发 Headers not defined 错误
- ebean - 具有单向 OneToMany 关系的 Ebean 在插入期间导致重复列异常
- javascript - 我的代码中是否有错字导致我的函数无法填写表单?