Exemple #1
0
 public function payOrder($orderData = array())
 {
     if (strlen($this->token) > 0) {
         global $APPLICATION;
         $url = "https://api-3t." . $this->domain . "paypal.com/nvp";
         $arFields = array("METHOD" => "GetExpressCheckoutDetails", "VERSION" => $this->version, "USER" => $this->username, "PWD" => $this->pwd, "SIGNATURE" => $this->signature, "TOKEN" => $this->token, "buttonsource" => "Bitrix_Cart");
         $ht = new \Bitrix\Main\Web\HttpClient(array("version" => "1.1"));
         if ($res = $ht->post($url, $arFields)) {
             $result = $this->parseResult($res);
             if ($result["ACK"] == "Success" && in_array($result["CHECKOUTSTATUS"], array("PaymentActionNotInitiated"))) {
                 $arFields["METHOD"] = "DoExpressCheckoutPayment";
                 $arFields["PAYERID"] = $this->payerId;
                 $arFields["PAYMENTACTION"] = "Sale";
                 $arFields["PAYMENTREQUEST_0_AMT"] = number_format($this->orderAmount, 2, ".", "");
                 $arFields["PAYMENTREQUEST_0_CURRENCYCODE"] = $this->currency;
                 $arFields["PAYMENTREQUEST_0_DESC"] = "Order #" . $this->orderId;
                 $arFields["PAYMENTREQUEST_0_NOTETEX"] = "Order #" . $this->orderId;
                 $arFields["PAYMENTREQUEST_0_INVNUM"] = $this->orderId;
                 if (DoubleVal($this->deliveryAmount) > 0) {
                     $arFields["PAYMENTREQUEST_0_SHIPPINGAMT"] = number_format($this->deliveryAmount, 2, ".", "");
                 }
                 $orderProps = $this->getProps();
                 if (!empty($orderProps)) {
                     $arFields["PAYMENTREQUEST_0_SHIPTONAME"] = $APPLICATION->ConvertCharset($orderProps["PP_SOURCE"]["PAYMENTREQUEST_0_SHIPTONAME"], SITE_CHARSET, "utf-8");
                     $arFields["PAYMENTREQUEST_0_SHIPTOSTREET"] = $APPLICATION->ConvertCharset($orderProps["PP_SOURCE"]["PAYMENTREQUEST_0_SHIPTOSTREET"], SITE_CHARSET, "utf-8");
                     $arFields["PAYMENTREQUEST_0_SHIPTOSTREET2"] = $APPLICATION->ConvertCharset($orderProps["PP_SOURCE"]["PAYMENTREQUEST_0_SHIPTOSTREET2"], SITE_CHARSET, "utf-8");
                     $arFields["PAYMENTREQUEST_0_SHIPTOCITY"] = $APPLICATION->ConvertCharset($orderProps["PP_SOURCE"]["PAYMENTREQUEST_0_SHIPTOCITY"], SITE_CHARSET, "utf-8");
                     $arFields["PAYMENTREQUEST_0_SHIPTOSTATE"] = $APPLICATION->ConvertCharset($orderProps["PP_SOURCE"]["PAYMENTREQUEST_0_SHIPTOSTATE"], SITE_CHARSET, "utf-8");
                     $arFields["PAYMENTREQUEST_0_SHIPTOZIP"] = $orderProps["PP_SOURCE"]["PAYMENTREQUEST_0_SHIPTOZIP"];
                     $arFields["PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE"] = $APPLICATION->ConvertCharset($orderProps["PP_SOURCE"]["PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE"], SITE_CHARSET, "utf-8");
                 }
                 if (!empty($orderData["BASKET_ITEMS"])) {
                     $arFields["PAYMENTREQUEST_0_ITEMAMT"] = number_format($this->orderAmount - $this->deliveryAmount, 2, ".", "");
                     foreach ($orderData["BASKET_ITEMS"] as $k => $val) {
                         $arFields["L_PAYMENTREQUEST_0_NAME" . $k] = $APPLICATION->ConvertCharset($val["NAME"], SITE_CHARSET, "utf-8");
                         $arFields["L_PAYMENTREQUEST_0_AMT" . $k] = number_format($val["PRICE"], 2, ".", "");
                         $arFields["L_PAYMENTREQUEST_0_QTY" . $k] = $val["QUANTITY"];
                         $arFields["L_PAYMENTREQUEST_0_NUMBER" . $k] = $val["PRODUCT_ID"];
                     }
                 }
                 if (strlen($this->notifyUrl) > 0) {
                     $arFields["PAYMENTREQUEST_0_NOTIFYURL"] = $this->notifyUrl;
                 }
                 if ($res2 = $ht->Post($url, $arFields)) {
                     $result2 = $this->parseResult($res2);
                     if ($result2["ACK"] == "Success" && in_array($result2["PAYMENTINFO_0_PAYMENTSTATUS"], array("Completed"))) {
                         CSaleOrder::PayOrder($this->orderId, "Y");
                         $strPS_STATUS_MESSAGE = "";
                         $strPS_STATUS_MESSAGE .= "Name: " . $result["FIRSTNAME"] . " " . $result["LASTNAME"] . "; ";
                         $strPS_STATUS_MESSAGE .= "Email: " . $result["EMAIL"] . "; ";
                         $strPS_STATUS_DESCRIPTION = "";
                         $strPS_STATUS_DESCRIPTION .= "Payment status: " . $result2["PAYMENTINFO_0_PAYMENTSTATUS"] . "; ";
                         $strPS_STATUS_DESCRIPTION .= "Payment sate: " . $result2["PAYMENTINFO_0_ORDERTIME"] . "; ";
                         $arOrderFields = array("PS_STATUS" => "Y", "PS_STATUS_CODE" => "-", "PS_STATUS_DESCRIPTION" => $strPS_STATUS_DESCRIPTION, "PS_STATUS_MESSAGE" => $strPS_STATUS_MESSAGE, "PS_SUM" => $result2["PAYMENTINFO_0_AMT"], "PS_CURRENCY" => $result2["PAYMENTINFO_0_CURRENCYCODE"], "PS_RESPONSE_DATE" => ConvertTimeStamp(false, "FULL"), "PAY_VOUCHER_NUM" => $result2["PAYMENTINFO_0_TRANSACTIONID"], "PAY_VOUCHER_DATE" => ConvertTimeStamp(false, "FULL"));
                     } else {
                         $strPS_STATUS_MESSAGE = "";
                         $strPS_STATUS_MESSAGE .= "Name: " . $result["FIRSTNAME"] . " " . $result["LASTNAME"] . "; ";
                         $strPS_STATUS_MESSAGE .= "Email: " . $result["EMAIL"] . "; ";
                         $strPS_STATUS_DESCRIPTION = "";
                         $strPS_STATUS_DESCRIPTION .= "Payment status: " . $result2["PAYMENTINFO_0_PAYMENTSTATUS"] . "; ";
                         $strPS_STATUS_DESCRIPTION .= "Pending reason: " . $result2["PAYMENTINFO_0_PENDINGREASON"] . "; ";
                         $strPS_STATUS_DESCRIPTION .= "Payment sate: " . $result2["PAYMENTINFO_0_ORDERTIME"] . "; ";
                         $arOrderFields = array("PS_STATUS" => "N", "PS_STATUS_CODE" => $result2["PAYMENTINFO_0_PAYMENTSTATUS"], "PS_STATUS_DESCRIPTION" => $strPS_STATUS_DESCRIPTION, "PS_STATUS_MESSAGE" => $strPS_STATUS_MESSAGE, "PS_SUM" => $result2["PAYMENTINFO_0_AMT"], "PS_CURRENCY" => $result2["PAYMENTINFO_0_CURRENCYCODE"], "PS_RESPONSE_DATE" => ConvertTimeStamp(false, "FULL"), "PAY_VOUCHER_NUM" => $result2["PAYMENTINFO_0_TRANSACTIONID"], "PAY_VOUCHER_DATE" => ConvertTimeStamp(false, "FULL"));
                     }
                     CSaleOrder::Update($this->orderId, $arOrderFields);
                 }
             }
         }
     }
 }