Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of customField to chargeCard request parameters #42

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ chargeCard() {
email: 'chargeIOS@master.dev',
amountInKobo: 150000,
subAccount: 'ACCT_pz61jjjsslnx1d9',
customField: [
{display_name: "Product", value: "Prada Women Shoe"},
{display_name: "Product S/N", value: "1002345678"}
]
})
.then(response => {
console.log(response); // card charged successfully, get reference here
Expand Down Expand Up @@ -252,6 +256,7 @@ chargeCard() {
| transactionCharge (optional) | integer | the amount to be charged on a split-payment, use only when `subAccount` is set |
| bearer (optional) | string | sets which party bears paystack fees on a split-payment e.g. 'subaccount', use only when `subAccount` is set |
| reference (optional) | string | sets the transaction reference which must be unique per transaction |
| customField (optional) | Array | sets the custom fields you want included in the transaction details while viewing on paystack dashboard e.g. `customField: [{display_name: "Your display name", value: "Your set value"}, ..., {display_name: "...", value: "..."}]` [Check the implementation here](#charging-a-card-ios--android) |

#### Response Object

Expand Down Expand Up @@ -301,6 +306,7 @@ Perhaps needless to say, this module leverages the [Paystack Android SDK](https:
* 3.1.4: Miscellaneous and dependencies update on Android.
* 3.2.0: A Breaking Change - Initialize library in JS, rather than in native code.
* 3.3.0: Move to a CocoaPods Flow for iOS.
* 3.4.1: Minor update to include `customField` in request.

## 6. License

Expand Down
24 changes: 24 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import RNPaystack from "..";

const PUBLIC_KEY = "pk_test_63847c1ea97fdd25178ca0297bbdb71542e1a9bb";
RNPaystack.init({ publicKey: PUBLIC_KEY});

it("Expects custom fields to be added", async () => {

try {
const response = await RNPaystack.chargeCard({
cardNumber: '4123450131001381',
expiryMonth: '10',
expiryYear: '17',
cvc: '883',
email: 'chargeIOS@master.dev',
amountInKobo: 150000,
subAccount: 'ACCT_pz61jjjsslnx1d9',
});

console.log("Paystack Response => ", response);
} catch (err) {
console.log("An error occurred => ", err);
}

})
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ dependencies {
implementation "com.facebook.react:react-native:+"
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'co.paystack.android.design.widget:pinpad:1.0.1'
implementation 'co.paystack.android:paystack:3.0.12'
implementation 'co.paystack.android.design.widget:pinpad:1.0.8'
implementation 'co.paystack.android:paystack:3.1.3'
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ private void validateFullTransaction() {
charge.setReference(chargeOptions.getString("reference"));
}

// **NEW: Added this for metadata/custom fields
if (hasArrayKey("customField")) {
// charge.setReference(chargeOptions.get)
ReadableArray customFields = chargeOptions.getArray("customField");
int customFieldListSize = customFields.size();
for (int i = 0; i < customFieldListSize; i++) {
ReadableMap customField = customFields.getMap(i);
String displayName = customField.getString("display_name");
String value = customField.getString("value");

// Now set paystack custom field.
try {
charge.putCustomField(displayName, value);
} catch (JSONException err) {
//Log.d("Custom Field Error", err);
}
}
}

}

private void createTransaction() {
Expand Down Expand Up @@ -313,6 +332,10 @@ private boolean hasIntKey(String key) {
return chargeOptions.hasKey(key) && !chargeOptions.isNull(key) && chargeOptions.getInt(key) > 0;
}

private boolean hasArrayKey(String key) {
return chargeOptions.hasKey(key) && !chargeOptions.isNull(key) && chargeOptions.getArray(key).size() > 0;
}

private void rejectPromise(String code, String message) {
if (this.pendingPromise != null) {
this.pendingPromise.reject(code, message);
Expand Down
16 changes: 13 additions & 3 deletions ios/RNPaystack.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth
requestIsCompleted = NO;

if (! [self cardParamsAreValid:params[@"cardNumber"] withMonth:params[@"expiryMonth"] withYear:params[@"expiryYear"] andWithCvc:params[@"cvc"]]) {

requestIsCompleted = YES;
// NSMutableDictionary *returnInfo = [self setErrorMsg:self.errorMsg withErrorCode:self.errorCode];
if (_reject) {
_reject(self.errorCode, self.errorMsg, nil);
Expand Down Expand Up @@ -170,7 +170,17 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth

if (params[@"reference"] != nil) {
transactionParams.reference = params[@"reference"];
}
}

// **NEW: Adding Custom Fields to Transaction
if (params[@"customField"] != nil) {
for (NSDictionary *customField in params[@"customField"]) {
if (customField[@"value"] != nil && customField[@"display_name"] != nil) {
NSError * __autoreleasing _Nullable *transactionError = nil;
[transactionParams setCustomFieldValue: customField[@"value"] displayedAs:customField[@"display_name"] error:&transactionError];
}
}
}

if ([self isCardValid:cardParams]) {

Expand Down Expand Up @@ -224,7 +234,7 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth
requestIsCompleted = NO;

if (! [self cardParamsAreValid:params[@"cardNumber"] withMonth:params[@"expiryMonth"] withYear:params[@"expiryYear"] andWithCvc:params[@"cvc"]]) {

requestIsCompleted = YES;
// NSMutableDictionary *returnInfo = [self setErrorMsg:self.errorMsg withErrorCode:self.errorCode];
if (_reject) {
_reject(self.errorCode, self.errorMsg, nil);
Expand Down
Loading