一、準(zhǔn)備信息
1.merchantID:商家ID,商戶(hù)號(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的配置類(lèi)
cybersource-rest-samples-php-56/Resources/ExternalConfiguration.php,
__construct 方法
????????// 將無(wú)參構(gòu)造,改成有參構(gòu)造,傳遞四個(gè)重要參數(shù)
?????? function __construct($merchantID, $apiKeyID, $secretKey, $runEnv)????
??? {
???????$this->authType = "http_signature";???????? // http_signature/jwt方式
? ??????$this->merchantID = $merchantID;??? //商家ID,商戶(hù)號(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ù)號(hào),組織ID
???????$this->keyPass = $merchantID;??//商家ID,商戶(hù)號(hào),組織ID
???????$this->keyFilename = $merchantID;??//商家ID,商戶(hù)號(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();
??? }
三、退款邏輯
public function refund()
??? {
???????$id = ‘’;?? //托管集成付款:CyberSource回調(diào)請(qǐng)求時(shí),有個(gè)transaction_id(事務(wù)id,請(qǐng)求id);SDK方式付款:客戶(hù)id,請(qǐng)求id,在付款后的響應(yīng)信息里會(huì)有id;要保存到數(shù)據(jù)庫(kù)
???????$code = ‘’;?????? //自己平臺(tái)的訂單編號(hào)
???????$totalAmount = 0;?????? //退款金額(全額,或者部分金額)
???????$requestParams['refundId'] = $id;??//記錄請(qǐng)求參數(shù)
???????$clientReferenceInformationArr = [
???????????"code" => $code
???????];
???????$requestParams = array_merge($requestParams,$clientReferenceInformationArr);??? //記錄請(qǐng)求參數(shù)
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsidrefundsClientReferenceInformation.php');???????? //引入該文件
???????$clientReferenceInformation = new\CyberSource\Model\Ptsv2paymentsidrefundsClientReferenceInformation($clientReferenceInformationArr);
???????$orderInformationAmountDetailsArr = [
???????????"totalAmount" => $totalAmount,
???????????"currency" => "USD"?????????? //測(cè)試環(huán)境,用USD;正式環(huán)境,可以換成動(dòng)態(tài)的貨幣類(lèi)型
???????];
???????$requestParams = array_merge($requestParams,$orderInformationAmountDetailsArr);??? //記錄請(qǐng)求參數(shù)
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsidcapturesOrderInformationAmountDetails.php');
???????$orderInformationAmountDetails = new\CyberSource\Model\Ptsv2paymentsidcapturesOrderInformationAmountDetails($orderInformationAmountDetailsArr);
???????$orderInformationArr = [
???????????"amountDetails" => $orderInformationAmountDetails
???????];
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsidrefundsOrderInformation.php');
???????$orderInformation = new\CyberSource\Model\Ptsv2paymentsidrefundsOrderInformation($orderInformationArr);
???????$requestObjArr = [
???????????"clientReferenceInformation" =>$clientReferenceInformation,
???????????"orderInformation" => $orderInformation
???????];
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/RefundPaymentRequest.php');
???????$requestObj = new\CyberSource\Model\RefundPaymentRequest($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/RefundApi.php');
???????$api_instance = new \CyberSource\Api\RefundApi($api_client);
???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/ApiException.php');
???????try {
???????????$apiResponse = $api_instance->refundPayment($requestObj, $id);
//??????????? $requestId = $apiResponse[0]['id'];
???????????$responseCode =$apiResponse[0]['processorInformation']['responseCode'];??? // 100
//??????????? $returnCode = $apiResponse[1];? // 201
???????????$message = '';
//??????????? $details = '';
???????????//獲取退款成功的 所有響應(yīng)信息(里面的對(duì)象要轉(zhuǎn)數(shù)組)
???????????$response = $apiResponse;
???????????$response[0] = $this->getRefundResponseParams($apiResponse);
???????} catch (\Cybersource\ApiException $e) {
//???????????$requestId =$e->getResponseBody()->id;
???????????$responseCode = $e->getCode();?// code
//??????????? $returnCode = $errorCode;?? // 400
???????????$message = $e->getResponseBody()->message;
//??????????? $details =$e->getResponseBody()->details;
???????????$response = $e->getResponseBody();?//所有響應(yīng)信息
???????????$response->responseCode = $e->getCode();??? //加上code
???????}
//???????$responseParams['requestId'] = $requestId;
//???????$responseParams['responseCode'] = $responseCode;??? // 100,0
//???????$responseParams['returnCode'] = $returnCode;??? // 201,400
//
//???????$responseParams['message'] = $message;
//???????$responseParams['details'] = $details;
???????//保存 請(qǐng)求退款的參數(shù)?$requestParams
? ? ? ?……
???????//保存 退款響應(yīng)的參數(shù)?$response
???????……
???????//請(qǐng)求退款接口,根據(jù)返回的信息,判斷成功/失敗
???????if ($responseCode == 100) {????????????? //退款請(qǐng)求成功
???????????return true;
???????}
???????return false;????????? //退款請(qǐng)求失敗
??? }
四、獲取 退款成功 的響應(yīng)參數(shù)(對(duì)象轉(zhuǎn)數(shù)組)-非托管集成方式
?private function getRefundResponseParams($apiResponse){
???????$responseArr['links=self=href'] = '';
???????$responseArr['links=self=method'] = '';
???????$responseArr['links=void=href'] = '';
???????$responseArr['links=void=method'] = '';
???????$responseArr['id'] = ''; //客戶(hù)id,請(qǐng)求id
???????$responseArr['submitTimeUtc'] = '';
???????$responseArr['status'] = '';
???????$responseArr['reconciliationId'] = '';
???????$responseArr['clientReferenceInformation=code'] = '';? //訂單編號(hào)code
???????$responseArr['clientReferenceInformation=submitLocalDateTime'] = '';
???????$responseArr['clientReferenceInformation=ownerMerchantId'] = '';
???????$responseArr['clientReferenceInformation=returnReconciliationId'] = '';
???????$responseArr['refundAmountDetails=refundAmount'] = '';
???????$responseArr['refundAmountDetails=creditAmount'] = '';
???????$responseArr['refundAmountDetails=currency'] = '';
???????$responseArr['processorInformation=approvalCode'] = '';
???????$responseArr['processorInformation=transactionId'] = '';
???????$responseArr['processorInformation=forwardedAcquirerCode'] = '';
???????$responseArr['processorInformation=merchantNumber'] = '';
???????$responseArr['processorInformation=responseCode'] = '';
???????$responseArr['processorInformation=achVerification'] = '';
???????$responseArr['processorInformation=networkTransactionId'] = '';
???????$responseArr['orderInformation=invoiceDetails'] = '';
???????$responseArr['pointOfSaleInformation'] = '';
???????$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;
??? }