本文主要是介绍Laravel 页面中使用ApplePay,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目需要,要在页面中加上ApplePay 的功能,首先需要申请四个参数
MerchantIdentifier
ValidateMerchantURL
MerchantID
Transaction Key
页面的调用有两部分,一部分是检查当前环境的,一部分执行付款功能的
一,检查当前坏境,是否适合使用applepay,其实是判断当前的操作环境是不是移动端,且是苹果系统,比如,iphone ,ipad...
//因为用的是台比,所以js 是这个路径
<script src="https://xxxxxxxx/app-tw-redheart/dist/applepay.min.js"></script><div class="radio applePayBox" style="display: none">
<label >
<input type="radio" name="radioType" id="radioType" value="applepay" >
<img src="{{ asset('/img/applePay.png') }}" alt="ApplePay" style="width: 80px">
</label>
</div><script>init_applePay_btn();function init_applePay_btn(){var status = false;var validateMerchantURL = "{{ @$applepay_gateway['validateMerchantURL']}}";if (window.ApplePaySession) {var merchantIdentifier = "{{ @$applepay_gateway['merchantIdentifier']}}";var promise = ApplePaySession.canMakePaymentsWithActiveCard(merchantIdentifier);promise.then(function(canMakePayments) {if (canMakePayments) {$('.applePayBox').show();} else {$('.applePayBox').hide();}});} else {$('.applePayBox').hide();}}
如果当前环境适合使用applepay付款,就会显示applepay 的按钮
二,执行逻辑
function init_applepay(){event.preventDefault();ApplePay.init({orderId:'{{@$transaction->invoice_id}}',validateMerchantURL: '{{ @$applepay_gateway["validateMerchantURL"]}}',paymentAuthorizedURL:'',payee: '{{ @$merchant_info->merchant_name}}',product: '{{@$transaction->invoice_id}}',currency: '{{@$transaction->currency}}',country: 'hk',amount: {{@$transaction->amount}},domain: ''}).then(function(result) {// alert(result);// document.getElementById('result').textContent = JSON.stringify(result);if(result.ccAuthReply_reasonCode == 100){window.location.href = "{{url('confirm/')}}";}if(result.ccAuthReply_reasonCode == 204){alert("Transaction rejected, please contact your bank. (Reference Number: "+result.requestID+")");window.location.href="{{url('checkout/')}}";}if(result.ccAuthReply_reasonCode == 150){alert("Transaction unsuccessful, please try again. (Reference Number: "+result.requestID+")");window.location.href="{{url('checkout/')}}";}})}
注意了,付款之后,applepay 会马上post 数据到paymentAuthorizedURL,需要返回一个指定格式的数据,applepay 才算是支付成功的,否则还是支付失败的。
三,后台处理数据
//導入
require_once(app_path('ApplePay/cybersource/sdk-php/lib/CybsNameValuePairClient.php'));//判斷是否屬於uatif($applepay_gateway['uat'] == "1"){$api_config["wsdl"] = "https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.121.wsdl";$api_config["nvp_wsdl"] = "https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_NVP_1.121.wsdl";}else{$api_config["wsdl"] = "https://ics2wsa.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.121.wsdl";$api_config["nvp_wsdl"] = "https://ics2wsa.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_NVP_1.121.wsdl";}//初始化 client$client = new \CybsNameValuePairClient(array(),$api_config);//賦值$request=['merchantID'=>$applepay_gateway['merchantid'],'ccAuthService_run'=>'true','ccCaptureService_run'=>'true','merchantReferenceCode'=>$payment_data['orderId'],'ccAuthService_reconciliationID'=>$payment_data['orderId'],'billTo_firstName'=>'','billTo_lastName'=>'','billTo_street1'=>'','billTo_city'=>'','billTo_state'=>'','billTo_postalCode'=>'','billTo_country'=>'','billTo_email'=>$payer_email,'encryptedPayment_descriptor'=>'','encryptedPayment_encoding'=>'Base64','encryptedPayment_data'=>base64_encode($payment_token),'purchaseTotals_currency'=>$currency,'purchaseTotals_grandTotalAmount'=>$amount,'paymentSolution'=>'001',];//查詢訂單$reply = $client->runTransaction($request);// 數據整理$arr_reply = explode("\n", $reply);$arr_reply_formatted = array();foreach($arr_reply as $item) {$arr_tmp = explode('=', $item);$key = $arr_tmp[0];if (!empty($key)){$value = $arr_tmp[1];$arr_reply_formatted[$key] = $value;}}//返回查詢內容,根據內容判斷訂單$arr_reply_formatted;//表示扣费成功 ,付款成功,当返回指定格式,就会扣费成功,处理结束if($arr_reply_formatted['reasonCode'] == '100' && $arr_reply_formatted['decision'] == 'ACCEPT'){}//返回指定格式的数据echo json_encode($arr_reply_formatted);exit;
这篇关于Laravel 页面中使用ApplePay的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!