sheibeck
sheibeck

Reputation: 41

PayPal Rest API - Execute Payment with updated transaction info

I'm using C# and PayPal Rest API to Get an approved payment and execute it. But, I need to update the transactions associated with approved payment. The PayPal documentation reads:

Use this call to execute (complete) a PayPal payment that has been approved by the payer. You can optionally update transaction information when executing the payment by passing in one or more transactions.

Here's my code

//Update the payment details in case totals changed because of a new address/zipcode
Details amountDetails = new Details();
amountDetails.subtotal = ValidationHelper.GetString(prices[Order.CartPricesEnum.Subtotal], "0");
amountDetails.tax = ValidationHelper.GetString(prices[Order.CartPricesEnum.Tax], "0");
amountDetails.shipping = ValidationHelper.GetString(prices[Order.CartPricesEnum.Shipping], "0");

Amount amount = new Amount();
amount.total = ValidationHelper.GetString(prices[Order.CartPricesEnum.Total], "0");
amount.currency = "USD";
amount.details = amountDetails;

//update the transaction to make sure we have accounted for any updated prices
Transactions trn = new Transactions();
trn.amount = amount;

List<Transactions> trns = new List<Transactions>();                    
trns.Add(trn);

//Create a payment execution object
PaymentExecution paymentExecution = new PaymentExecution();
paymentExecution.payer_id = payPalPayerID;
paymentExecution.transactions = trns;                    

//Execute (complete) the payment
Payment newPayment = payment.Execute(accessToken, paymentExecution);

The problem is that when when this runs I get the following error:

{"name":"VALIDATION_ERROR","details":[{"field":"transactions[0].total","issue":"Required field missing"},{"field":"transactions[0].currency","issue":"Required field missing"},{"field":"transactions[0].amount","issue":"This field name is not defined for this resource type"}],"message":"Invalid request - see details","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#VALIDATION_ERROR","debug_id":"bcba38f3c56d7"}

This is telling me that I'm missing .total and .currency, and that the .amount field is not defined. However, you can plainly see that I am setting the total and currency, and the amount field is the ONLY field that you can set on the transactions object according to the PayPal API documentation:

transactions
array of transaction objects
Transactional details if updating a payment. Note that this instance of the transactions object accepts only the amount object.

So, my question is: How can I take an approved payment, update the price on the transaction of the payment and then execute that payment?

Upvotes: 4

Views: 5032

Answers (2)

Nick
Nick

Reputation: 1

Looks like that the paymentExecution transactions object only accepts an amount object and not a transaction object.

https://developer.paypal.com/docs/api/#execute-an-approved-paypal-payment

Upvotes: 0

Alex G
Alex G

Reputation: 11

I am not an expert at this but I just implemented a similar payment transaction and I think it has to be done in two steps: create the payment THEN execute the payment as the buyer has to log in into his PayPal account and confirm the payment in between. This way it worked for me.

So what you need is something like this (sorry my code is VB.NET):

'Create payment
Dim createdPayment As Payment = payment.Create(apiContext)

And then on postback return from PayPal you can execute the payment:

'Execute payment
Dim paymentExecution As New PaymentExecution()
Dim executedPayment As Payment = payment.Execute(apiContext, paymentExecution)

Here is a full example taken from the samples provided with the API:

// #Create Payment Using PayPal Sample
// This sample code demonstrates how you can process a 
// PayPal Account based Payment.
// API used: /v1/payments/payment
using System;
using System.Web;
using PayPal;
using PayPal.Api.Payments;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace RestApiSample
{
    public partial class PaymentWithPayPal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpContext CurrContext = HttpContext.Current;
            Payment pymnt = null;

            // ### Api Context
            // Pass in a `APIContext` object to authenticate 
            // the call and to send a unique request id 
            // (that ensures idempotency). The SDK generates
            // a request id if you do not pass one explicitly. 
             // See [Configuration.cs](/Source/Configuration.html) to know more about APIContext..
            APIContext apiContext = Configuration.GetAPIContext();

            // ## ExecutePayment
            if (Request.Params["PayerID"] != null)
            {
                pymnt = new Payment();
                if (Request.Params["guid"] != null)
                {
                    pymnt.id = (string)Session[Request.Params["guid"]];

                }
                try
                {
                    PaymentExecution pymntExecution = new PaymentExecution();
                    pymntExecution.payer_id = Request.Params["PayerID"];

                    Payment executedPayment = pymnt.Execute(apiContext, pymntExecution);
                    CurrContext.Items.Add("ResponseJson", JObject.Parse(executedPayment.ConvertToJson()).ToString(Formatting.Indented));
                }
                catch (PayPal.Exception.PayPalException ex)
                {
                    CurrContext.Items.Add("Error", ex.Message);
                }
            }

            // ## Creating Payment
            else
            {
                // ###Items
                // Items within a transaction.
                Item item = new Item();
                item.name = "Item Name";
                item.currency = "USD";
                item.price = "15";
                item.quantity = "5";
                item.sku = "sku";

                List<Item> itms = new List<Item>();
                itms.Add(item);
                ItemList itemList = new ItemList();
                itemList.items = itms;

                // ###Payer
                // A resource representing a Payer that funds a payment
                // Payment Method
                // as `paypal`
                Payer payr = new Payer();
                payr.payment_method = "paypal";
                Random rndm = new Random();
                var guid = Convert.ToString(rndm.Next(100000));

                string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/PaymentWithPayPal.aspx?";

                // # Redirect URLS
                RedirectUrls redirUrls = new RedirectUrls();
                redirUrls.cancel_url = baseURI + "guid=" + guid;
                redirUrls.return_url = baseURI + "guid=" + guid;

                // ###Details
                // Let's you specify details of a payment amount.
                Details details = new Details();
                details.tax = "15";
                details.shipping = "10";
                details.subtotal = "75";

                // ###Amount
                // Let's you specify a payment amount.
                Amount amnt = new Amount();
                amnt.currency = "USD";
                // Total must be equal to sum of shipping, tax and subtotal.
                amnt.total = "100";
                amnt.details = details;

                // ###Transaction
                // A transaction defines the contract of a
                // payment - what is the payment for and who
                // is fulfilling it. 
                List<Transaction> transactionList = new List<Transaction>();
                Transaction tran = new Transaction();
                tran.description = "Transaction description.";
                tran.amount = amnt;
                tran.item_list = itemList;
                // The Payment creation API requires a list of
                // Transaction; add the created `Transaction`
                // to a List
                transactionList.Add(tran);

                // ###Payment
                // A Payment Resource; create one using
                // the above types and intent as `sale` or `authorize`
                pymnt = new Payment();
                pymnt.intent = "sale";
                pymnt.payer = payr;
                pymnt.transactions = transactionList;
                pymnt.redirect_urls = redirUrls;

                try
                {
                    // Create a payment using a valid APIContext
                    Payment createdPayment = pymnt.Create(apiContext);

                    CurrContext.Items.Add("ResponseJson", JObject.Parse(createdPayment.ConvertToJson()).ToString(Formatting.Indented));

                    var links = createdPayment.links.GetEnumerator();

                    while (links.MoveNext())
                    {
                        Links lnk = links.Current;
                        if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                        {
                            CurrContext.Items.Add("RedirectURL", lnk.href);
                        }
                    }
                    Session.Add(guid, createdPayment.id);
                }
                catch (PayPal.Exception.PayPalException ex)
                {
                    CurrContext.Items.Add("Error", ex.Message);
                }
            }
            CurrContext.Items.Add("RequestJson", JObject.Parse(pymnt.ConvertToJson()).ToString(Formatting.Indented));

            Server.Transfer("~/Response.aspx");

        }
    }
}

Upvotes: 1

Related Questions