Skip to content

Commit

Permalink
Merge pull request #29 from iamport/fix/custom_data
Browse files Browse the repository at this point in the history
fix : custom_data base64 en/decode 처리 [PCM-438]
  • Loading branch information
kjh5833 authored Jan 12, 2022
2 parents fe55b45 + d8de104 commit 0b4ac24
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@
"version": "5.4.3"
}
},
{
"package": "GenericJSON",
"repositoryURL": "https://github.com/zoul/generic-json-swift",
"state": {
"branch": null,
"revision": "a137894b2a217abe489cdf1ea287e6f7819b1051",
"version": "2.0.1"
}
},
{
"package": "RxBus",
"repositoryURL": "https://github.com/ridi/RxBus-swift",
"repositoryURL": "https://github.com/ridi/RxBus-swift.git",
"state": {
"branch": null,
"revision": "b154da5fffb4c5d0d17b7f46e9cd38f382e298e4",
Expand Down
38 changes: 28 additions & 10 deletions Example/iamport-ios/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ViewModel: ObservableObject, Then {
@Published var isPayment: Bool = false
@Published var isCert: Bool = false
@Published var showResult: Bool = false
@Published var CardDirectCode: String = "365"
@Published var CardDirectCode: String = ""
var iamPortResponse: IamPortResponse?

init() {
Expand Down Expand Up @@ -110,15 +110,33 @@ public class ViewModel: ObservableObject, Then {
if (isCardDirect) {
$0.card = Card(direct: Direct(code: order.cardCode.value))
}
$0.custom_data = [
"num": 1,
"str": "baz",
"bool": true,
"obj": [
["foo": "jar"],
["bar": 1]
]
]
$0.custom_data = """
{
"employees": {
"employee": [
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise",
"photo": "https://jsonformatter.org/img/tom-cruise.jpg",
"cuppingnote": "[\\"\\",\\"\\",\\"\\",\\"\\",\\"\\",\\"\\",\\"\\"]"
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova",
"photo": "https://jsonformatter.org/img/Maria-Sharapova.jpg"
},
{
"id": "3",
"firstName": "Robert",
"lastName": "Downey Jr.",
"photo": "https://jsonformatter.org/img/Robert-Downey-Jr.jpg"
}
]
}
}
"""
}

return req
Expand Down
4 changes: 1 addition & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ let package = Package(
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/ReactiveX/RxSwift.git", .upToNextMajor(from: "6.0.0")),
.package(name: "RxBus", url: "https://github.com/ridi/RxBus-swift", .upToNextMajor(from: "1.3.2")),
.package(name: "RxBus", url: "https://github.com/ridi/RxBus-swift.git", .upToNextMinor(from: "1.3.0")),
.package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.4.0")),
.package(url: "https://github.com/devxoul/Then.git", .upToNextMajor(from: "2.7.0")),
.package(name: "GenericJSON", url: "https://github.com/zoul/generic-json-swift", .upToNextMajor(from: "2.0.1")),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -32,7 +31,6 @@ let package = Package(
"RxBus",
"Alamofire",
"Then",
"GenericJSON",
.product(name: "RxCocoa",package: "RxSwift"),
],
resources: [.process("Assets")],
Expand Down
18 changes: 17 additions & 1 deletion Sources/iamport-ios/Assets/iamportcdn.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
});

function dLog(log) {
console.log(log)
<!-- console.log(log)-->
iosHandler.debugConsoleLog.postMessage(log)
}

Expand All @@ -39,6 +39,22 @@
IMP.request_pay(JSON.parse(impRequest), callback);
}

function requestPayWithCustomData(impRequest, customData) {
var data = JSON.parse(impRequest)

var decodeCustom = decodeURIComponent(escape(window.atob(customData)))
dLog(decodeCustom)

try {
data.custom_data = JSON.parse(decodeCustom)
} catch (error) {
dLog(error.message)
data.custom_data = decodeCustom
}

IMP.request_pay(data, callback);
}

function certification(impCertification) {
dLog("certification :: " + impCertification)
IMP.certification(JSON.parse(impCertification), callback);
Expand Down
7 changes: 5 additions & 2 deletions Sources/iamport-ios/Classes/Data/IamPortRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import Foundation
import Then
import GenericJSON

/**
app_scheme 에 대하여..
Expand All @@ -19,7 +18,7 @@ public class IamPortRequest: Codable, Then {
public var customer_uid: String?
public var name: String?
let amount: String // 없음안됨
public var custom_data: JSON?
public var custom_data: String?
public var tax_free: Float?
public var currency: String? // default KRW 페이팔은 USD 이어야 함
public var language: String? // default "ko"
Expand Down Expand Up @@ -63,6 +62,10 @@ public class IamPortRequest: Codable, Then {
func getRedirectUrl() -> String? {
m_redirect_url
}

private enum CodingKeys: String, CodingKey {
case pg, pay_method, escrow, merchant_uid, customer_uid, name, amount, tax_free, currency, language, buyer_name, buyer_tel, buyer_email, buyer_addr, buyer_postcode, notice_url, display, digital, vbank_due, m_redirect_url, app_scheme, biz_num, popup, niceMobileV2, naverPopupMode, naverUseCfm, naverProducts, naverCultureBenefit, naverProductCode, naverActionType, cultureBenefit, naverInterface, card
}
}

extension IamPortRequest {
Expand Down
3 changes: 1 addition & 2 deletions Sources/iamport-ios/Classes/Data/PrepareRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import Foundation
import Then
import GenericJSON

class PrepareRequest: DictionaryEncodable, Then {

Expand All @@ -26,7 +25,7 @@ class PrepareRequest: DictionaryEncodable, Then {
var buyer_addr: String? // 구매자 주소
var buyer_postcode: String? // 구매자 우편번호
var app_scheme: String? // 결제 후 돌아갈 app scheme
var custom_data: JSON? // 결제 건에 연결해 저장할 meta data
var custom_data: String? // 결제 건에 연결해 저장할 meta data
var notice_url: Array<String>? // Webhook Url
var confirm_url: String? // Confirm process Url
var _extra: Extra // 차이 마케팅 팀과 사전협의된 파라메터
Expand Down
7 changes: 7 additions & 0 deletions Sources/iamport-ios/Classes/Domain/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,11 @@ class Utils {
}
}


}

extension String {
func getBase64Encode() -> String? {
data(using: .utf8)?.base64EncodedString()
}
}
63 changes: 52 additions & 11 deletions Sources/iamport-ios/Classes/Presentation/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -540,17 +540,23 @@ extension WebViewController: WKScriptMessageHandler {
}
ddump(pay)

let encoder = JSONEncoder()
// encoder.outputFormatting = .prettyPrinted

initSDK(userCode: pay.userCode, tierCode: pay.tierCode)

if (pay.isCertification()) {
let jsonData = try? encoder.encode(pay.iamPortCertification)

guard let iamPortCertification = pay.iamPortCertification else {
print("iamPortCertification 를 찾을 수 없습니다.")
return
}

let jsonData = try? JSONEncoder().encode(iamPortCertification)
certification(impCertificationJsonData: jsonData)
} else {
let jsonData = try? encoder.encode(pay.iamPortRequest)
requestPay(impRequestJsonData: jsonData)
guard let iamPortRequest = pay.iamPortRequest else {
print("iamPortRequest 를 찾을 수 없습니다.")
return
}
requestPay(iamPortRequest: iamPortRequest)
}

case .RECEIVED:
Expand Down Expand Up @@ -585,12 +591,47 @@ extension WebViewController: WKScriptMessageHandler {
evaluateJS(method: jsInitMethod)
}

private func requestPay(impRequestJsonData: Data?) {
if let json = impRequestJsonData,
let request = String(data: json, encoding: .utf8) {
dlog("requestPay request : '\(request)'")
evaluateJS(method: "requestPay('\(request)');")
private func requestPay(iamPortRequest: IamPortRequest) {

guard let impRequestJsonData = try? JSONEncoder().encode(iamPortRequest) else {
print("requestPay :: iamPortRequest 을 JSONEncoder encode 할 수 없습니다.")
return
}

if let customData = iamPortRequest.custom_data {
requestPayWithCustomData(impRequestJsonData: impRequestJsonData, customData: customData)
} else {
requestPayNormal(impRequestJsonData: impRequestJsonData)
}
}


private func requestPayNormal(impRequestJsonData: Data) {

guard let request = String(data: impRequestJsonData, encoding: .utf8) else {
print("requestPayNormal :: impRequestJsonData 을 String 화 할 수 없습니다.")
return
}

dlog("requestPay request : '\(request)'")
evaluateJS(method: "requestPay('\(request)');")
}

private func requestPayWithCustomData(impRequestJsonData: Data, customData: String) {

guard let request = String(data: impRequestJsonData, encoding: .utf8) else {
print("requestPayWithCustomData :: impRequestJsonData 을 String 화 할 수 없습니다.")
return
}

guard let encodedCustomData = customData.getBase64Encode() else {
print("requestPayWithCustomData :: getBase64Encode 를 가져올 수 없어 requestPayNormal 실행")
requestPayNormal(impRequestJsonData: impRequestJsonData)
return
}

dlog("requestPayWithCustomData request : '\(request)', encodedCustomData : '\(encodedCustomData)'")
evaluateJS(method: "requestPayWithCustomData('\(request)', '\(encodedCustomData)');")
}

private func certification(impCertificationJsonData: Data?) {
Expand Down
5 changes: 2 additions & 3 deletions iamport-ios.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'iamport-ios'
s.version = '1.1.0-hotfix03'
s.version = '1.2.0'
s.summary = 'iamport-ios will help develop for your iOS App payments'

# This description is used to generate tags and improve search results.
Expand Down Expand Up @@ -59,7 +59,6 @@ Pod::Spec.new do |s|
s.dependency 'RxRelay'
s.dependency 'RxOptional'
s.dependency 'RxViewController'

s.dependency 'GenericJSON'


end

0 comments on commit 0b4ac24

Please sign in to comment.