CyberSource支付二---SDK方式付款

一、準備信息

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();

??? }


三、付款邏輯

?????? 卡類型、卡號、卡過期年月、卡的cvn,可以提供一個form進行輸入。


public function getCheckoutUrlIFrame(){

???????$card_type = ‘’;?????? //卡類型(001:Visa;002:Mastercard)

???????$card_number = ‘’;??????? //卡號(16位數字)

???????$card_expiry_date = ‘’;?? //卡過期年月(格式:MM-yyyy)

???????$card_cvn = ‘’;??????? //卡的cvn(3位/4位數字)


???????// 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


???????//請求信息+部分響應信息提取出來

???????$params['requestId'] = $requestId;????????? //成功時的響應信息是對象,json后為空,無法獲取

???????//$params['transactionId'] = $transactionId;? //成功時的響應信息是對象,json后為空,無法獲取

//???????$params['responseCode'] = $responseCode;??? //成功時的響應信息是對象,json后為空,無法獲取

//???????$params['return_code'] = $return_code;????? //失敗時的響應信息,沒有code

???????//請求信息+所有響應信息

???????$params['response'] = $response;

???????$params['errmsg'] = $errmsg;


???????//只有響應信息

???????$responseParams['card_type'] = $card_type;????? //加上卡類型

???????$responseParams['requestId'] = $requestId;????????? //成功時的響應信息是對象,json后為空,無法獲取

???????//$params['transactionId'] = $transactionId;? //成功時的響應信息是對象,json后為空,無法獲取

???????$responseParams['responseCode'] = $responseCode;??? //成功時的響應信息是對象,json后為空,無法獲取

???????$responseParams['return_code'] = $return_code;????? //失敗時的響應信息,沒有code

???????$responseParams['status'] = $status;??????????? //響應的狀態

???????//所有響應信息

???????$responseParams['response'] = $response;

???????$responseParams['errmsg'] = $errmsg;


? ? ? ? //保存響應信息 $responseParams

? ? ? ? ……


? ? ? ?//保存到數據庫時,進行處理(為了安全,不保存具體的卡信息)

???????$params['card_number'] = '***';

???????$params['card_expiry_date'] = '***';

???????$params['card_cvn'] = '***';


? ? ? ? //保存請求+響應信息 $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" => ‘’??? //每個交易的唯一的商家生成的訂單參考或跟蹤編號。可以從數據庫中獲取。

???????];

???????$params = $clientReferenceInformationArr;?? //記錄提交參數

???????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);? //記錄提交參數

???????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;?????//記錄提交參數

???????$params['card_number'] = $card_number;????????? //重新支付時使用(為了安全,建議重新支付時,重新輸入,不要保存具體內容到數據庫)

???????$params['card_expiry_date'] = $card_expiry_date;???? //重新支付時使用(為了安全,建議重新支付時,重新輸入,不要保存具體內容到數據庫)

???????$params['card_cvn'] = $card_cvn;????//重新支付時使用(為了安全,建議重新支付時,重新輸入,不要保存具體內容到數據庫)

???????$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"?????????? //測試環境,用USD;正式環境,可以換成動態的貨幣類型

???????];

???????$params = array_merge($params, $orderInformationAmountDetailsArr);? //記錄提交參數

???????include('cybersource-rest-samples-php-56/vendor/cybersource/rest-client-php/lib/Model/Ptsv2paymentsOrderInformationAmountDetails.php');

???????$orderInformationAmountDetails = new\CyberSource\Model\Ptsv2paymentsOrderInformationAmountDetails($orderInformationAmountDetailsArr);


? ? ? ?//可以輸入,也可以從數據庫中獲取

???????$orderInformationBillToArr = [

???????????"firstName" => ‘’,???//姓

???????????"lastName" => ‘’,???//名

???????????"address1" => ‘’,???? //地址

???????????"locality" => ‘’, ?????? //城市

???????????//"administrativeArea" => "CA",

???????????"postalCode" => '',????? //郵編,默認可以給000000

???????????"country" => ‘’,? //國家代碼

???????????"email" => ‘’,?? //郵箱

???????????"phoneNumber" => ‘’??? //手機號

???????];

???????$params = array_merge($params, $orderInformationBillToArr); //記錄提交參數

???????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的運行環境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/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,請求id

???????????//$transactionId =$apiResponse[0]['processorInformation']['transactionId'];??? //每個請求,值都一樣

???????????$responseCode = $apiResponse[0]['processorInformation']['responseCode'];

???????????$status = $apiResponse[0]['status'];

???????????$return_code = $apiResponse[1];


???????????//獲取付款成功的 所有響應信息(里面的對象要轉數組)

???????????$response = $apiResponse;

???????????$response[0] = $this->getPaymentResponseParams($apiResponse);


???????????$errmsg = ($responseCode == 100 && $status == 'AUTHORIZED') ? '': ‘自定義錯誤信息’;?? //錯誤信息


???????} catch (\Cybersource\ApiException $e) {

???????????$errorCode = $e->getCode();


???????????$requestId = $e->getResponseBody()->id; //客戶id,請求id

???????????//$transactionId = '';

???????????$responseCode = $e->getCode();

???????????$status = $e->getResponseBody()->status;

???????????$return_code = $errorCode;


???????????$response = $e->getResponseBody();?//所有響應信息

???????????$response->responseCode = $e->getCode();


???????????$errmsg = $e->getMessage();????//錯誤信息

???????}

???????// 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;

??? }


五、獲取 付款成功 的響應參數(對象轉數組)-非托管集成方式

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,請求id

???????$responseArr['submitTimeUtc'] = '';

???????$responseArr['status'] = '';

???????$responseArr['reconciliationId'] = '';

???????$responseArr['errorInformation'] = '';


???????$responseArr['clientReferenceInformation=code'] = '';? //訂單編號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,請求的方法

???public function verifyReturn()

??? {

???????$card_type = ‘’;??? //從數據表中,獲取之前保存的響應數據card_type

???????$requestId = ‘’;??????????? //從數據表中,獲取之前保存的響應數據客戶id,請求id

???????//$transaction_id = ‘’;??? //從數據表中,獲取之前保存的響應數據transactionId

???????$response_code = ‘’;??? //從數據表中,獲取之前保存的響應數據responseCode

???????//$return_code = ‘’;??? //從數據表中,獲取之前保存的響應數據return_code

???????$status = ‘’;????????? //從數據表中,獲取之前保存的響應數據獲取狀態

???????$errmsg = ‘’;????????? //從數據表中,獲取之前保存的響應數據錯誤信息


???????//驗證返回的狀態碼

???????if ($response_code == 100 && $status == 'AUTHORIZED') {

???????????????????//付款成功的處理邏輯

???????????????????? ……


???????????????????? //付款成功的返回信息(可自定義)

???????????????????return ['res' => true, 'msg' => $errmsg];

???????}


????????????? //付款失敗的返回信息(可自定義)

???????????return ['res' => false, 'msg' => $errmsg];

??? }


七、付款失敗的url,請求的方法

?????? 與付款成功的url 類似,自定義處理邏輯即可,一般是跳轉展示付款失敗的頁面。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容