Using the JPPaymentContext 2017-03-08T15:03:06+00:00

iOS – Using the JPPaymentContext 

JPPaymentContext is a singleton model object used to collect together all data required to make a COMPLETE and VALID transaction payment.  In addition, you can copy additional data into the JPPaymentContext which can be used at a later time to relate transaction data with data structures in your own application.

Example data would be transaction amount, a payment card and customer details.

Data can be added to JPPaymentContext directly by your app via a well defined API and set of read/write properties. In addition, when using SDK UI elements to collect data on your behalf, this data may be copied into the JPPaymentContext automatically or you must move these data from these SDK UI elements into the JPPaymentContext yourself.

Once all data for your current transaction has been collected (i.e. stored in JPPaymentContext), your app can request JPPaymentContext to submit the transaction to the JetPay gateway for processing. At this point, JPPaymentContext will verify that all required data is present, validate this data and submit the transaction request.

Invalid or incomplete data is NOT submitted. All feedback is through the JPPaymentContextDelegate protocol or the Swift closure used when requesting the SDK to post a transaction. In addition, the JPPaymentContext always maintains a – which can be interrogated by you for status of data or error conditions when the JPPaymentContext POSTed the last transaction request.

Once a transaction has been made by JPPaymentContext, the context is NOT cleared. You are responsible for doing this yourself. Part of the reason for this is if you want to reference the successful transaction back to your own data objects. JPPaymentContext holds it’s context data until you clear it.

JPPaymentContext can ONLY make one transaction request at a time. When requested to POST a transaction request JPPaymentContext effectively “locks” the context – any attempt to change context data is silently ignored. The JPPaymentContext is only unlocked once a transaction POST has completed (either successfully or with failure).

For more details, please see the JetPay Full iOS SDK Reference.

This snippet illustrates directly loading JPPaymentContext with data, requesting a transaction to be processed, and receiving feedback via the JPPaymentContextDelegate protocol AND the Swift closure:

func postExampleTransaction() -> Void {

   let payContext = JPPaymentContext.sharedContext
   payContext.delegate = self

   /* create and load customer data into the payment context */
   let customer = JPCustomer(customerID: “your customer ID”, firstName: “Jon”, secondName: “Appleseed”)
   payContext.setCustomer( customer )

   /* create and load payment card information */
   let payCard = JPCardParams(number: “4242 1111 2222 3333”, expireMonth:06, expireYear:2017, cvc: “123”)
   payContext.setPaymentCard( payCard )

   /* set a transaction amount - currency independent */
   payContext.setPaymentAmount( 15.00 )

        /* set the transactionId */
        payContext.setTransactionId( “010327153017T10017” )

   if payContext.isValid() {
      payContext.postSaleTransaction { [weak self] (transactionResponse, error) in

         guard error == nil else {
            /* error - do something! */
            return
         }

         /* no error, we will have a valid response from the Jetpay backend. HOWEVER!, the transactionResponse may still represent a problem with the request - a DECLINED card for example */

         if let tr = transactionResponse {
            if tr.actionCode.isApproved() {
               /* transaction approved */
            } else {
               /* not approved */
            }
         } else {
            /* this will not happen - transactionResponse==nil AND error==nil is NOT a valid combination */
      }
   }
}

/* feedback from the delegate protocol */
func paymentContextDidPostTransaction( paymentContext: JPPaymentContext ) -> Void {

    func paymentContextDidPostTransaction(_ paymentContext: JPPaymentContext) {
        print( "ViewController: Payment Context did post transaction" )
    }
    
    func paymentContextDidCancelPostAsPaymentAmountIsInvalid(_ paymentContext: JPPaymentContext) {
        print( "ViewController: Payment Context did CANCEL POST because Payment Amount is invalid" )
    }
    
    func paymentContextDidCancelPostAsPaymentCardIsInvalid(_ paymentContext: JPPaymentContext) {
        print( "ViewController: Payment Context did CANCEL POST because Payment Card is invalid" )
    }
    
    func paymentContextDidCancelPostAsPaymentCardIsUnsupported(_ paymentContext: JPPaymentContext) {
        print( "ViewController: Payment Context did CANCEL POST because Payment Card is valid BUT unsupported by the Jetpay Gateway“ )
    }
    
    func paymentContextDidCancelPostAsTransactionIdIsInvalid(_ paymentContext: JPPaymentContext) {
        print( "ViewController: Payment Context did CANCEL POST because TransactionId is invalid" )
    }
    
    func paymentContextDidCancelPostAsPostCurrentlyInProgress(_ paymentContext: JPPaymentContext) {
        print( "ViewController: Payment Context did CANCEL POST because Payment Transaction in progress" )
    }
    
    func paymentContextTransactionPostReceivedTransactionResponse(_ paymentContext: JPPaymentContext) {
        print( "ViewController: Payment Context did receive a transaction response" )
    }
    
    func paymentContextTransactionPostDidNotReceiveTransactionResponse(_ paymentContext: JPPaymentContext) {
        print( "ViewController: Payment Context did NOT receive a transaction response" )
    }
}

 

- (void)postExampleTransaction {

    JPPaymentContext *payContext = [JPPaymentContext sharedContext];
    payContext.delegate = self;
    
    /* create and load customer data into the payment context */
    JPCustomer *customer = [[JPCustomer alloc] initWithCustomerID:@"your customer ID" firstName:@"Jon" secondName:@"Appleseed"];
    [payContext setCustomer:customer];
    
    /* create and load payment card information */
    JPCardParams *payCard = [[JPCardParams alloc] initWithCardNumber:@"4242 1111 2222 3333" expireMonth:06 expireYear:2017 cvc:@"123"];
    [payContext setPaymentCard:payCard];
    
    /* set a transaction amount - currency independent */
    [payContext setPaymentAmount:15.00];
    
    /* set the transactionId */
    [payContext setTransactionId:@"010327153017T10017"];
    
    if ([payContext isValid]) {
        [payContext postSaleTransaction:^(JPTransactionResponse *transactionResponse, NSError *error) {
            
            if (error != nil) {
                /* error - do something! */
                return;
            }
            
            /* no error, we will have a valid response from the Jetpay backend. HOWEVER!, the transactionResponse may still present a problem with the request - a DECLINED card for example */
            if (transactionResponse != nil) {
                if ([transactionResponse.actionCode isApproved]) {
                    /* transaction approved */
                } else {
                    /* not approved */
                }
            } else {
                /* this will not happen - transactionResponse AND error=nil is NOT a valid combination */
            }
        }];
    }
}

- (void)paymentContextTransactionPostDidNotReceiveTransactionResponse:(JPPaymentContext *)paymentContext {
    NSLog( @"ViewController: Payment Context did not receive transaction response" );
}

- (void)paymentContextTransactionPostReceivedTransactionResponse:(JPPaymentContext *)paymentContext {
    NSLog( @"ViewController: Payment Context did received transaction post response" );
    
}

- (void)paymentContextDidPostTransaction:(JPPaymentContext *)paymentContext {
    NSLog( @"ViewController: Payment Context did post transaction" );
    
}

- (void)paymentContextDidCancelPostAsPaymentCardIsInvalid:(JPPaymentContext *)paymentContext {
    NSLog( @"ViewController: Payment Context cancelled transaction post because payment card is invalid" );
    
}

- (void)paymentContextDidCancelPostAsPaymentAmountIsInvalid:(JPPaymentContext *)paymentContext {
    NSLog( @"ViewController: Payment Context cancelled transaction post because payment amount is invalid" );
    
}

- (void)paymentContextDidCancelPostAsTransactionIdIsInvalid:(JPPaymentContext *)paymentContext {
    NSLog( @"ViewController: Payment Context cancelled transaction post because transactionId is invalid" );
    
}

- (void)paymentContextDidCancelPostAsPostCurrentlyInProgress:(JPPaymentContext *)paymentContext {
    NSLog( @"ViewController: Payment Context cancelled transaction post because an existing transaction request is currently in progress" );
    
}