-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eumhongin
committed
Sep 16, 2022
1 parent
363dfe3
commit 57238fd
Showing
9 changed files
with
10,412 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,5 +147,8 @@ | |
} | ||
] | ||
] | ||
}, | ||
"dependencies": { | ||
"react-native-webview": "^11.23.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { SafeAreaView } from 'react-native'; | ||
import React from 'react'; | ||
import WebView, { WebViewMessageEvent } from 'react-native-webview'; | ||
|
||
import { isAppUrl, isBlank, openPGApp } from '../../libs/libs'; | ||
import type { TossPaymentRequestDataTypes } from './types/payment.types'; | ||
|
||
type Props = { | ||
clientKey: string; | ||
payment: TossPaymentRequestDataTypes; | ||
onWebViewMessageReceived: (e: WebViewMessageEvent) => void; | ||
detectIsLoading: (isLoading: boolean) => void; | ||
}; | ||
|
||
const Payment = ({ | ||
clientKey, | ||
payment, | ||
onWebViewMessageReceived, | ||
detectIsLoading, | ||
}: Props) => { | ||
const WEBVIEW_SOURCE_HTML = ` | ||
<html> | ||
<head> | ||
<meta http-equiv='content-type' content='text/html; charset=utf-8'> | ||
<meta name='viewport' content='width=device-width, initial-scale=1.0'> | ||
<script src="https://js.tosspayments.com/v1"></script> | ||
</head> | ||
<body> | ||
응 결제 준비중이야~ | ||
<script> | ||
var clientKey = '${clientKey}' | ||
var tossPayments = TossPayments(clientKey) // 클라이언트 키로 초기화하기 | ||
</script> | ||
</body> | ||
</html> | ||
`; | ||
|
||
return ( | ||
<SafeAreaView | ||
style={{ | ||
flex: 1, | ||
}} | ||
> | ||
<WebView | ||
style={{ | ||
flex: 1, | ||
}} | ||
source={{ | ||
html: WEBVIEW_SOURCE_HTML, | ||
}} | ||
injectedJavaScript={` | ||
tossPayments.requestPayment('카드',${JSON.stringify( | ||
payment | ||
)}).catch(err => { | ||
window.ReactNativeWebView.postMessage(JSON.stringify(err)); | ||
}) | ||
`} | ||
onMessage={onWebViewMessageReceived} | ||
originWhitelist={['*']} | ||
sharedCookiesEnabled={true} | ||
onShouldStartLoadWithRequest={(request) => { | ||
const { url, mainDocumentURL } = request; | ||
console.log(url, mainDocumentURL); | ||
if (isBlank(url, mainDocumentURL)) { | ||
detectIsLoading(true); | ||
return true; | ||
} | ||
|
||
detectIsLoading(false); | ||
|
||
if (isAppUrl(url)) { | ||
console.log('앱 URL 입니다.'); | ||
/* 3rd-party 앱 오픈 */ | ||
// console.log('3rd-party 앱 오픈'); | ||
openPGApp(url).catch((e) => { | ||
// const { code, message } = e; | ||
console.log(e); | ||
// callback({ | ||
// imp_success: false, | ||
// error_code: code, | ||
// error_msg: message, | ||
// }); | ||
}); | ||
|
||
return false; | ||
} | ||
console.log('앱 URL 아닙니다.'); | ||
return true; | ||
}} | ||
/> | ||
</SafeAreaView> | ||
); | ||
}; | ||
|
||
export default Payment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import React, { useCallback } from 'react'; | ||
import Payment from '../Payment'; | ||
|
||
import type { | ||
TossPaymentApproveTypes, | ||
TossPaymentFailMessageTypes, | ||
TossPaymentRequestDataTypes, | ||
TossPaymentResultMessageTypes, | ||
} from '../types/payment.types'; | ||
|
||
import type { WebViewMessageEvent } from 'react-native-webview'; | ||
|
||
type Props = { | ||
clientKey: string; | ||
payment: TossPaymentRequestDataTypes; | ||
onLoading: (isLoading: boolean) => void; | ||
onApproveError: () => void; | ||
onApproveFailed: (e: TossPaymentFailMessageTypes) => void; | ||
onApproveSucceed: (e: TossPaymentApproveTypes) => void; | ||
}; | ||
|
||
const PaymentContainer = ({ | ||
clientKey, | ||
payment, | ||
onLoading, | ||
onApproveError, | ||
onApproveFailed, | ||
onApproveSucceed, | ||
}: Props) => { | ||
const onWebViewMessageReceived = useCallback( | ||
async (e: WebViewMessageEvent) => { | ||
const tossPaymentMessageFromWeb = JSON.parse( | ||
e.nativeEvent.data | ||
) as TossPaymentResultMessageTypes; | ||
|
||
console.log(tossPaymentMessageFromWeb); | ||
|
||
// 웹뷰로 부터 성공 및 실패 둘중 아무것도 받지못했을떄. | ||
if (!tossPaymentMessageFromWeb.type) { | ||
onApproveError(); | ||
return; | ||
} | ||
|
||
// 결제 실패했을경우 | ||
if (tossPaymentMessageFromWeb.type === 'fail') { | ||
onApproveFailed(tossPaymentMessageFromWeb.data); | ||
return; | ||
} | ||
|
||
// 결제 승인되었을경우 | ||
onApproveSucceed(tossPaymentMessageFromWeb.data); | ||
}, | ||
[onApproveSucceed, onApproveError, onApproveFailed] | ||
); | ||
|
||
const detectIsLoading = useCallback( | ||
(isLoading: boolean) => { | ||
console.log(isLoading); | ||
onLoading(isLoading); | ||
}, | ||
[onLoading] | ||
); | ||
|
||
return ( | ||
<Payment | ||
clientKey={clientKey} | ||
payment={payment} | ||
onWebViewMessageReceived={onWebViewMessageReceived} | ||
detectIsLoading={detectIsLoading} | ||
/> | ||
); | ||
}; | ||
|
||
export default PaymentContainer; |
Oops, something went wrong.