汇款到 Paypal 帐户 ASP.Net 服务器端代码

Send Money to Paypal Account ASP.Net Server Side Code

我很难找到有关如何向另一个 Paypal 帐户汇款的中途下降文档或示例。

我已经安装了 Nuget 包 PaypalSDK 版本 1.0.4。我已阅读 https://developer.paypal.com/home. I have browsed and tried to implement the sample code at https://github.com/paypal/Checkout-NET-SDK.

上的文档

我遇到的问题是我没有在我的沙盒帐户中看到发送或接收的付款通知。我可以在购物车视图中使用 Javascript 按钮成功执行结帐。但最终我想添加从我的 Paypal 企业账户向另一个 Paypal 企业账户汇款的功能,而无需其他 Paypal 企业账户所有者登录我的网站。

收款人是否必须授权我发送的钱,还是应该在我发送后直接存入他们的帐户?

这是我的代码:

namespace MyShoppingCart.Helpers.Paypal
{
    public class CaptureOrderSample
    {
        static string PayPalClientID = Startup.StaticConfig.GetValue<string>("Paypal:ClientID");
        static string PayPalClientSecret = Startup.StaticConfig.GetValue<string>("Paypal:ClientSecret");

        public static HttpClient client()
        {
            // Creating a sandbox environment
            PayPalEnvironment environment = new SandboxEnvironment(PayPalClientID, PayPalClientSecret);

            // Creating a client for the environment
            PayPalHttpClient client = new PayPalHttpClient(environment);
            return client;
        }

        public async static Task<HttpResponse> createOrder(string Email)
        {
            HttpResponse response;
            // Construct a request object and set desired parameters
            // Here, OrdersCreateRequest() creates a POST request to /v2/checkout/orders
            var order = new OrderRequest()
            {
                CheckoutPaymentIntent = "CAPTURE",
                PurchaseUnits = new List<PurchaseUnitRequest>()
                {
                    new PurchaseUnitRequest()
                    {
                        AmountWithBreakdown = new AmountWithBreakdown()
                        {
                            CurrencyCode = "USD",
                            Value = "100.00"
                        },
                            Payee = new Payee
                            {
                              Email = Email // "payee@email.com"
                            }
                    }
                }
                
                //,
                //ApplicationContext = new ApplicationContext()
                //{
                //    ReturnUrl = "https://www.example.com",
                //    CancelUrl = "https://www.example.com"
                //}
            };


            // Call API with your client and get a response for your call
            var request = new OrdersCreateRequest();
            request.Prefer("return=representation");
            request.RequestBody(order);
            response = await client().Execute(request);
            var statusCode = response.StatusCode;
            Order result = response.Result<Order>();
            Debug.WriteLine($"Status: {result.Status}");
            Debug.WriteLine($"Order Id: {result.Id}");
            Debug.WriteLine($"Intent: {result.CheckoutPaymentIntent}");
            Debug.WriteLine("Links:");
            foreach (LinkDescription link in result.Links)
            {
                Debug.WriteLine($"\t{link.Rel}: {link.Href}\tCall Type: { link.Method}");
            }
            return response;
        }
    }
}

当前,这是在订单完成时从我的订单控制器中调用的。这仅用于测试目的。

[Authorize]
public async Task<IActionResult> CompleteOrder()
{
    var items = _shoppingCart.GetShoppingCartItems();


    Models.Order order = await _ordersService.StoreOrderAsync(items);

    PrepareSellerEmail(items, order, "You Have a New Order!");
    PrepareBuyerEmail(items, order, "Thank You for Your Order!");
    await _shoppingCart.ClearShoppingCartAsync(_serviceProvider);
    DeleteCartIDCookie();

    //OrderRequest request = Helpers.CreateOrderSample.BuildRequestBody("USD", "100.00", "sb-r43z1e9186231@business.example.com");
    //var client =  Helpers.Paypal.CaptureOrderSample.client();

    var result = Helpers.Paypal.CaptureOrderSample.createOrder("sb-r43z1e9186231@business.example.com");

    //var response = await PayPalClient.client().execute.(request);

    return View("OrderCompleted");
}

输出结果为:

Status: CREATED
Order Id: 51577255GE4475222
Intent: CAPTURE
Links:
    self: https://api.sandbox.paypal.com/v2/checkout/orders/51577255GE4475222   Call Type: GET
    approve: https://www.sandbox.paypal.com/checkoutnow?token=51577255GE4475222 Call Type: GET
    update: https://api.sandbox.paypal.com/v2/checkout/orders/51577255GE4475222 Call Type: PATCH
    capture: https://api.sandbox.paypal.com/v2/checkout/orders/51577255GE4475222/capture    Call Type: POST

这是我的沙盒帐户的屏幕截图:

我是否应该做其他事情来实际执行传输?

编辑:我想出了如何使用 Paypal 付款 API。

首先我安装了 Nuget 包。它简称为PayoutsSdk。我使用的是 1.1.1 版本。

要执行支付,您需要此 post 上面列出的 client() 方法和下面列出的 CreatePayout() 方法。

public async static Task<HttpResponse> CreatePayout()
{
    var body = new CreatePayoutRequest()
    {
        SenderBatchHeader = new SenderBatchHeader()
        {
            EmailMessage = "Congrats on recieving 1$",
            EmailSubject = "You recieved a payout!!"
        },
        Items = new List<PayoutItem>()  
        {
            new PayoutItem()
            {
                RecipientType="EMAIL",
                Amount=new Currency()
                {
                    CurrencyCode="USD",
                    Value="1",
                },
                Receiver="sb-r43z1e9186231@business.example.com",
            }
        }
    };
    PayoutsPostRequest request = new PayoutsPostRequest();
    request.RequestBody(body);
    var response = await client().Execute(request);
    var result = response.Result<CreatePayoutResponse>();
    Debug.WriteLine($"Status: {result.BatchHeader.BatchStatus}");
    Debug.WriteLine($"Batch Id: {result.BatchHeader.PayoutBatchId}");
    Debug.WriteLine("Links:");
    foreach (PayoutsSdk.Payouts.LinkDescription link in result.Links)
    {
        Debug.WriteLine($"\t{link.Rel}: {link.Href}\tCall Type: {link.Method}");
    }

    return response;
}

当然,我会为电子邮件、金额、货币代码、电子邮件消息和主题的方法添加参数。

现在,我从控制器方法调用此方法,如下所示:var result = Helpers.Paypal.CaptureOrderSample.CreatePayout(); 其中 Helpers.Paypal 是包含名为 CaptureOrderSample 的 class 的文件夹,我可能会重命名。

要从您的帐户向另一个帐户汇款,有几种不同的选择:

  • 使用 Payouts API 或 Payouts Web(电子表格上传)自动发送。对于现场直播,只有在发送付款的真实账户为 approved for payouts.
  • 时才能使用付款
  • 登录到要在 https://www.paypal.com or https://www.sandbox.paypal.com 中汇款的帐户,然后单击“付款并收款”菜单 -> “汇款”。
  • 使用 PayPal Checkout 集成,有或没有订单 API 和 specify a payee that is to receive the money。您必须使用付款(发送)帐户登录才能批准发送,最后必须捕获订单(通过API或客户端actions.order.capture())是导致 PayPal 交易的原因。如果未执行最后的捕获步骤,则不会发送任何款项,订单将仅保持创建或批准状态并最终到期(创建后 72 小时或批准后 3 小时)

在沙盒中,不会发送带有通知的实际电子邮件。相反,developer.paypal.com 仪表板在左侧有一个“通知”选项卡,当然 activity 也可以通过登录帐户在每个沙盒帐户中看到。只有捕获的 activity 可能可见。