Skip to content

Charge Method

The Charge Method is utilized in situations where users encounter insufficient funds in their Amazon Pay balance. It operates by orchestrating the charge process in a Chrome Custom Tab/Browser and manages the subsequent response back to the application.

Prerequisites

payURL: URL obtained from the server-side Charge API call.

Request Parameters

Parameter Type Description
requestID String Returned from the server-side Charge API call
payURL String URL received from the server-side Charge API call

Response Parameters

Parameter Type Description
ChargeResponse Object Contains the essential transaction details

Using Objective-C

Calling the API

APayChargeRequest *request = [APayChargeRequest build:^(id<APayChargeRequestBuilder> builder) {
    [builder withPayURL:params[@"response"][@"payURL"]];
    [builder withRequestId:params[@"requestId"]];
}];

APayChargeCallbackHandler *callbackHandler = [[APayChargeCallbackHandler alloc] init];

[[AmazonPay sharedInstance] charge:request apayChargeCallback:callbackHandler];

Handling Response

APayChargeCallbackHandler.h

#import <AmazonPayHardenediOSSDK/APayChargeCallbackDelegate.h> 

@interface APayChargeCallbackHandler: NSObject<APayChargeCallbackDelegat e> 
@end

APayChargeCallbackHandler.m

#import <Foundation/Foundation.h> 
#import "APayChargeCallbackHandler.h" 
#import "SilentPayHelper.h" 
#import <UIKit/UIKit.h> 
#import "APayChargeCallbackHandler.h" 

@implementation APayChargeCallbackHandler 
- (void)onFailure:(NSError *)error { 
    // charge failed 
}

- (void)onMobileSDKError:(NSError *)error { 
    //mobile sdk error 
} 

- (void)onNetworkUnavailable { 
    //network unavailable 
}

- (void)onSuccess:(APayChargeResponse *)response { 
    // Confirm order using backend status check API 
} 

- (void)onCancel { 
    //user clicked ‘done’ button in SFSafariViewController 
} 
@end

Using Swift

Calling the API

// Extract payURL and requestId from charge response, if available
let request = APayChargeRequest.build({ builder in builder?.withPayURL(responseDict["payURL"]! as!String)
    builder?.withRequestId(params!["requestId"]! as!String)
})

let callbackHandler = APayChargeCallbackHandler()

AmazonPay.sharedInstance().charge(request, apayChargeCallback: callbackHandler)

Handling Response

class APayChargeCallbackHandler : APayChargeCallbackDelegate {
    func onSuccess(_ response: APayChargeResponse!) {
        //Confirm order using backend status check API 
    }

    func onFailure(_ error: Error!) {
        // Handle charge failure
    }

    func onMobileSDKError(_ error: Error!) {
        // Handle mobile SDK specific error
    }

    func onNetworkUnavailable() {
        // Handle connection issues
    }

    func onCancel() {
        // User canceled the charge
    }
}