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 |
Charge Response
Parameter | Type | Description |
---|---|---|
transactionId | String | Amazon-generated transaction ID |
signature | String | Signature for verifying response authenticity |
orderTotalAmount | String | Charged amount |
orderTotalCurrencyCode | String | Always INR |
status | String | Transaction status (SUCCESS, FAILURE) |
reasonCode | String | Reason code associated with the transaction |
description | String | Description corresponding to the reason code |
transactionDate | String | Transaction creation date |
sellerOrderId | String | Merchant-provided order ID |
customInformation | String | Optional information passed by the merchant (if added) |
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 {
//verify that response is SUCCESS
//because onSuccess will be called in case of PENDING response as well
if (response.status == SUCCESS) {
//next action to be taken after charge is successful
//merchant can verify the signature present in response
}
else {
}
}
- (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!) {
// Verify response status is SUCCESS
}
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
}
}