forked from rm3rdmodules/react-native-iamport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
139 lines (111 loc) · 3.78 KB
/
index.android.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
import React, {Component} from 'react';
import {requireNativeComponent, DeviceEventEmitter} from 'react-native';
const IAmPortViewManager = requireNativeComponent('IAmPortViewManager', null);
class IAmPort extends Component {
componentDidMount() {
DeviceEventEmitter.addListener('paymentEvent', this.paymentEvent.bind(this));
}
componentWillUnmount() {
DeviceEventEmitter.removeAllListeners('paymentEvent');
}
getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results)
return null;
if (!results[2])
return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
paymentEvent(e) {
var url = e.result;
var me = this;
var original = e;
//TODO delete
console.log("paymentEvent", e);
if (e.result == "success" || e.result == "failed") {
this.props.onPaymentResultReceive(e);
}
var imp_uid = this.getParameterByName("imp_uid", url),
merchant_uid = this.getParameterByName("merchant_uid", url),
result = "";
if (url.includes('success=false')) { // 취소 버튼을 눌렀거나 결제 실패시
result = "failed"
} else if (url.includes('success=true')) {
result = "success";
} else if (url.includes('payments/vbank')) {
result = "vbank";
}
if (result) {
this.props.onPaymentResultReceive({result, imp_uid, merchant_uid, original});
}
return true;
}
getRequestContent() {
let params = this.props.params;
const merchant_uid = params.merchant_uid || ('merchant_' + new Date().getTime());
const m_redirect_url = params.m_redirect_url || (params.pg == 'paypal' ? 'https://service.iamport.kr/payments/success' : null);
let HTML = `
<!DOCTYPE html>
<html>
<head>
<title>i'mport react native payment module</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" ></script>
<script type="text/javascript" src="https://service.iamport.kr/js/iamport.payment-1.1.5.js"></script>
<script type="text/javascript">
var IMP = window.IMP;
IMP.init('${params.code}');
IMP.request_pay({
pg : '${params.pg}',
pay_method : '${params.pay_method}',
merchant_uid : '${merchant_uid}',
m_redirect_url : '${m_redirect_url}',
app_scheme : '${params.app_scheme}',
name : '${params.name}',
amount : ${params.amount},
buyer_email : '${params.buyer_email}',
buyer_name : '${params.buyer_name}',
buyer_tel : '${params.buyer_tel}',
buyer_addr : '${params.buyer_addr}',
buyer_postcode : '${params.buyer_postcode}',
vbank_due : '${params.vbank_due}',
kakaoOpenApp : ${params.pg === "kakaopay"}
}, function(rsp){
if('${params.pg}' == 'nice'){
return;
}
iamport.receiveResult(rsp.success ? "success" : "failed", rsp.imp_uid, rsp.merchant_uid);
});
</script>
</body>
</html>
`;
return HTML;
}
_onPaymentResultReceive(e) {
// TODO: delete
// console.log(e);
if (this.props.onPaymentResultReceive) {
this.props.onPaymentResultReceive(e);
}
}
render() {
return (
<IAmPortViewManager
{...this.props}
source={this.getRequestContent()}
pg={this.props.params.pg}
appScheme={this.props.params.app_scheme}
></IAmPortViewManager>
);
}
}
module.exports = IAmPort