外貿(mào)獨立站是做外貿(mào)的公司自己開設(shè)的獨立品牌商城,主要區(qū)別于商城平臺如亞馬遜、阿里巴巴等,優(yōu)點是自己的地盤自己說了算,缺點是需要自己推廣引流,適合有一定品牌的商家。
大部分外貿(mào)公司都是兩者都做,從商品平臺推廣獲客,然后把流量引入自己的品牌商城,打造自己的私域流量商城。
stripe支付公司是由一對來自愛爾蘭的天才兄弟collison brothers一手創(chuàng)辦的,他們表示隨著美國大的民營金融科技公司進入小企業(yè)貸款領(lǐng)域,新一輪融資使其價值增加了一半以上。
klipc分析稱,stripe的商業(yè)模式主要是梳理目前有的支付方式,將不同的支付方式打包成一套sdk接口,通過整體接入,降低用戶的接入成本,以收取手續(xù)費或者服務(wù)盈利。目前在金融行業(yè),很多公司已經(jīng)采用了stripe的支付通道,比起傳統(tǒng)通道,stripe效率更高,成本更低。
第一步:安裝類庫
composer require stripe/stripe-php
第二步后臺控制器:
functioncreate(){
\stripe\stripe::setapikey($this->clientsecret);//私鑰
try{
$jsonstr=file_get_contents('php://input');
$jsonobj=json_decode($jsonstr);//獲取頁面參數(shù)
$arr=object_array($jsonobj);//轉(zhuǎn)換為數(shù)組
$order_id=$arr['items'][0]['order_id'];//訂單單號
$order=db('order')->where('order_id',$order_id)->find();//查找訂單
//訂單是否存在和支付狀態(tài)
if(empty($order)){
echo"can'tfindorder!";
exit();
}
if($order['pay_status']==20){
echo'theorderwaspaid!';
exit();
}
$request=request::instance();
$base_url=$request->domain();//獲取網(wǎng)址
$time=time();
//判斷支付訂單是不是已經(jīng)生成
if(!$order['stripe_pay']||$time-$order['stripe_time']>30*60){
$currency_list=exchangeratemodel::getfront();
$currency=$currency_list['code'];
$total_amount_currency=$order['pay_price'];
$paymentintent=\stripe\paymentintent::create([
'amount'=>$total_amount_currency*100,//訂單金額
'currency'=>$currency,
'automatic_payment_methods'=>[
'enabled'=>true,
],
]);
$output=[
'clientsecret'=>$paymentintent->client_secret,
];
$transaction=explode('_secret_',$paymentintent->client_secret);//記錄生成的支付單號,單號后面會加‘單號_secret_安全碼’
$transaction_id=$transaction[0];
db('order')->where('order_id',$order_id)->update(['stripe_pay'=>$paymentintent->client_secret,'stripe_time'=>$time,'transaction_id'=>$transaction_id]);//記錄單號
}else{
$output=[
'clientsecret'=>$order['stripe_pay'],
];
}
//createapaymentintentwithamountandcurrency
echojson_encode($output);
}catch(error$e){
http_response_code(500);
echojson_encode(['error'=>$e->getmessage()]);
}
}
三,前端
<linkrel="stylesheet"href="__static__/css/style.css">
<linkrel="stylesheet"href="__static__/css/checkout.css">
<scriptsrc="https://js.stripe.com/v3/"></script>
<script>
varorder_id="{$order_id}"//訂單號
varurl="/home/stripepay"
varkey="{$key}"
varbase_url="{$base_url}";
</script>
<scriptsrc="__static__/js/checkout.js"defer></script>
<formid="payment-form">
<divid="payment-element">
<!--stripe.jsinjectsthepaymentelement-->
</div>
<buttonid="submit">
<divclass="spinnerhidden"id="spinner"></div>
<spanid="button-text">paynow</span>
</button>
<divid="payment-message"class="hidden"></div>
</form>
checout.js
//thisisyourtestpublishableapikey.
conststripe=stripe(key);//公鑰
//theitemsthecustomerwantstobuy
constitems=[{id:"xl-tshirt",order_id:order_id}];
letelements;
initialize();
checkstatus();
document
.queryselector("#payment-form")
.addeventlistener("submit",handlesubmit);
//fetchesapaymentintentandcapturestheclientsecret
asyncfunctioninitialize(){
const{clientsecret}=awaitfetch(url+"/create",{
method:"post",
headers:{"content-type":"application/json"},
body:json.stringify({items}),
}).then(res=>res.json());
elements=stripe.elements({clientsecret});
constpaymentelement=elements.create("payment");
paymentelement.mount("#payment-element");
}
asyncfunctionhandlesubmit(e){
e.preventdefault();
setloading(true);
console.log(elements);
const{error}=awaitstripe.confirmpayment({
elements,
confirmparams:{
//makesuretochangethistoyourpaymentcompletionpage
return_url:base_url+url+"/successful.html",//成功后,回跳地址
},
});
//thispointwillonlybereachedifthereisanimmediateerrorwhen
//confirmingthepayment.otherwise,yourcustomerwillberedirectedto
//your`return_url`.forsomepaymentmethodslikeideal,yourcustomerwill
//beredirectedtoanintermediatesitefirsttoauthorizethepayment,then
//redirectedtothe`return_url`.
if(error.type==="card_error"||error.type==="validation_error"){
showmessage(error.message);
}else{
showmessage("anunexpectederroroccured.");
}
setloading(false);
}
//fetchesthepaymentintentstatusafterpaymentsubmission
asyncfunctioncheckstatus(){
constclientsecret=newurlsearchparams(window.location.search).get(
"payment_intent_client_secret"
);
if(!clientsecret){
return;
}
const{paymentintent}=awaitstripe.retrievepaymentintent(clientsecret);
switch(paymentintent.status){
case"succeeded":
showmessage("paymentsucceeded!");
break;
case"processing":
showmessage("yourpaymentisprocessing.");
break;
case"requires_payment_method":
showmessage("yourpaymentwasnotsuccessful,pleasetryagain.");
break;
default:
showmessage("somethingwentwrong.");
break;
}
}
//-------uihelpers-------
functionshowmessage(messagetext){
constmessagecontainer=document.queryselector("#payment-message");
messagecontainer.classlist.remove("hidden");
messagecontainer.textcontent=messagetext;
settimeout(function(){
messagecontainer.classlist.add("hidden");
messagetext.textcontent="";
},4000);
}
//showaspinneronpaymentsubmission
functionsetloading(isloading){
if(isloading){
//disablethebuttonandshowaspinner
document.queryselector("#submit").disabled=true;
document.queryselector("#spinner").classlist.remove("hidden");
document.queryselector("#button-text").classlist.add("hidden");
}else{
document.queryselector("#submit").disabled=false;
document.queryselector("#spinner").classlist.add("hidden");
document.queryselector("#button-text").classlist.remove("hidden");
}
}
在平臺付款可以看到支付信息
四,獲取支付狀態(tài):
webhooks
添加回調(diào)地址和事件
charge.succeeded-支付成功后
需要密鑰
五、獲取回調(diào)信息,控制器
publicfunctioncallback()
{
$endpoint_secret='密鑰';//;
$payload=@file_get_contents('php://input');
$sig_header=$_server['http_stripe_signature'];
$event=null;
if($payload){
try{
$event=\stripe\webhook::constructevent(
$payload,$sig_header,$endpoint_secret
);
}catch(\stripe\exception\signatureverificationexception$e){
//invalidsignature
http_response_code(400);
exit();
}
}
$log_name="notify_url.log";
$this->log_result($log_name,'pay-start|--'.$event->data->object->paymentintent.'--|');
//handletheevent
switch($event->type){
case'charge.succeeded':
$paymentintent=$event->data->object;
//$payment=json_decode($paymentintent);
$payid=$paymentintent->payment_intent;
$order_no=db('order')->where('transaction_id',$payid)->value('order_no');
try{
$total_money=$event->amount/100;
//實例化訂單模型
$model=$this->getordermodel($order_no,10);
//訂單信息
$order=$model->getorderinfo();
if(empty($order)){
echo'ordernotexist';
}
$update_data['transaction_id']=$payid;
$status=$model->onpaysuccess(20,$update_data);
$this->log_result($log_name,'order_no:'.$order_no.'pay|--'.$paymentintent->payment_intent.'--|'.'status:'.$status);
if($status==false){
echo$model->geterror();
}
}catch(exception$e){
$this->error('payerror!','home/member/order');
//echo$e.',支付失敗,支付id【'.$paymentid.'】,支付人id【'.$payerid.'】';
//exit();
}
break;
case'charge.attached':
$paymentmethod=$event->data->object;
$this->log_result($log_name,'pay-attached|--'.$event->type.'--|');
break;
//...handleothereventtypes
default:
$this->log_result($log_name,'pay-fail|--'.$event->type.'--|');
echo'receivedunknowneventtype'.$event->type;
}
}
揚州到仁壽物流專線昆明到霸州物流專線163網(wǎng)和169網(wǎng)是指什么二者之間有什么關(guān)系?深圳到朔州物流專線杭州到秦皇島物流專線鄭州到靖邊物流專線僵尸網(wǎng)絡(luò)在大規(guī)模DDoS攻擊中的作用泉州到延安物流專線