Cancel a Transaction

This code steps you through the process of cancelling a credit card transaction.

You can cancel a transaction if it has not yet been settled. If a transaction has already been settled, then you must issue a refund instead.
For that Go to: Reverse a sale

This code cancels single transactions; it doesn't cancel recurring payments.
For that Go to: Cancel recurring payments

There are three outcomes when processing credit card transactions:

  1. Approved
  2. Declined
  3. Error

'Approved' transactions can be canceled. 'Declined' transactions cannot be canceled. As a result, in order to test the cancellation of a transaction, you must first pass an odd dollar amount. This will return the transaction with an 'Approved' status. Now, you can test the cancellation of that transaction.

An error could result from a variety of conditions such as missing data, invalid card numbers, and expiration dates provided in invalid formats.

The following sample code calls a routine from our Code Library.
Download BluePay.cs

/*
* BluePay C#.NET Sample code.
*
* This code sample runs a $3.00 Credit Card Sale transaction
* against a customer using test payment information.
* If using TEST mode, odd dollar amounts will return
* an approval and even dollar amounts will return a decline.
*/

using System;
using BluePayLibrary;

namespace Transactions
{
    class CancelTransaction
    {
        public static void Main()
        {
            string accountID = "Merchant's Account ID Here";
            string secretKey = "Merchant's Secret Key Here";
            string mode = "TEST";

            BluePay payment = new BluePay
            (
                accountID,
                secretKey,
                mode
            );

            payment.SetCustomerInformation
            (
                firstName: "Bob",
                lastName: "Tester",
                address1: "1234 Test St.",
                address2: "Apt #500",
                city: "Testville",
                state: "IL",
                zip: "54321",
                country: "USA",
                phone: "123-123-12345",
                email: "[email protected]"
            );
           
            payment.SetCCInformation
            (
                ccNumber: "4111111111111111",
                ccExpiration: "1215",
                cvv2: "123"
            );

            // Sale Amount: $3.00
            payment.Sale(amount: "3.00");

            // Makes the API Request with BluePay
            payment.Process();


            if (payment.IsSuccessfulTransaction())
            {
                // Creates a payment cancelation
                BluePay paymentCancel = new BluePay
                (
                    accountID,
                    secretKey,
                    mode
                );

                // Finds the previous payment by ID and attempts to void it
                paymentCancel.Void(payment.GetTransID());

                // Makes the API Request with BluePay to cancel transaction
                paymentCancel.Process();

                // Reads the responses from BluePAy
                Console.WriteLine("Transaction Status: " + paymentCancel.GetStatus());
                Console.WriteLine("Transaction ID: " + paymentCancel.GetTransID());
                Console.WriteLine("Transaction Message: " + paymentCancel.GetMessage());
                Console.WriteLine("AVS Response: " + paymentCancel.GetAVS());
                Console.WriteLine("CVV2 Response: " + paymentCancel.GetCVV2());
                Console.WriteLine("Masked Payment Account: " + paymentCancel.GetMaskedPaymentAccount());
                Console.WriteLine("Card Type: " + paymentCancel.GetCardType());
                Console.WriteLine("Authorization Code: " + paymentCancel.GetAuthCode());
            }
            else
            {
                Console.WriteLine("Error: " + payment.GetMessage());
            }
        }
    }
}
BluePay Processing, LLC is a registered ISO of Wells Fargo Bank, N.A., Walnut Creek, CA, U.S.A.
BluePay Canada ULC, is a Registered ISO/MSP of Peoples Trust Company, Vancouver, Canada.