一佩捞、準(zhǔn)備信息
1.merchantID:商家ID楣导,商戶號(hào)烦租,組織ID
2.apiKeyID:密鑰
3.secretKey:共享密鑰
4.runEnv:
????(1)沙盒(測(cè)試)環(huán)境url:apitest.cybersource.com
????(2)正式環(huán)境url:api.cybersource.com
二豺鼻、修改SDK的配置類
cybersource-rest-samples-php-56/Resources/ExternalConfiguration.php沫勿,
__construct 方法
????// 將無參構(gòu)造挨约,改成有參構(gòu)造,傳遞四個(gè)重要參數(shù)
?????? function __construct($merchantID, $apiKeyID, $secretKey, $runEnv)????
??? {
???????$this->authType = "http_signature";???????? // http_signature/jwt方式
? ??????$this->merchantID = $merchantID;??? //商家ID产雹,商戶號(hào)诫惭,組織ID
???????$this->apiKeyID = $apiKeyID;??????? //密鑰
???????$this->secretKey = $secretKey;??????? //共享密鑰
???????// MetaKey configuration [Start]
???????$this->useMetaKey = false;
???????$this->portfolioID = "";
???????// MetaKey configuration [End]
???????$this->keyAlias = $merchantID;?//商家ID,商戶號(hào)蔓挖,組織ID
???????$this->keyPass = $merchantID;??//商家ID夕土,商戶號(hào),組織ID
???????$this->keyFilename = $merchantID;??//商家ID瘟判,商戶號(hào)怨绣,組織ID
???????$this->keyDirectory = "Resources/";
???????$this->runEnv = $runEnv;??? ?? //沙盒(測(cè)試)/正式環(huán)境的url
???????// new property has been added for user to configure the base path sothat request can route the API calls via Azure Management URL.
???????// Example: If intermediate url is https://manage.windowsazure.com thenin property input can be same url or manage.windowsazure.com.
???????$this->IntermediateHost ="https://manage.windowsazure.com";
???????//OAuth related config
???????$this->enableClientCert = false;
???????$this->clientCertDirectory = "Resources/";
???????$this->clientCertFile = "";
???????$this->clientCertPassword = "";
???????$this->clientId = "";
???????$this->clientSecret = "";
???????// New Logging
???????$this->enableLogging = true;
???????$this->debugLogFile = __DIR__ . DIRECTORY_SEPARATOR . "..". DIRECTORY_SEPARATOR . "Log" . DIRECTORY_SEPARATOR ."debugTest.log";
???????$this->errorLogFile = __DIR__ . DIRECTORY_SEPARATOR . "..". DIRECTORY_SEPARATOR . "Log" . DIRECTORY_SEPARATOR ."errorTest.log";
???????$this->logDateFormat = "Y-m-d\TH:i:s";
???????$this->logFormat = "[%datetime%] [%level_name%] [%channel%] :%message%\n";
???????$this->logMaxFiles = 3;
???????$this->logLevel = "debug";
???????$this->enableMasking = true;
???????$this->merchantConfigObject();
???????$this->merchantConfigObjectForIntermediateHost();
??? }
三、付款邏輯
?????? 卡類型荒适、卡號(hào)梨熙、卡過期年月开镣、卡的cvn刀诬,可以提供一個(gè)form進(jìn)行輸入。
public function getCheckoutUrlIFrame(){
???????$card_type = ‘’;?????? //卡類型(001:Visa邪财;002:Mastercard)
???????$card_number = ‘’;??????? //卡號(hào)(16位數(shù)字)
???????$card_expiry_date = ‘’;?? //卡過期年月(格式:MM-yyyy)
???????$card_cvn = ‘’;??????? //卡的cvn(3位/4位數(shù)字)
???????// sdk部分
???????$return = $this->sdkPayment($card_type, $card_number,$card_expiry_date, $card_cvn);
???????$params = $return['params'];
???????$requestId = $return['requestId'];
???????$responseCode = $return['responseCode'];
???????$return_code = $return['return_code'];
???????$response = $return['response'];
???????$status = $return['status'];
???????$errmsg = $return['errmsg'];
???????// sdk部分陕壹,end
???????//請(qǐng)求信息+部分響應(yīng)信息提取出來
???????$params['requestId'] = $requestId;????????? //成功時(shí)的響應(yīng)信息是對(duì)象,json后為空树埠,無法獲取
???????//$params['transactionId'] = $transactionId;? //成功時(shí)的響應(yīng)信息是對(duì)象糠馆,json后為空,無法獲取
//???????$params['responseCode'] = $responseCode;??? //成功時(shí)的響應(yīng)信息是對(duì)象怎憋,json后為空又碌,無法獲取
//???????$params['return_code'] = $return_code;????? //失敗時(shí)的響應(yīng)信息九昧,沒有code
???????//請(qǐng)求信息+所有響應(yīng)信息
???????$params['response'] = $response;
???????$params['errmsg'] = $errmsg;
???????//只有響應(yīng)信息
???????$responseParams['card_type'] = $card_type;????? //加上卡類型
???????$responseParams['requestId'] = $requestId;????????? //成功時(shí)的響應(yīng)信息是對(duì)象,json后為空毕匀,無法獲取
???????//$params['transactionId'] = $transactionId;? //成功時(shí)的響應(yīng)信息是對(duì)象铸鹰,json后為空,無法獲取
???????$responseParams['responseCode'] = $responseCode;??? //成功時(shí)的響應(yīng)信息是對(duì)象皂岔,json后為空蹋笼,無法獲取
???????$responseParams['return_code'] = $return_code;????? //失敗時(shí)的響應(yīng)信息,沒有code
???????$responseParams['status'] = $status;??????????? //響應(yīng)的狀態(tài)
???????//所有響應(yīng)信息
???????$responseParams['response'] = $response;
???????$responseParams['errmsg'] = $errmsg;
? ? ? ? //保存響應(yīng)信息 $responseParams
? ? ? ? ……
? ? ? ?//保存到數(shù)據(jù)庫時(shí)躁垛,進(jìn)行處理(為了安全剖毯,不保存具體的卡信息)
???????$params['card_number'] = '***';
???????$params['card_expiry_date'] = '***';
???????$params['card_cvn'] = '***';
? ? ? ? //保存請(qǐng)求+響應(yīng)信息 $params
? ? ? ? ……
???????if ($responseCode == 100 && $status == 'AUTHORIZED') {
???????????return ‘付款成功的url’;
???????} else {
???????????return ‘付款失敗的url’;
???????}
??? }
四、封裝SDK付款邏輯
public function sdkPayment($card_type, $card_number, $card_expiry_date, $card_cvn){
???????// sdk接口部分
???????$flag = "true";
???????if (isset($flag) && $flag == "true") {
???????????$capture = true;
???????} else {
???????????$capture = false;
???????}
???????$clientReferenceInformationArr = [
???????????"code" => ‘’??? //每個(gè)交易的唯一的商家生成的訂單參考或跟蹤編號(hào)教馆⊙纺保可以從數(shù)據(jù)庫中獲取。
???????];
???????$params = $clientReferenceInformationArr;?? //記錄提交參數(shù)
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsClientReferenceInformation.php');
???????$clientReferenceInformation = new\CyberSource\Model\Ptsv2paymentsClientReferenceInformation($clientReferenceInformationArr);
???????$processingInformationArr = [
???????????"capture" => $capture
???????];
???????$params = array_merge($params, $processingInformationArr);? //記錄提交參數(shù)
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsProcessingInformation.php');
???????$processingInformation = new\CyberSource\Model\Ptsv2paymentsProcessingInformation($processingInformationArr);
???????$params['card_type'] = $card_type;?????//記錄提交參數(shù)
???????$params['card_number'] = $card_number;????????? //重新支付時(shí)使用(為了安全活玲,建議重新支付時(shí)涣狗,重新輸入,不要保存具體內(nèi)容到數(shù)據(jù)庫)
???????$params['card_expiry_date'] = $card_expiry_date;???? //重新支付時(shí)使用(為了安全舒憾,建議重新支付時(shí)镀钓,重新輸入,不要保存具體內(nèi)容到數(shù)據(jù)庫)
???????$params['card_cvn'] = $card_cvn;????//重新支付時(shí)使用(為了安全镀迂,建議重新支付時(shí)丁溅,重新輸入,不要保存具體內(nèi)容到數(shù)據(jù)庫)
???????$card_expiry_date_arr = explode('-', $card_expiry_date);
???????$paymentInformationCardArr = [
???????????"number" => $card_number,
???????????"expirationMonth" => current($card_expiry_date_arr),
???????????"expirationYear" => end($card_expiry_date_arr),
???????????"type" => $card_type,
???????????"securityCode" => $card_cvn???? //可以不加該字段探遵,可以為空
???????];
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsPaymentInformationCard.php');
???????$paymentInformationCard = new\CyberSource\Model\Ptsv2paymentsPaymentInformationCard($paymentInformationCardArr);
???????$paymentInformationArr = [
???????????"card" => $paymentInformationCard
???????];
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsPaymentInformation.php');
???????$paymentInformation = new\CyberSource\Model\Ptsv2paymentsPaymentInformation($paymentInformationArr);
???????$orderInformationAmountDetailsArr = [
???????????"totalAmount" => ‘’,???????????? //付款金額
???????????"currency" => "USD"?????????? //測(cè)試環(huán)境窟赏,用USD;正式環(huán)境箱季,可以換成動(dòng)態(tài)的貨幣類型
???????];
???????$params = array_merge($params, $orderInformationAmountDetailsArr);? //記錄提交參數(shù)
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsOrderInformationAmountDetails.php');
???????$orderInformationAmountDetails = new\CyberSource\Model\Ptsv2paymentsOrderInformationAmountDetails($orderInformationAmountDetailsArr);
? ? ? ?//可以輸入涯穷,也可以從數(shù)據(jù)庫中獲取
???????$orderInformationBillToArr = [
???????????"firstName" => ‘’,???//姓
???????????"lastName" => ‘’,???//名
???????????"address1" => ‘’,???? //地址
???????????"locality" => ‘’, ?????? //城市
???????????//"administrativeArea" => "CA",
???????????"postalCode" => '',????? //郵編,默認(rèn)可以給000000
???????????"country" => ‘’,? //國(guó)家代碼
???????????"email" => ‘’,?? //郵箱
???????????"phoneNumber" => ‘’??? //手機(jī)號(hào)
???????];
???????$params = array_merge($params, $orderInformationBillToArr); //記錄提交參數(shù)
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsOrderInformationBillTo.php');
???????$orderInformationBillTo = new\CyberSource\Model\Ptsv2paymentsOrderInformationBillTo($orderInformationBillToArr);
???????$orderInformationArr = [
???????????"amountDetails" => $orderInformationAmountDetails,
???????????"billTo" => $orderInformationBillTo
???????];
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsOrderInformation.php');
???????$orderInformation = new\CyberSource\Model\Ptsv2paymentsOrderInformation($orderInformationArr);
???????$requestObjArr = [
???????????"clientReferenceInformation" =>$clientReferenceInformation,
???????????"processingInformation" => $processingInformation,
???????????"paymentInformation" => $paymentInformation,
???????????"orderInformation" => $orderInformation
?? ?????];
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/CreatePaymentRequest.php');
???????$requestObj = new\CyberSource\Model\CreatePaymentRequest($requestObjArr);
???????include('cybersource-rest-samples-php-56/Resources/ExternalConfiguration.php');
???????$merchantID = ‘’;?????????? //商家ID
???????$apiKeyID = ‘’;??????? // key
???????$apiSecretKey = ‘’;???????? //secret key
???????$runEnv = ‘’;?????????? // cybersource的運(yùn)行環(huán)境url藏雏,測(cè)試(沙盒)/正式環(huán)境
???????$commonElement = new \CyberSource\ExternalConfiguration($merchantID, $apiKeyID, $apiSecretKey, $runEnv);
???????$config = $commonElement->ConnectionHost();
???????$merchantConfig = $commonElement->merchantConfigObject();
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/ApiClient.php');
???????$api_client = new \CyberSource\ApiClient($config, $merchantConfig);
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Api/PaymentsApi.php');
???????$api_instance = new \CyberSource\Api\PaymentsApi($api_client);
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/ApiException.php');
???????try {
???????????$apiResponse = $api_instance->createPayment($requestObj);
???????????$requestId = $apiResponse[0]['id']; //客戶id拷况,請(qǐng)求id
???????????//$transactionId =$apiResponse[0]['processorInformation']['transactionId'];??? //每個(gè)請(qǐng)求,值都一樣
???????????$responseCode = $apiResponse[0]['processorInformation']['responseCode'];
???????????$status = $apiResponse[0]['status'];
???????????$return_code = $apiResponse[1];
???????????//獲取付款成功的 所有響應(yīng)信息(里面的對(duì)象要轉(zhuǎn)數(shù)組)
???????????$response = $apiResponse;
???????????$response[0] = $this->getPaymentResponseParams($apiResponse);
???????????$errmsg = ($responseCode == 100 && $status == 'AUTHORIZED') ? '': ‘自定義錯(cuò)誤信息’;?? //錯(cuò)誤信息
???????} catch (\Cybersource\ApiException $e) {
???????????$errorCode = $e->getCode();
???????????$requestId = $e->getResponseBody()->id; //客戶id掘殴,請(qǐng)求id
???????????//$transactionId = '';
???????????$responseCode = $e->getCode();
???????????$status = $e->getResponseBody()->status;
???????????$return_code = $errorCode;
???????????$response = $e->getResponseBody();?//所有響應(yīng)信息
???????????$response->responseCode = $e->getCode();
???????????$errmsg = $e->getMessage();????//錯(cuò)誤信息
???????}
???????// sdk接口部分赚瘦,end
???????$return['params'] = $params;
???????$return['requestId'] = $requestId;
???????$return['responseCode'] = $responseCode;
?? ?????$return['status'] = $status;
???????$return['return_code'] = $return_code;
???????$return['response'] = $response;
???????$return['errmsg'] = $errmsg;
???????return $return;
??? }
五、獲取 付款成功 的響應(yīng)參數(shù)(對(duì)象轉(zhuǎn)數(shù)組)-非托管集成方式
private function getPaymentResponseParams($apiResponse){
???????$responseArr['links=self=href'] = '';
???????$responseArr['links=self=method'] = '';
???????$responseArr['links=reversal'] = '';
???????$responseArr['links=capture'] = '';
???????$responseArr['links=customer'] = '';
???????$responseArr['links=paymentInstrument'] = '';
???????$responseArr['links=shippingAddress'] = '';
???????$responseArr['links=instrumentIdentifier'] = '';
???????$responseArr['id'] = ''; //客戶id奏寨,請(qǐng)求id
???????$responseArr['submitTimeUtc'] = '';
???????$responseArr['status'] = '';
???????$responseArr['reconciliationId'] = '';
???????$responseArr['errorInformation'] = '';
???????$responseArr['clientReferenceInformation=code'] = '';? //訂單編號(hào)code
???????$responseArr['clientReferenceInformation=submitLocalDateTime'] = '';
???????$responseArr['clientReferenceInformation=ownerMerchantId'] = '';
???????$responseArr['processingInformation'] = '';
???????$responseArr['processorInformation=authIndicator'] = '';
???????$responseArr['processorInformation=approvalCode']= '';
???????$responseArr['processorInformation=cardReferenceData'] = '';
???????$responseArr['processorInformation=transactionId'] = '';
???????$responseArr['processorInformation=networkTransactionId'] = '';
???????$responseArr['processorInformation=responseCode'] = '';
???????$responseArr['processorInformation=responseCodeSource'] = '';
???????$responseArr['processorInformation=responseDetails'] = '';
???????$responseArr['processorInformation=responseCategoryCode'] = '';
???????$responseArr['processorInformation=forwardedAcquirerCode'] = '';
???????$responseArr['processorInformation=avs=code'] = '';
???????$responseArr['processorInformation=avs=codeRaw'] = '';
???????$responseArr['processorInformation=cardVerification'] = '';
???????$responseArr['processorInformation=merchantAdvice'] = '';
???????$responseArr['processorInformation=electronicVerificationResults'] = '';
???????$responseArr['processorInformation=achVerification'] = '';
???????$responseArr['processorInformation=customer'] = '';
???????$responseArr['processorInformation=consumerAuthenticationResponse'] ='';
???????$responseArr['processorInformation=systemTraceAuditNumber'] = '';
???????$responseArr['processorInformation=paymentAccountReferenceNumber'] = '';
???????$responseArr['processorInformation=transactionIntegrityCode'] = '';
???????$responseArr['processorInformation=amexVerbalAuthReferenceNumber'] = '';
???????$responseArr['processorInformation=masterCardServiceCode'] = '';
???????$responseArr['processorInformation=masterCardServiceReplyCode'] = '';
???????$responseArr['processorInformation=masterCardAuthenticationType'] = '';
???????$responseArr['processorInformation=name'] = '';
???????$responseArr['processorInformation=routing'] = '';
???????$responseArr['processorInformation=merchantNumber'] = '';
???????$responseArr['processorInformation=retrievalReferenceNumber'] = '';
???????$responseArr['processorInformation=paymentUrl'] = '';
???????$responseArr['processorInformation=completeUrl'] = '';
???????$responseArr['processorInformation=signature'] = '';
???????$responseArr['processorInformation=publicKey'] = '';
???????$responseArr['issuerInformation'] = '';
//???????????$responseArr['paymentAccountInformation=card=suffix']= '';
//???????????$responseArr['paymentAccountInformation=card=expirationMonth'] = '';
//???????????$responseArr['paymentAccountInformation=card=expirationYear'] = '';
//??????????? $responseArr['paymentAccountInformation=card=type']= '';
//???????????$responseArr['paymentAccountInformation=card=prefix'] = '';
//???????????$responseArr['paymentAccountInformation=card=hashedNumber'] = '';
//???????????$responseArr['paymentInformation=card=suffix'] = '';
//???????????$responseArr['paymentInformation=card=expirationMonth'] = '';
//???????????$responseArr['paymentInformation=card=expirationYear'] = '';
//???????????$responseArr['paymentInformation=card=type'] = '';
//??????????? $responseArr['paymentInformation=card=prefix']= '';
//???????????$responseArr['paymentInformation=card=hashedNumber'] = '';
//???????????$responseArr['paymentInformation=tokenizedCard=prefix'] = '';
//???????????$responseArr['paymentInformation=tokenizedCard=suffix'] = '';
//???????????$responseArr['paymentInformation=tokenizedCard=type'] = '';
//???????????$responseArr['paymentInformation=tokenizedCard=assuranceLevel'] = '';
//???????????$responseArr['paymentInformation=tokenizedCard=expirationMonth'] = '';
//???????????$responseArr['paymentInformation=tokenizedCard=expirationYear']= '';
//???????????$responseArr['paymentInformation=tokenizedCard=requestorId'] = '';
???????$responseArr['paymentInformation=accountFeatures'] = '';
???????$responseArr['paymentInformation=bank'] = '';
???????$responseArr['paymentInformation=customer'] = '';
???????$responseArr['paymentInformation=paymentInstrument'] = '';
???????$responseArr['paymentInformation=instrumentIdentifier'] = '';
???????$responseArr['paymentInformation=shippingAddress'] = '';
???????$responseArr['paymentInformation=scheme'] = '';
???????$responseArr['paymentInformation=bin'] = '';
???????$responseArr['paymentInformation=accountType'] = '';
???????$responseArr['paymentInformation=issuer'] = '';
???????$responseArr['paymentInformation=binCountry'] = '';
???????$responseArr['paymentInsightsInformation'] = '';
???????$responseArr['orderInformation=amountDetails=totalAmount'] = '';
???????$responseArr['orderInformation=amountDetails=authorizedAmount'] = '';
???????$responseArr['orderInformation=amountDetails=currency'] = '';
???????$responseArr['orderInformation=invoiceDetails'] = '';
???????$responseArr['orderInformation=rewardPointsDetails'] = '';
???????$responseArr['pointOfSaleInformation=emv'] = '';
???????$responseArr['pointOfSaleInformation=amexCapnData'] = '';
???????$responseArr['pointOfSaleInformation=terminalId'] = '';
???????$responseArr['installmentInformation'] = '';
???????$responseArr['tokenInformation'] = '';
???????$responseArr['buyerInformation'] = '';
???????$responseArr['riskInformation'] = '';
???????$responseArr['consumerAuthenticationInformation'] = '';
???????$responseArrR = [];
???????foreach ($responseArr as $k => $v) {
???????????$kArr = explode('=', $k);
???????????if (count($kArr) == 1) {
??????????????? $k1 = current($kArr);
??????????????? $responseArrR[$k1] =$apiResponse[0][$k1];
???????????}
???????????if (count($kArr) == 2) {
??????????????? $k1 = current($kArr);
??????????????? $k2 = end($kArr);
??????????????? $responseArrR[$k1][$k2] =$apiResponse[0][$k1][$k2];
???????????}
???????????if (count($kArr) == 3) {
??????????????? $k1 = current($kArr);
??????????????? $k2 = $kArr[1];
??????????????? $k3 = end($kArr);
?????????????? ?$responseArrR[$k1][$k2][$k3] =$apiResponse[0][$k1][$k2][$k3];
???????????}
???????}
???????return $responseArrR;
??? }
六起意、付款成功的url,請(qǐng)求的方法
???public function verifyReturn()
??? {
???????$card_type = ‘’;??? //從數(shù)據(jù)表中病瞳,獲取之前保存的響應(yīng)數(shù)據(jù)card_type
???????$requestId = ‘’;??????????? //從數(shù)據(jù)表中揽咕,獲取之前保存的響應(yīng)數(shù)據(jù)客戶id悲酷,請(qǐng)求id
???????//$transaction_id = ‘’;??? //從數(shù)據(jù)表中,獲取之前保存的響應(yīng)數(shù)據(jù)transactionId
???????$response_code = ‘’;??? //從數(shù)據(jù)表中亲善,獲取之前保存的響應(yīng)數(shù)據(jù)responseCode
???????//$return_code = ‘’;??? //從數(shù)據(jù)表中舔涎,獲取之前保存的響應(yīng)數(shù)據(jù)return_code
???????$status = ‘’;????????? //從數(shù)據(jù)表中,獲取之前保存的響應(yīng)數(shù)據(jù)獲取狀態(tài)
???????$errmsg = ‘’;????????? //從數(shù)據(jù)表中逗爹,獲取之前保存的響應(yīng)數(shù)據(jù)錯(cuò)誤信息
???????//驗(yàn)證返回的狀態(tài)碼
???????if ($response_code == 100 && $status == 'AUTHORIZED') {
???????????????????//付款成功的處理邏輯
???????????????????? ……
???????????????????? //付款成功的返回信息(可自定義)
???????????????????return ['res' => true, 'msg' => $errmsg];
???????}
????????????? //付款失敗的返回信息(可自定義)
???????????return ['res' => false, 'msg' => $errmsg];
??? }
七亡嫌、付款失敗的url,請(qǐng)求的方法
?????? 與付款成功的url 類似掘而,自定義處理邏輯即可挟冠,一般是跳轉(zhuǎn)展示付款失敗的頁面。