1 | //
|
2 | // RazorpayEventEmitter.m
|
3 | // RazorpayCheckout
|
4 | //
|
5 | // Created by Akshay Bhalotia on 19/09/16.
|
6 | // Copyright © 2016 Facebook. All rights reserved.
|
7 | //
|
8 |
|
9 | #import "RazorpayEventEmitter.h"
|
10 |
|
11 | #import "RCTBridge.h"
|
12 | #import "RCTEventDispatcher.h"
|
13 |
|
14 | NSString *const kPaymentError = @"PAYMENT_ERROR";
|
15 | NSString *const kPaymentSuccess = @"PAYMENT_SUCCESS";
|
16 | NSString *const kExternalWalletSelected = @"EXTERNAL_WALLET_SELECTED";
|
17 |
|
18 | @implementation RazorpayEventEmitter
|
19 |
|
20 | RCT_EXPORT_MODULE();
|
21 |
|
22 | - (NSArray<NSString *> *)supportedEvents {
|
23 | return @[
|
24 | @"Razorpay::PAYMENT_SUCCESS",
|
25 | @"Razorpay::PAYMENT_ERROR",
|
26 | @"Razorpay::EXTERNAL_WALLET_SELECTED"
|
27 | ];
|
28 | }
|
29 |
|
30 | - (void)startObserving {
|
31 | [[NSNotificationCenter defaultCenter] addObserver:self
|
32 | selector:@selector(paymentSuccess:)
|
33 | name:kPaymentSuccess
|
34 | object:nil];
|
35 | [[NSNotificationCenter defaultCenter] addObserver:self
|
36 | selector:@selector(paymentError:)
|
37 | name:kPaymentError
|
38 | object:nil];
|
39 | [[NSNotificationCenter defaultCenter]
|
40 | addObserver:self
|
41 | selector:@selector(externalWalletSelected:)
|
42 | name:kExternalWalletSelected
|
43 | object:nil];
|
44 | }
|
45 |
|
46 | - (void)stopObserving {
|
47 | [[NSNotificationCenter defaultCenter] removeObserver:self];
|
48 | }
|
49 |
|
50 | - (void)paymentSuccess:(NSNotification *)notification {
|
51 | [self sendEventWithName:@"Razorpay::PAYMENT_SUCCESS"
|
52 | body:notification.userInfo];
|
53 | }
|
54 |
|
55 | - (void)paymentError:(NSNotification *)notification {
|
56 | [self sendEventWithName:@"Razorpay::PAYMENT_ERROR"
|
57 | body:notification.userInfo];
|
58 | }
|
59 |
|
60 | - (void)externalWalletSelected:(NSNotification *)notification {
|
61 | [self sendEventWithName:@"Razorpay::EXTERNAL_WALLET_SELECTED"
|
62 | body:notification.userInfo];
|
63 | }
|
64 |
|
65 | + (void)onPaymentSuccess:(NSString *)payment_id
|
66 | andData:(NSDictionary *)response {
|
67 | NSDictionary *payload = [NSDictionary dictionaryWithDictionary:response];
|
68 | [[NSNotificationCenter defaultCenter] postNotificationName:kPaymentSuccess
|
69 | object:nil
|
70 | userInfo:payload];
|
71 | }
|
72 |
|
73 | + (void)onPaymentError:(int)code
|
74 | description:(NSString *)str
|
75 | andData:(NSDictionary *)response {
|
76 | NSDictionary *payload = @{
|
77 | @"code" : @(code),
|
78 | @"description" : str,
|
79 | @"details" : response
|
80 | };
|
81 | [[NSNotificationCenter defaultCenter] postNotificationName:kPaymentError
|
82 | object:nil
|
83 | userInfo:payload];
|
84 | }
|
85 |
|
86 | + (void)onExternalWalletSelected:(NSString *)walletName
|
87 | andData:(NSDictionary *)paymentData {
|
88 |
|
89 | NSMutableDictionary *payload = [[NSMutableDictionary alloc] init];
|
90 | [payload addEntriesFromDictionary: paymentData];
|
91 | [payload setValue:walletName forKey:@"external_wallet"];
|
92 |
|
93 | [[NSNotificationCenter defaultCenter]
|
94 | postNotificationName:kExternalWalletSelected
|
95 | object:nil
|
96 | userInfo:payload];
|
97 | }
|
98 |
|
99 | @end
|