一、准备信息
1.merchantID:商家ID,商户号,组织ID
2.apiKeyID:密钥
3.secretKey:共享密钥
4.runEnv:
(1)沙盒(测试)环境url:apitest.cybersource.com
(2)正式环境url:api.cybersource.com
二、修改SDK的配置类
cybersource-rest-samples-php-56/Resources/ExternalConfiguration.php,
__construct 方法
// 将无参构造,改成有参构造,传递四个重要参数
function __construct($merchantID, $apiKeyID, $secretKey, $runEnv)
{
$this->authType = "http_signature"; // http_signature/jwt方式
$this->merchantID = $merchantID; //商家ID,商户号,组织ID
$this->apiKeyID = $apiKeyID; //密钥
$this->secretKey = $secretKey; //共享密钥
// MetaKey configuration [Start]
$this->useMetaKey = false;
$this->portfolioID = "";
// MetaKey configuration [End]
$this->keyAlias = $merchantID; //商家ID,商户号,组织ID
$this->keyPass = $merchantID; //商家ID,商户号,组织ID
$this->keyFilename = $merchantID; //商家ID,商户号,组织ID
$this->keyDirectory = "Resources/";
$this->runEnv = $runEnv; //沙盒(测试)/正式环境的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回调请求时,有个transaction_id(事务id,请求id);SDK方式付款:客户id,请求id,在付款后的响应信息里会有id;要保存到数据库
$code = ‘’; //自己平台的订单编号
$totalAmount = 0; //退款金额(全额,或者部分金额)
$requestParams['refundId'] = $id; //记录请求参数
$clientReferenceInformationArr = [
"code" => $code
];
$requestParams = array_merge($requestParams,$clientReferenceInformationArr); //记录请求参数
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" //测试环境,用USD;正式环境,可以换成动态的货币类型
];
$requestParams = array_merge($requestParams,$orderInformationAmountDetailsArr); //记录请求参数
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的运行环境url,测试(沙盒)/正式环境
$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 = '';
//获取退款成功的 所有响应信息(里面的对象要转数组)
$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(); //所有响应信息
$response->responseCode = $e->getCode(); //加上code
}
// $responseParams['requestId'] = $requestId;
// $responseParams['responseCode'] = $responseCode; // 100,0
// $responseParams['returnCode'] = $returnCode; // 201,400
//
// $responseParams['message'] = $message;
// $responseParams['details'] = $details;
//保存 请求退款的参数 $requestParams
……
//保存 退款响应的参数 $response
……
//请求退款接口,根据返回的信息,判断成功/失败
if ($responseCode == 100) { //退款请求成功
return true;
}
return false; //退款请求失败
}
四、获取 退款成功 的响应参数(对象转数组)-非托管集成方式
private function getRefundResponseParams($apiResponse){
$responseArr['links=self=href'] = '';
$responseArr['links=self=method'] = '';
$responseArr['links=void=href'] = '';
$responseArr['links=void=method'] = '';
$responseArr['id'] = ''; //客户id,请求id
$responseArr['submitTimeUtc'] = '';
$responseArr['status'] = '';
$responseArr['reconciliationId'] = '';
$responseArr['clientReferenceInformation=code'] = ''; //订单编号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;
}