独角数卡切换英文界面并集成Stripecheckout记录

独角数卡默认Stripe插件太旧了,所以不支持强制3D验证。为了更优雅安全的使用,所以最好升级到Stripecheckout,这个对需要面对海外用户,或者或者你懂的需求还是很有用的。

最近做到这一步,突然有个这个小需求,就顺便看了看。过程还是蛮简单,这里做个记录,希望帮到有同样需求的童鞋!

这里顺道提一嘴,独角数卡默认是包含英文语言包,但在后台默认没有开启。在站点目录中找到这个路径 /resources/lang 查看是否包含en文件夹,如果有就请编辑 config/dujiaoka.php 这个文件。在

'language' => [
'zh_CN' => '简体中文',
'zh_TW' => '繁体中文',

添加 ‘en’ => ‘English’, 后台设置语言选择将出现英语选择,如果你觉得这个语言包还不够完善,也可以将 /resources/lang 下面中文文件夹复制,并重命名为en文件夹,然后对其中的文件进行编辑翻译。

用到的文件都可以从Github上独角数卡的项目页上找到,直接拷贝代码即可。

首先在这个路径app/Http/Controllers/Pay/新建一个 StripeCheckoutController.php 文件,将下列代码拷贝到这个文件中并保存。这里注意下你所使用的货币,默认是人民币兑换港币,意思就是说独角数卡默认是以人民币作为单位,Stripe是香港账号,这里按以港币结算。如果你是美区账号将按照美元结算,以此类推!请根据你的实际情况调整以下代码中的:

$price = Currency::convert()
->from('CNY')
->to('HKD')

<?php namespace App\Http\Controllers\Pay; 

use AmrShawky\LaravelCurrency\Facade\Currency; 
use App\Exceptions\RuleValidationException; 
use App\Http\Controllers\PayController; 
use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; 
use Stripe\Checkout\Session; use Stripe\Exception\UnexpectedValueException; 
use Stripe\Exception\SignatureVerificationException; use Stripe\Webhook; 

class StripeCheckoutController extends PayController 
{ 
   public function gateway(string $payway, string $orderSN) 
      { 
        try{ $this->loadGateWay($orderSN, $payway);
            \Stripe\Stripe::setApiKey($this->payGateway->merchant_id);
            $price = Currency::convert()
                ->from('CNY')
                ->to('HKD')
                ->amount($this->order->actual_price)
                ->round(2)
                ->get();
            $TotalAmount = $price * 100;
            $data = [
                'success_url'         => url('detail-order-sn', ['orderSN' => $this->order->order_sn]),
                'cancel_url'          => url('/'),
                'client_reference_id' => $this->order->order_sn,
                'line_items' => [[
                    'price_data' => [
                        'currency'     => 'HKD',
                        'product_data' => [
                            'name' => $this->order->order_sn
                        ],
                        'unit_amount'  => $TotalAmount 
                    ],
                    'quantity'   => 1
                ]],
                'mode'                => 'payment',
                'customer_email'      => $this->order->email
            ]; 
            $session = Session::create($data);
                return redirect()->away($session->url);//可以使用自定义域名
        }catch (\Exception $e) {
            return $this->err(__('dujiaoka.prompt.abnormal_payment_channel') . $e->getMessage());
        }
    }
    //webhook地址:https://shop.liner77.xyz/pay/stripecheckout/webhook  自行替换域名 
    //侦听的事件: 'checkout.session.completed' 'checkout.session.async_payment_succeeded'
    public function webhook(Request $request)
    {   
        $payload = file_get_contents('php://input');
        $data = json_decode($payload, true);
        if(!$this->orderService->detailOrderSN($data['data']['object']['client_reference_id'])){
            return 'order error';
        }
        $order = $this->orderService->detailOrderSN($data['data']['object']['client_reference_id']);
        if(!$this->payService->detail($order->pay_id)){
            return 'order error';
        }
        $payGateway = $this->payService->detail($order->pay_id);
        $endpoint_secret = $payGateway->merchant_pem;
        $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
        try{
            $event = Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
        }catch(UnexpectedValueException $e) {
            http_response_code(400);
            exit();
        }catch(SignatureVerificationException $e) {
            http_response_code(400);
            exit();
        }
        switch($event->type){
            case 'checkout.session.completed':
                $session = $event->data->object;
                if ($session->payment_status == 'paid') {
                    $this->orderProcessService->completedOrder($session->client_reference_id,$order->actual_price,$session->payment_intent);
                }
                break;
            case 'checkout.session.async_payment_succeeded':
                $session = $event->data->object;
                    $this->orderProcessService->completedOrder($session->client_reference_id,$order->actual_price,$session->payment_intent);
                break;
        }
        http_response_code(200);
        exit();
    }
}

接下来在这个路径 routes/common/pay.php 对pay.php进行编辑,将以下代码粘贴到你喜欢的合适位置并保存。

// StripeCheckout
    Route::get('stripecheckout/{payway}/{orderSN}','StripeCheckoutController@gateway');
    Route::post('stripecheckout/webhook', 'StripeCheckoutController@webhook');

最后登录phpmyadmin,找到相应数据库执行下SQL,搞定!

INSERT INTO `pays` VALUES (21, 'Stripe[微信支付宝]', 'stripe', 1, 1, 'pk开头的可发布密钥', NULL, 'sk开头的密钥', 'pay/stripe', 1, '2020-10-29 13:15:56', '2020-10-29 13:18:29', NULL);
INSERT INTO `pays` VALUES (22, 'Coinbase[加密货币]', 'coinbase', 1, 3, '费率', 'API密钥', '共享密钥', 'pay/coinbase', 0, '2021-08-15 13:15:56', '2021-10-12 13:15:56', NULL);
INSERT INTO `pays` VALUES (23, 'Epusdt[trc20]', 'epusdt', 1, 3, 'API密钥', '不填即可', 'api请求地址', 'pay/epusdt', 0, '2022-03-22 13:15:56', '2022-03-22 13:15:56', NULL);

INSERT INTO `pays` VALUES (24, 'stripeCheckout', 'stripeCheckout', '1', '3', 'sk_live_xxxxxxxxxxxxxxxxxxx', '//自行替换k_live_xx和 whsec_xxx开头的密钥\r\n//webhook地址:https://www.baidu.com/pay/stripecheckout/webhook 自行替换域名 \r\n//侦听的事件: \'checkout.session.completed\' \'checkout.session.async_payment_succeeded\'\r\n//默认cny转hkd', 'whsec_xxxxxxxxxxxxxxxxxxx', 'pay/stripecheckout', '0', '2022-04-26 19:27:31', '2022-09-29 21:21:55', NULL);
-- ----------------------------
COMMIT;

是不是超级简单,这个时候回到独角后台可以看到多出了相应支付方式,按照提示填入stripe相应信息即可!

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

资源猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值