How can I authorize and capture payments for orders using PayPal’s ExpressCheckout API?
When using PayPal’s ExpressCheckout functionality, the Order payment action represents an agreement to pay one or more authorized amounts up to the specified total over a maximum of 29 days. This functionality is available in the ExpressCheckout component through a few key properties and configuration settings.
To set up an order, simply set the PaymentAction property to Order when calling SetCheckout. For example:
expresscheckout1.OrderTotal = "45.00"; expresscheckout1.ReturnURL = returnURL; expresscheckout1.CancelURL = cancelURL; expresscheckout1.PaymentAction = ExpresscheckoutPaymentActions.aOrder; expresscheckout1.SetCheckout();
Then, use the returned ResponseToken and redirect the user to the PayPal as with any other ExpressCheckout payment. When the customer approves the authorization, the user is redirected back to the return URL. Here, the component’s GetCheckoutDetails method can be used to obtain the order details. These details include the PayerId, which uniquely identifies the customer and is needed when confirming the order.
expresscheckout1.Token = token; expresscheckout1.GetCheckoutDetails();
Then, confirm the authorization using the CheckoutPayment. Note that the PaymentAction will need to be set to Order here as well:
expresscheckout1.Token = token; expresscheckout1.OrderTotal = "45.00"; expresscheckout1.PaymentAction = ExpresscheckoutPaymentActions.aOrder; expresscheckout1.CheckoutPayment();
At this point, a transaction ID will be returned to be used for future order payment authorizations. This can be retrieved using the TransactionId configuration setting. This value should be saved at this time for future use.
When the time comes, authorize the first order payment using the AuthorizeOrderPayment config. This requires the TransactionId config to be set to the transaction ID returned when confirming the authorization. A different transaction ID will be returned from this call to be used as the authorization ID when capturing the payment:
expresscheckout1.Config("TransactionId=" + orderTransactionId); // The amount of the payment to be authorized expresscheckout1.OrderTotal = "25.00"; // Authorize the payment expresscheckout1.Config("AuthorizeOrderPayment"); //Save the transaction ID to capture the payment later string paymentAuthId = expresscheckout1.Config("TransactionId");
When ready to capture the payment, use the CaptureOrderPayment configuration setting to do so. This requires the AuthorizationId config to be set to the transaction ID returned from the previous step. The LastPayment config can also be used to tell PayPal whether this is the last payment for this order. If set to true (default), PayPal will prevent future authorizations on the order:
expresscheckout1.Config("AuthorizationId=" + paymentAuthId); // The amount of the payment to be captured expresscheckout1.OrderTotal = "25.00"; // Optional, is this the last payment or not? expresscheckout1.Config("LastPayment=false"); // Capture the payment expresscheckout1.Config("CaptureOrderPayment");
The authorization and capture steps can be repeated to process more order payments. On last payment, set LastPayment to true when capturing to prevent future authorizations.
For more information on processing order payments using Express Checkout, please see the following PayPal links: