PayPal .net REST API - 带有额外一次性付款的重复计费协议

Posted

技术标签:

【中文标题】PayPal .net REST API - 带有额外一次性付款的重复计费协议【英文标题】:PayPal .net REST API - Recurring billing agreements with additional one time payments 【发布时间】:2015-06-03 07:11:07 【问题描述】:

我是通过 .Net 接触 PayPal 的 REST API 的新手,但我遇到了以下问题: 我可以设置定期计费协议,一切正常。 我可以设置一个简单的付款方式,效果很好。

但是如何一步将两者结合起来呢? 一旦您将付款添加到结算协议(或相反),您就会得到一个无效的令牌。

理想情况下,我们只想将用户定向到贝宝一次进行身份验证。设置新的计费协议并同时支付一笔管理员费用。 有任何想法吗?

如果有帮助,这是我的代码:

public string CreateOrder(MarketingFramework mf, HttpContext httpContext, User user, int packageid)
    

        Group topgroup = user.RootGroup;

        //get the package
        var package = mf.Core.GetById<Package>(packageid);
        var admincharge =
            mf.Core.GetAll<SystemSettings>()
                .FirstOrDefault(s => s.Name == "Admin Charge" && s.Type == user.ProfileType)
                .Value;
        var cancelUrl = ConfigurationManager.AppSettings["Domain"] + "/Basket/Cancel.aspx";
        var confirmationUrl = ConfigurationManager.AppSettings["Domain"] + "/Basket/Confirm.aspx";


        //set the new purchase history date, if you had a package before and its not free, take that date and move it on a month
        //the date is set on the group everytime the IPN gateway makes a payment, therefore should always only be one month behind
        var newdate = DateTime.Now;
        if (topgroup.CurrentPackage.Package.Cost != 0) newdate = topgroup.CurrentPackage.DateAdded.AddMonths(1);

        //Authenticate with paypal
        var apiContext = Configuration.GetAPIContext();

        //cancel any current recurring packages
        if (topgroup.CurrentPackage.PaymentStatus == "Active Profile")
        
            var plan = Plan.Get(apiContext, topgroup.CurrentPackage.ProfileId);
            plan.Delete(apiContext);
        

        //if the package costs money eg: you not coming from a free account, an admin charge will apply
        //there is different prices for corporate or public in the settings table
        if (topgroup.CurrentPackage.Package.Cost != 0)
        
            //calc the upgrade cost
            var totalamount = package.Cost - user.RootGroup.CurrentPackage.Package.Cost;
            if (totalamount < 0) totalamount = 0;

            var guid = Convert.ToString((new Random()).Next(100000));

            var itemList = new ItemList()
            
                items = new List<Item>() 
                
                    new Item()
                    
                        name = "Administration charge",
                        currency = "GBP",
                        price = admincharge,
                        quantity = "1",
                        description = "Administration charge for upgrading package",
                        sku = package.Id.ToString()
                                    ,
                    new Item()
                    
                        name = "Upgrade charge",
                        description = "Difference in price when upgrading to a new package",
                        currency = "GBP",
                        price = totalamount.ToString(),
                        quantity = "1",
                        sku = package.Id.ToString()
                    
                
            ;



            var payer = new Payer()  payment_method = "paypal" ;
            var redirUrls = new RedirectUrls()
            
                cancel_url = cancelUrl,
                return_url = confirmationUrl
            ;

            var details = new Details()
            
                tax = "0",
                shipping = "0",
                subtotal = (Convert.ToDecimal(totalamount) + Convert.ToDecimal(admincharge)).ToString()
            ;

            var amount = new Amount()
            
                currency = "GBP",
                total = details.subtotal,
                details = details
            ;

            var transactionList = new List<Transaction>();

            transactionList.Add(new Transaction()
            
                description = "Proofanything transaction",
                invoice_number = "1", //TODO need to setup invoice numbering
                amount = amount,
                item_list = itemList
            );

            var payment = new Payment()
            
                intent = "sale",
                payer = payer,
                transactions = transactionList,
                redirect_urls = redirUrls
            ;

            ////setup the recurring payment
            var recurring = new Plan
            
                name = "Payment plan",
                description = "Proofanything monthly package plan",
                type = "INFINITE",
                // Define the merchant preferences.
                // More Information: https://developer.paypal.com/webapps/developer/docs/api/#merchantpreferences-object
                merchant_preferences = new MerchantPreferences()
                
                    setup_fee = GetCurrency(admincharge),
                    return_url = confirmationUrl,
                    cancel_url = cancelUrl,
                    auto_bill_amount = "YES",
                    initial_fail_amount_action = "CONTINUE",
                    max_fail_attempts = "0"
                ,
                payment_definitions = new List<PaymentDefinition>
                
                    new PaymentDefinition()
                    
                        name = package.Name,
                        type = "REGULAR",
                        frequency = "MONTH",
                        frequency_interval = "1",
                        amount = GetCurrency(package.Cost.ToString()),
                        cycles = "0"
                    ,

                
            ;


            ////create the payments and plan
            var createdPlan = recurring.Create(apiContext);
            var createdPayment = payment.Create(apiContext);


            //redirect to paypal for approval
            return createdPayment.GetApprovalUrl();

【问题讨论】:

【参考方案1】:

听起来您希望merchant_preferences 中的setup_fee 可用,可以直接在billing plan 上设置,也可以通过override_merchant_preferences 在billing agreement 上设置。 setup_fee 是在执行计费协议时处理的一次性付款。

抱歉,我无法为您提供任何 .net 细节方面的帮助。我所有的 PayPal 工作都是通过 php 完成的。

【讨论】:

这可能是我一直在寻找的答案!一个简单的问题:如果我使用设置费并将定期开始日期设置为下个月,现在收取的费用和下个月的定期账单吗? @MarkGeary setup_fee 会立即被采用,无论何时设置重复开始日期。请参阅here 了解更多信息。 确实,正如@JasonZ 所说。正如他的链接所示,setup_fee 对协议的执行甚至是原子的。在merchant_preferences 上有一个选项initial_fail_amount_action 采用CONTINUE(默认)或CANCEL。不过,我不确定这是否适用于 setup_fee 或第一次定期付款,或两者兼而有之。 非常感谢大家,很好的答案!

以上是关于PayPal .net REST API - 带有额外一次性付款的重复计费协议的主要内容,如果未能解决你的问题,请参考以下文章

带有 Laravel 4 的 PayPal rest-api-sdk-php

用于银行交易中预期目的的带有自定义发票编号的 PayPal Rest API 付款

带有 PHP SDK 的 Paypal REST API - 如何获取交易号?

带有 RestSharp 的 Paypal Rest Api 在 xamarin android 中不起作用

在 paypal REST API 中发送带有付款人信息的用户电子邮件地址

PayPal 401 未经授权在实时制作中使用 .NET REST API for Express checkout API 进行信用卡支付