首页

Paypal支付使用方法

c#

2020-9-5

境外电商Paypal是否方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PayPal.Api;

public partial class pay : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        APIContext apiContext = PaypalConfiguration.GetAPIContext();
        try
        {
            string payerId = Request.Params["PayerID"];
            if (string.IsNullOrEmpty(payerId))
            {
                // 创建 PayerID
                string baseURI = Request.Url.Scheme   "://"   Request.Url.Authority   "?";
                var guid = Convert.ToString((new Random()).Next(10000));
                var createPayment = CreatePayment(apiContext, baseURI   "guid="   guid);

                var links = createPayment.links.GetEnumerator();
                string paypalRedirectUrl = string.Empty;
                while (links.MoveNext())
                {
                    Links link = links.Current;
                    if (link.rel.ToLower().Trim().Equals("approval_url"))
                    {
                        paypalRedirectUrl = link.href;
                    }
                }

                Session["GUID"] = createPayment.id;

                // 重定向
                Response.Redirect(paypalRedirectUrl);
            }
            else
            {
                var guiid = Request.Params["guiid"];
                var executePayment = ExecutePayment(apiContext, payerId, Session["GUID"].ToString());
                if (executePayment.state.ToLower() != "approved")
                {
                    Response.Write("Failure");
                }
            }
        }
        catch (Exception ex)
        {
            PaypalLogger.Log("Error:"   ex.Message);

            Response.Write("Failure");
        }

        Response.Write("Success");
    }

    protected void btnPaypal_Click(object sender, EventArgs e)
    {
        
    }




    private Payment payment;
    private Payment CreatePayment(APIContext apiContext, string redirectUrl)
    {
        var listItems = new ItemList()
        {
            items = new List<Item>()
        };

        // 商品信息
        listItems.items.Add(new Item()
        {
            name = "this is test products",
            currency = "USD",
            price = "19",
            quantity = "1",
            sku = "sku001"
        });

        var payer = new Payer() { payment_method = "paypal" };

        // 配置回调地址
        var redirectUrls = new RedirectUrls()
        {
            return_url = redirectUrl,
            cancel_url = redirectUrl
        };

        // 结算信息
        var details = new Details()
        {
            tax = "1",
            shipping = "2",
            subtotal = "19"
        };

        var amount = new Amount()
        {
            currency = "USD",
            total = (Convert.ToDouble(details.tax)   Convert.ToDouble(details.shipping)   Convert.ToDouble(details.subtotal)).ToString(), // 税点tax   运费shipping   subtotal小结
            details = details
        };

        var transactionList = new List<Transaction>();
        transactionList.Add(new Transaction()
        {
            description = "testing transaction description",
            //invoice_number = "IVO" DateTime.Now.ToString("yyyyMMddHHmmssfff"),
            invoice_number = "IVO"   Convert.ToString((new Random()).Next(10000)),
            amount = amount,
            item_list = listItems
        });

        payment = new Payment()
        { 
            intent = "sale",
            payer = payer,
            transactions = transactionList,
            redirect_urls = redirectUrls
        };

        return payment.Create(apiContext);
    }


    // 执行支付
    private Payment ExecutePayment(APIContext apiContext, String payerId, string paymentId)
    {
        var paymentExecution = new PaymentExecution()
        {
            payer_id = payerId
        };

        payment = new Payment() { id = paymentId };

        return payment.Execute(apiContext, paymentExecution);
    }



}

资源下载此资源下载价格为2D币(VIP免费),请先
资源文件列表
App_Code/PaypalConfiguration.cs , 1165
App_Code/PaypalLogger.cs , 782
Bin/Newtonsoft.Json.dll , 700336
Bin/Newtonsoft.Json.dll.refresh , 130
Bin/Newtonsoft.Json.xml , 707721
Bin/PayPal.dll , 304128
Bin/PayPal.dll.refresh , 94
Bin/PayPal.xml , 401819
index.aspx , 436
index.aspx.cs , 277
packages.config , 212
pay.aspx , 606
pay.aspx.cs , 4276
Web - 副本.config , 2581
没有账号? 忘记密码?

社交账号快速登录