2016-12-26 5 views
0

Braintree 드롭 인을 사용하여 트랜잭션을 생성하기위한 Braintree 드롭 인 Swift 코드와 Ruby 서버 코드를 참조하십시오. 이것은 1 USD의 거래를 생성하기 위해 멋지게 작동하며 모든 것은 Braintree에서 올바르게 예약됩니다.iOS Swift의 Braintree : Ruby 서버에 다른 거래 금액을 전달하는 방법은 무엇입니까?

이제 제 질문은 금액을 변경하는 방법입니까? 내 신속한 코드에서 내 루비 서버로 변수를 전달하는 방법을 모르겠다. (또한 원하는 양을 포함하고있다.) 또한 그렇게 할 수 있는지 또는 전달할 때 양을 암호화해야하는지 궁금하다.

제쳐두고, 저는 Swift에서 Ruby로 금액을 전달하는 대안이 될 수있는 Swift 코드에서 'request.amount = "23.00"'을 포함하는 문구를 보았습니다. 나는 올바르게 사용하는 방법을 모르겠어요 그리고 내가 사용하는 브레인 트리의 웹 사이트에 설명 아니에요 : https://developers.braintreepayments.com/start/hello-server/python#create-a-transaction

SWIFT를 (App에서) : (Heroku가에)

func postNonceToServer(paymentMethodNonce: String) { 
    let paymentURL = URL(string: "https://myexample-31423.herokuapp.com/checkout")! 
    let request = NSMutableURLRequest(url: paymentURL) 
    request.httpBody = "payment_method_nonce=\(paymentMethodNonce)".data(using: String.Encoding.utf8) 
    request.httpMethod = "POST" 
    URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) -> Void in 
     // TODO: Handle success or failure 
     }.resume() 
} 

func showDropIn(clientTokenOrTokenizationKey: String) { 
    let request = BTDropInRequest() 
    let dropIn = BTDropInController(authorization: clientTokenOrTokenizationKey, request: request) 
    { (controller, result, error) in 
     if (error != nil) { 
      print("ERROR") 
     } else if (result?.isCancelled == true) { 
      print("CANCELLED") 
     } else if let result = result {  
      let selectedPaymentMethod = result.paymentMethod! 
      self.postNonceToServer(paymentMethodNonce: selectedPaymentMethod.nonce) 
     } 
     controller.dismiss(animated: true, completion: nil) 
    } 
    self.present(dropIn!, animated: true, completion: nil) 
} 

RUBY :

post "/checkout" do 
    nonce_from_the_client = params[:payment_method_nonce] 
    result = Braintree::Transaction.sale(
         :amount => "1.00", 
         :payment_method_nonce => nonce_from_the_client, 
         :options => { 
         :submit_for_settlement => true 
         } 
    ) 
end 

답변

1

좋아, 나 스스로 해결했다. 스위프트에서 다음 줄을 사용하십시오.

let amount  = "50" as String 
request.httpBody = "payment_method_nonce=\(paymentMethodNonce)&amount=\(amount)".data(using: String.Encoding.utf8) 

그리고 나서 루비에서는 다음을 사용하십시오.

post "/checkout" do 
    nonce_from_the_client = params[:payment_method_nonce] 
    amount     = params[:amount] 
    result = Braintree::Transaction.sale(
            :amount => amount, 
            :payment_method_nonce => nonce_from_the_client, 
            :options => { 
            :submit_for_settlement => true 
            } 
            ) 
end 

그 일을합니다. 나는 두 변수가 단순히 '&'에 의해 분리를 통과 할 수 있다는 것을 알지 못했습니다.