2013-10-30 101 views
1

這是我設計的一個網站,其中多個商店我可以提供他們的產品出售。每個賣家都會在我的網站上有一個虛擬商店。我使用paypal進行購買操作。我已考慮允許客戶使用信用卡,而無需使用PayPal帳戶,並且我正嘗試使用自適應付款流程以允許「以客人購買」的流量。我試圖使用PayPal默認流量(不是其餘api),因爲我不想擔心處理信用卡數據,並必須將我的網站設計爲符合PCI標準。MVC貝寶自適應付款

所以用這個escenario這裏是我使用的是什麼:

設置網頁使用燈箱調用嵌入式支付流程

  • 由於這種支付流程需要將生成付款密鑰,我使用這個鏈接上找到的代碼:

https://github.com/paypal/rest-api-sdk-dotnet/tree/master/Samples/RestApiSample

- 所以在我的MVC我有一個生成順序的頁面,它會調用Helper方法來獲取paykey。這裏是最相關的一個:

public static string GetPayKey(DummyPurchase purchase) 
    { 
     ReceiverList receiverList = new ReceiverList(); 
     receiverList.receiver = new List<Receiver>(); 
     //(Required) Amount to be paid to the receiver 
     string[] amt = new string[1] { purchase.TotalPrice.ToString() }; 
     // Receiver's email address. This address can be unregistered with paypal.com. 
     // If so, a receiver cannot claim the payment until a PayPal account is linked 
     // to the email address. The PayRequest must pass either an email address or a phone number. 
     // Maximum length: 127 characters 
     string[] receiverEmail = new string[1] { purchase.StoreId.ToString() }; 
     string cancelUrl = ConfigurationHelper<string>.GetKeyValue(Constants.PAYPAL_CANCEL_URL); 
     string returnUrl = ConfigurationHelper<string>.GetKeyValue(Constants.PAYPAL_RETURN_URL); 
     string currency = ConfigurationHelper<string>.GetKeyValue(Constants.PAYPAL_CURRENCY_CODE); 

     //Generate Receivers list 
     for (int i = 0; i < amt.Length; i++) 
     { 
      Receiver rec = new Receiver(Convert.ToDecimal(amt[i])); 
      if (receiverEmail[i] != string.Empty) 
      { 
       rec.email = receiverEmail[i]; 
      } 

      receiverList.receiver.Add(rec); 
     } 

     PayRequest request = new PayRequest(new RequestEnvelope("en_US"), "PAY", 
          cancelUrl, currency, 
          receiverList, returnUrl); 




     //call the service 
     AdaptivePaymentsService service = null; 
     PayResponse response = null; 
     try 
     { 
      // (https://github.com/paypal/sdk-core-dotnet/wiki/SDK-Configuration-Parameters) 
      Dictionary<string, string> configurationMap = GetAcctAndConfig(); 

      // Creating service wrapper object to make an API call and loading 
      // configuration map for your credentials and endpoint 
      service = new AdaptivePaymentsService(configurationMap); 

      response = service.Pay(request); 
     } 
     catch (Exception ex) 
     { 
      Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex)); 
      return ""; 
     } 

     Dictionary<string, string> responseValues = new Dictionary<string, string>(); 
     string redirectUrl = null; 
     if (!(response.responseEnvelope.ack == AckCode.FAILURE) && 
      !(response.responseEnvelope.ack == AckCode.FAILUREWITHWARNING)) 
     { 

      return response.payKey; 
     } 
     return ""; 
    } 

- 後我得到這個關鍵,我從具有形式的API手冊中規定,具有paykey字符串作爲模型對這一觀點的另一個觀點得到HTML。

@model string 
<h2>ConfirmCheckout</h2> 
<script src="https://www.paypalobjects.com/js/external/dg.js"> 
</script> 
<form action="https://www.paypal.com/webapps/adaptivepayment/flow/pay" 
target="PPDGFrame"> 
<input id="type" type="hidden" name="expType" value="light"> 
<input id="paykey" type="hidden" name="paykey" value="@Model"> 
<input type="submit" id="submitBtn" value="Pay with PayPal"> 
</form> 

- 後視圖渲染,我調用JavaScript代碼來啓動流程:

var dgFlow = new PAYPAL.apps.DGFlow({ trigger: 'submitBtn' }); 

-The流完美的作品,我得到呈現這種形式在一個有效的付款密鑰。但是當我點擊這個按鈕(用paykey提交表單提交按鈕)時,我得到2個不同的錯誤。這是最常見的一種:

此交易已被批准。請訪問您的PayPal賬戶概覽以查看詳細信息。

- 但有時我收到「您的付款會話已過期」錯誤。

我有2個問題:

  • 是否有人知道如何解決這些錯誤?
  • 我使用clasic API,因爲適應性付款的客人付款流程需要PayKey啓動流程(爲了避免安全和PCI配合問題)。我沒有在Paypal REST API上找到可以獲得相同PayKey的方法。有什麼方法可以獲得這些密鑰?

非常感謝

+0

您可以查看這裏的例子: http://highlightsolutions.com/shoppingcart 商店下拉列表的第一個選項是工作的權利立即生成一個支付鑰匙。 –

回答

3

嗯,這真的很尷尬......但真正的問題是形式後操作的URL。我有

<form action="https://www.paypal.com/webapps/adaptivepayment/flow/pay" target="PPDGFrame"> 

這是生產環節。而且我不會去現場的是,我使用貝寶API憑據的帳戶的SANbox,所以真正的表單動作應該是:

<form action="https://www.sandbox.paypal.com/webapps/adaptivepayment/flow/pay" target="PPDGFrame"> 

咄!那麼希望這可以幫助另一個人犯同樣的錯誤。

非常感謝@Andrew安格爾

+0

感謝1000次 - 做出同樣的錯誤! –

0

這聽起來像你發送你的付款請求一個預先批准的關鍵。當你這樣做時,不需要重定向到PayPal。這就是關鍵所在......您已通過Preapproval密鑰獲得批准,因此,當您提交包含預先批准密鑰的Pay請求時,付款將立即生效(只要預批准資料仍然有效)。

因此,當您進行重定向時,它會告訴您發生了什麼事......交易已經處理完畢。再次,不需要重定向。

整個問題的關鍵是要能夠在任何需要進行任何重定向或進一步批准的情況下,使用應用程序中的預批准配置文件觸發付款。一旦您擁有該預付款密鑰,您就可以以任何您想要的方式觸發付款。例如,您可能希望按用戶收費,並在登錄到您的網站時觸發付款,但您不希望他們每次都必須批准付款。支付可能會在後臺通過Pay API與預先批准的密鑰一起發生,但用戶不會被打斷。

希望有所幫助。

+0

對不起,我是Paypal編碼新手。我在哪裏指定我的代碼的預批准密鑰?我已經讀過關於Preapproval鍵(以及API文檔中指定的那些標記爲可選)的唯一方法是第二種形式......重定向到[https://www.paypal.com/webapps/adaptivepayment/flow/pay]但是我沒有呈現這個控件'' –

+0

我必須查看正在生成的原始API請求字符串。 –