Thinkphp使用Authorize.Net实现VISA信用卡支付

官方网站:https://developer.authorize.net/
开发者文档:https://developer.authorize.net/api/reference/index.html

一、注册沙箱账号进行调试

在这里插入图片描述
注册成功之后会弹出你的沙箱账号信息

API LOGIN ID
48h4xxxxxePS

TRANSACTION KEY
4S9xxxxxxxxxx8Aq

KEY
Simon

其中 API LOGIN ID 跟 TRANSACTION KEY 是需要程序使用的,保存下来
如果秘钥忘记了,可以去账户里面重置
ACCOUNT -> Settings -> API Credentials & Keys
里面可以看到Login Id
其中的秘钥生成有两种类型 New Transaction Key 跟 New Signature Key
第一种是直接交易使用,第二种是托管表单使用

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

composer安装SDK

composer require authorizenet/authorizenet

github上有案例可以参考
地址:https://github.com/AuthorizeNet/sample-code-php

使用信用卡支付的方式

前端页面表单提交传递 信用卡号,到期时间,安全码三个参数
判断支付成功之后直接修改数据表的状态

/*** 信用卡支付* $pay = new AuthorizePay();* $result = $pay->chargeCreditCard(1.45);* @param $cardData* @param $AddressData* @param $userData* @param $orderData* @return bool|string*/
public function chargeCreditCard($cardData,$AddressData,$userData,$orderData){$refId = 'ref'.time();//创建信用卡账户$creditCard = new CreditCardType();$creditCard->setCardNumber($cardData['cardno']); //信用卡号$creditCard->setExpirationDate($cardData['deadtime']); //信用卡到期时间$creditCard->setCardCode($cardData['cardcode']); //卡代码//创建支付对象$paymentOne = new PaymentType();$paymentOne->setCreditCard($creditCard);//创建订单信息$order = new OrderType();//$order->setInvoiceNumber("10101"); //发票编号$order->setDescription("Grfresh Order ".date("d/m/Y H:i:s",time())); //订单说明//设置账单地址,收货地址$customerAddress = new CustomerAddressType();$customerAddress->setFirstName($AddressData['alias']);$customerAddress->setLastName($AddressData['consignee']);$customerAddress->setCompany($AddressData['alias']);$customerAddress->setAddress($AddressData['address']);$customerAddress->setCity($AddressData['city_name']);$customerAddress->setState($AddressData['province_name']);$customerAddress->setZip($AddressData['zip_code']);$customerAddress->setCountry($AddressData['country']);/*addeess:1 Cromwell CT,city:Princetonstate: NJzipcode: 08540country: USA*///设置用户信息$customerData = new CustomerDataType();$customerData->setType("individual");$customerData->setId($userData['id'].'_'.time()); //用户id$customerData->setEmail($userData['user_email']); //用户邮箱//为事物设置值/*$duplicateWindowSetting = new SettingType();$duplicateWindowSetting->setSettingName("duplicateWindow");$duplicateWindowSetting->setSettingValue("60");*///设置商家的自定义字段/* $merchantDefinedField = new UserFieldType();$merchantDefinedField->setName("customerLoyaltyNum");$merchantDefinedField->setValue("1128836273");*///创建request对象$transactionRequestType = new TransactionRequestType();$transactionRequestType->setTransactionType("authCaptureTransaction"); //交易类型$transactionRequestType->setAmount($orderData['total_amount']);$transactionRequestType->setOrder($order);$transactionRequestType->setPayment($paymentOne);$transactionRequestType->setBillTo($customerAddress);$transactionRequestType->setCustomer($customerData);//$transactionRequestType->addToTransactionSettings($duplicateWindowSetting);//$transactionRequestType->addToUserFields($merchantDefinedField);//组装完整的事物请求$request = new CreateTransactionRequest();$request->setMerchantAuthentication($this->merchantAuthentication);$request->setRefId($refId);$request->setTransactionRequest($transactionRequestType);//var_dump($request);echo "<br><br>";//获取响应$controller = new CreateTransactionController($request);//var_dump($controller);echo "<br><br>";$response = $controller->executeWithApiResponse($this->url);//ANetEnvironment::PRODUCTION//var_dump($response);if ($response != null) {if($response->getTransactionResponse() == null){return "No response returned ";}// Check to see if the API request was successfully received and acted uponif ($response->getTransactionResponse()->getErrors() == null && $response->getMessages()->getResultCode() == "Ok") {return true;} else {//这里是支付失败的回调$tresponse = $response->getTransactionResponse();if ($tresponse != null && $tresponse->getErrors() != null) {return $tresponse->getErrors()[0]->getErrorText();} else {return $response->getMessages()->getMessage()[0]->getText();}}} else {return  "No response returned ";}
}

使用托管表单的方式支付

下面地址讲的很清楚,结合官方文档对照看
参考地址:https://segmentfault.com/a/1190000010599644

整合了一些方法

<?php/*** Created by PhpStorm.* User: Administrator* Date: 2020/9/15* Time: 15:34*/
namespace lib;use net\authorize\api\constants\ANetEnvironment;
use net\authorize\api\contract\v1\CreateCustomerProfileRequest;
use net\authorize\api\contract\v1\CreateTransactionRequest;
use net\authorize\api\contract\v1\CreditCardType;
use net\authorize\api\contract\v1\CustomerAddressType;
use net\authorize\api\contract\v1\CustomerDataType;
use net\authorize\api\contract\v1\CustomerProfilePaymentType;
use net\authorize\api\contract\v1\CustomerProfileType;
use net\authorize\api\contract\v1\GetCustomerProfileRequest;
use net\authorize\api\contract\v1\GetHostedProfilePageRequest;
use net\authorize\api\contract\v1\MerchantAuthenticationType;
use net\authorize\api\contract\v1\OrderType;
use net\authorize\api\contract\v1\PaymentProfileType;
use net\authorize\api\contract\v1\PaymentType;
use net\authorize\api\contract\v1\SettingType;
use net\authorize\api\contract\v1\TransactionRequestType;
use net\authorize\api\contract\v1\UserFieldType;
use net\authorize\api\controller\CreateCustomerProfileController;
use net\authorize\api\controller\CreateTransactionController;
use net\authorize\api\controller\GetCustomerProfileController;
use net\authorize\api\controller\GetHostedProfilePageController;class AuthorizePay
{public $test_url = "https://apitest.authorize.net/xml/v1/request.api"; //沙箱地址public $url = "https://api.authorize.net/xml/v1/request.api"; //生产地址public $login_id = null; //MERCHANT_LOGIN_IDpublic $key = null; //MERCHANT_TRANSACTION_KEYpublic $merchantAuthentication = null;public function __construct(){$this->login_id = "63xxxxxEt"; $this->key = "6FhLxxxxxxxx5TDy"; $this->merchantAuthentication = $this->createMerchant();}/*** 信用卡支付* $pay = new AuthorizePay();* $result = $pay->chargeCreditCard(1.45);* @param $cardData* @param $AddressData* @param $userData* @param $orderData* @return bool|string*/public function chargeCreditCard($cardData,$AddressData,$userData,$orderData){$refId = 'ref'.time();//创建信用卡账户$creditCard = new CreditCardType();$creditCard->setCardNumber($cardData['cardno']); //信用卡号$creditCard->setExpirationDate($cardData['deadtime']); //信用卡到期时间$creditCard->setCardCode($cardData['cardcode']); //卡代码//创建支付对象$paymentOne = new PaymentType();$paymentOne->setCreditCard($creditCard);//创建订单信息$order = new OrderType();//$order->setInvoiceNumber("10101"); //发票编号$order->setDescription("Grfresh Order ".date("d/m/Y H:i:s",time())); //订单说明//设置账单地址,收货地址$customerAddress = new CustomerAddressType();$customerAddress->setFirstName($AddressData['alias']);$customerAddress->setLastName($AddressData['consignee']);$customerAddress->setCompany($AddressData['alias']);$customerAddress->setAddress($AddressData['address']);$customerAddress->setCity($AddressData['city_name']);$customerAddress->setState($AddressData['province_name']);$customerAddress->setZip($AddressData['zip_code']);$customerAddress->setCountry($AddressData['country']);/*addeess:1 Cromwell CT,city:Princetonstate: NJzipcode: 08540country: USA*///设置用户信息$customerData = new CustomerDataType();$customerData->setType("individual");$customerData->setId($userData['id'].'_'.time()); //用户id$customerData->setEmail($userData['user_email']); //用户邮箱//为事物设置值/*$duplicateWindowSetting = new SettingType();$duplicateWindowSetting->setSettingName("duplicateWindow");$duplicateWindowSetting->setSettingValue("60");*///设置商家的自定义字段/* $merchantDefinedField = new UserFieldType();$merchantDefinedField->setName("customerLoyaltyNum");$merchantDefinedField->setValue("1128836273");*///创建request对象$transactionRequestType = new TransactionRequestType();$transactionRequestType->setTransactionType("authCaptureTransaction"); //交易类型$transactionRequestType->setAmount($orderData['total_amount']);$transactionRequestType->setOrder($order);$transactionRequestType->setPayment($paymentOne);$transactionRequestType->setBillTo($customerAddress);$transactionRequestType->setCustomer($customerData);//$transactionRequestType->addToTransactionSettings($duplicateWindowSetting);//$transactionRequestType->addToUserFields($merchantDefinedField);//组装完整的事物请求$request = new CreateTransactionRequest();$request->setMerchantAuthentication($this->merchantAuthentication);$request->setRefId($refId);$request->setTransactionRequest($transactionRequestType);//var_dump($request);echo "<br><br>";//获取响应$controller = new CreateTransactionController($request);//var_dump($controller);echo "<br><br>";$response = $controller->executeWithApiResponse($this->url);//ANetEnvironment::PRODUCTION//var_dump($response);if ($response != null) {if($response->getTransactionResponse() == null){return "No response returned ";}// Check to see if the API request was successfully received and acted uponif ($response->getTransactionResponse()->getErrors() == null && $response->getMessages()->getResultCode() == "Ok") {return true;} else {//这里是支付失败的回调$tresponse = $response->getTransactionResponse();if ($tresponse != null && $tresponse->getErrors() != null) {return $tresponse->getErrors()[0]->getErrorText();} else {return $response->getMessages()->getMessage()[0]->getText();}}} else {return  "No response returned ";}}/*** 创建一个身份验证对象*/public function createMerchant(){$merchantAuthentication = new MerchantAuthenticationType();$merchantAuthentication->setName($this->login_id);$merchantAuthentication->setTransactionKey($this->key);return $merchantAuthentication;}/*** 用户申请CustomerProfileID* @param $user* @return string*/public function getCustomerProfileId($user){$customerProfile = new CustomerProfileType();$customerProfile->setDescription($user['description']);$customerProfile->setMerchantCustomerId($user['customer_id']);$customerProfile->setEmail($user['email']);$request = new CreateCustomerProfileRequest();$refId = 'ref' . time();$request->setMerchantAuthentication($this->merchantAuthentication);$request->setRefId($refId);$request->setProfile($customerProfile);$controller = new CreateCustomerProfileController($request);$response = $controller->executeWithApiResponse(ANetEnvironment::SANDBOX);return $response->getCustomerProfileId();}/*** 获取用户的支付信息* @param $profileId* @return \net\authorize\api\contract\v1\AnetApiResponseType*/public function getCustomProfileInfo($profileId){$request = new GetCustomerProfileRequest();$request->setMerchantAuthentication($this->merchantAuthentication);$request->setCustomerProfileId($profileId);$controller = new GetCustomerProfileController($request);$response = $controller->executeWithApiResponse(ANetEnvironment::SANDBOX);return $response;}/*** 创建发起支付* @return*/public function createPayTransaction(){$customer = $this->getCustomProfileInfo(session('profileId'));$payment = $customer->getProfile()->getPaymentProfiles();$paymentProfileId = $payment[0]->getCustomerPaymentProfileId();$profileToCharge = new CustomerProfilePaymentType();$profileToCharge->setCustomerProfileId(session('profileId'));$paymentProfile = new PaymentProfileType();$paymentProfile->setPaymentProfileId($paymentProfileId);$profileToCharge->setPaymentProfile($paymentProfile);$transactionRequestType = new TransactionRequestType();$transactionRequestType->setTransactionType("authCaptureTransaction");$transactionRequestType->setAmount(1); // 支付的价格$transactionRequestType->setProfile($profileToCharge);$request = new CreateTransactionRequest();$request->setMerchantAuthentication($this->merchantAuthentication);$request->setTransactionRequest( $transactionRequestType);$controller = new CreateTransactionController($request);$response = $controller->executeWithApiResponse(ANetEnvironment::SANDBOX);//判断是否支付成功,成功返回order_sn ,失败返回信息;if($response->getMessages()->getResultCode() == 'Ok'){return ['code'=>1,'data'=>$customer->getProfile()->getMerchantCustomerId()];}else{$tresponse = $response->getTransactionResponse();if ($tresponse != null && $tresponse->getErrors() != null) {return ['code'=>0,'msg'=> $tresponse->getErrors()[0]->getErrorText()];} else {return ['code'=>0,'msg'=> $response->getMessages()->getMessage()[0]->getText()];}}//return $response;}/*** 获取网页表单的授权token* @param $customerProfileID* @param $url string 接收处理相应的页面* @return mixed*/public function getFormToken($customerProfileID,$url){$setting = new SettingType();$setting->setSettingName("hostedProfileIFrameCommunicatorUrl");$setting->setSettingValue($url);$request = new GetHostedProfilePageRequest();$request->setMerchantAuthentication($this->merchantAuthentication);$request->setCustomerProfileId($customerProfileID);$request->addToHostedProfileSettings($setting);$controller = new GetHostedProfilePageController($request);$response = $controller->executeWithApiResponse(ANetEnvironment::PRODUCTION);/*if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") ){echo $response->getToken()."\n";}else{echo "ERROR :  Failed to get hosted profile page\n";$errorMessages = $response->getMessages()->getMessage();echo "Response : " . $errorMessages[0]->getCode() . "  " .$errorMessages[0]->getText() . "\n";}*/return $response->getToken();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/69054.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

4款好用的PC端电子书阅读软件,千万别错过

分享4款好用的电子书阅读软件&#xff0c;支持多种电子书格式阅读&#xff0c;并且阅读界面舒适可随意调整&#xff0c;大家快去试试吧&#xff01; 1、百度阅读器精简版 支持阅读的格式&#xff1a;TXT、PDF 一个百度推出的电子书阅读软件&#xff0c;简单小巧&#xff0c;…

GitBook制作epub电子书,并上传到微信读书

目标&#xff1a;将一本 GitBook&#xff08;SpringBoot2 中文参考指南&#xff09;转换为 epub 电子书&#xff0c;放到微信读书里。 准备工作&#xff1a;Windows 10 X64&#xff0c;NodeJS及版本管理工具nvm、Chrome浏览器 步骤一&#xff1a;打开 https://jack80342.gitbo…

学生党福音 电子教材下载网站推荐

还在购买电子教材&#xff1f;这几个电子教材下载网站可以免费下载下载教材&#xff0c;一起来看看吧。 1.中小学数字教材一站式下载 一个包含小学和中学教科书的网站。从小学一年级到高中三年级的教科书均包括在内。支持在线查看和下载&#xff0c;下载格式为PDF。我们可以滑…

信息时代,为什么还读纸质书

后人进步&#xff0c;是因为脚踩先人的脚印&#xff0c;这是知识进步最重要的途径之一。 唐僧取经&#xff0c;历经千山万水也要把真经取回来&#xff0c;取回来&#xff0c;再翻译&#xff0c;再传播&#xff1b;中国古代四大发明之造纸术、印刷术&#xff0c;承载了古代劳动…

Kindle下线在即 使用cpolar建立自己的电子书图书馆

在电子书风靡的时期&#xff0c;大部分人都购买了一本电子书&#xff0c;虽然这本电子书更多的时候是被搁置在储物架上吃灰&#xff0c;或者成为盖泡面的神器&#xff0c;但当亚马逊发布消息将放弃电子书在中国的服务时&#xff0c;还是有些令人惋惜&#xff0c;毕竟谁也不想大…

推荐一些可以获取免费的国外的原版书籍(电子版)网站

Z-library 推荐指数&#xff1a;★★★★★ 网站&#xff1a;https://z-lib.org/ 这个网站据称是世界最大的电子图书馆&#xff0c;收藏的资源包含725万本书、8075万的文献条目&#xff0c;可以说是相当丰富了。 网站支持中文搜索&#xff0c;不过注册登录就可以直接下载电子书…

彻底凉了!全球最大电子书网站遭封站

公众号关注 「奇妙的 Linux 世界」 设为「星标」&#xff0c;每天带你玩转 Linux &#xff01; 前几天&#xff0c;号称是世界上最大的免费电子图书馆 Z-Library&#xff0c;被美国查封&#xff0c;相关的数个域名全部无法访问&#xff01; 根据 DNS 记录和其他信息显示&#x…

2023年最值得关注的十大科技趋势,这些技术将迎来爆发,把握住风口和掘金机会!

1 月 11 日&#xff0c;InfoQ获悉&#xff0c;达摩院 2023 十大科技趋势发布&#xff0c;生成式 AI、Chiplet 模块化设计封装、全新云计算体系架构等技术入选。 达摩院发布十大科技趋势 达摩院认为&#xff0c;全球科技日趋显现出交叉融合发展的新态势&#xff0c;尤其在信息与…

爆火论文打造《西部世界》雏形:25个AI智能体,在虚拟小镇自由成长

机器之心报道 机器之心编辑部 《西部世界》的游戏逐渐走进现实。 我们能否创造一个世界&#xff1f;在那个世界里&#xff0c;机器人能够像人类一样生活、工作、社交&#xff0c;去复刻人类社会的方方面面。 这种想象&#xff0c;曾在影视作品《西部世界》的设定中被完美地还原…

Android 添加App快捷方式到桌面

原创文章&#xff0c;如有转载&#xff0c;请注明出处&#xff1a;http://blog.csdn.net/myth13141314/article/details/68926849 主要原理是通过向系统发送创建快捷方式的广播 设置Intent&#xff0c;传递快捷方式的信息&#xff0c;名字和图标等 Intent shortcut new Int…

如何把一个网页设置快捷方式放到桌面上去,或者手机桌面当App一样使用

分别讲电脑端和手机端: 电脑端: 在尝试好几种方式后,还是觉得最最简单的方法,还是用电脑自带的方式不借助任何外力方便,利用谷歌的方式也讲一下哈(利用谷歌会有自己的图标这点不错); 其他方式: https://zh.wikihow.com/%E6%8A%8A%E7%BD%91%E7%AB%99%E7%9A%84%E5%BF%AB%E6%8D…

给你的AppImage创建桌面快捷方式

运行环境:Ubuntu 22.04 LTS 1.首先准备好AppImage文件并放在一个你知道的地方 2.打开终端&#xff0c;在/usr/share/applications下新建APP.desktop文件(APP可以改成你的应用名称) cd /usr/share/applications sudo touch APP.desktop 3. root模式下使用vi编辑qi编辑APP.deskto…

iPhone苹果手机如何将百度小程序添加到手机桌面方便使用?

苹果iPhone手机将百度小程序添加到手机桌面后&#xff0c;下次使用直接可以在iPhone苹果手机桌面找到像APP一样的图标&#xff0c;点击直接打开百度小程序方便使用&#xff1b; 如何将百度小程序添加到手机桌面方便使用&#xff1f; 1、打开手机百度APP&#xff0c;搜索要添加…

iOS 添加快捷方式到主屏幕

参考文章&#xff1a; iOS 添加到主屏幕/ iOS Add To Desktop iOS创建桌面快捷方式代码 在上面文章和其他资料基础上实现此功能&#xff0c;详细介绍和技术点可参考上述文章。Demo是以第三方CocoaHTTPServer为基础&#xff0c;建立本机服务器&#xff0c;调起Safari创建快…

OpenAI 直播大秀语音指挥 AI 自动编程

本文转载自IT之家 刚刚&#xff0c;OpenAI 又玩出了一个新高度。 只输入自然语句&#xff0c;AI 就自动做了个小游戏&#xff01; 划重点&#xff1a;不&#xff01; 用&#xff01; 你&#xff01; 编&#xff01; 程&#xff01; 来&#xff0c;感受一下这个 feel。 第一…

直播预告 | 腾讯云工业AI系列直播

随着工业革命的不断推进&#xff0c;人工智能等新技术新理念在各行业兴起。同时&#xff0c;各行业也逐步向数字化、智能化、自动化转型&#xff0c;进入现代化工业新阶段。 工业质检是整个制造中一个非常重要的环节&#xff0c;但工业AI质检的有效落地是我们面临的一个巨大挑…

Steam教育对儿童在幼儿园阶段概念理解

孩子对有关科学领域的探究和学习&#xff0c;往往受到好奇心和兴趣的直接驱使&#xff0c;少儿编程就是从这一点出发&#xff0c;来培养孩子的科学思维与能力的。具体而言&#xff0c;少儿编程是怎样助力培养孩子的科学素养呢&#xff1f; 增强孩子处理信息的能力。现实中充斥着…

聚观早报 | 推特临时培训员工应对世界杯;世界杯足球内置传感器

今日要闻&#xff1a;推特临时培训员工应对世界杯;京东靠降本增效实现转亏为盈;世界杯足球内置传感器;艾格重返迪士尼CEO职位;特斯拉明年或开启收购计划 推特临时培训员工应对世界杯 据消息&#xff0c; 2022年世界杯拉开帷幕&#xff0c;推特的使用量即将激增&#xff0c;其…

Chrome浏览器模拟微信客户端访问网址,方法图文讲解模拟微信

我们访问有的网址&#xff0c;网址里限制了只能微信客户端访问才能打开&#xff0c;要不然就打不开或者跳转到其他页面去了。 下面图文并茂的讲解下怎么用 Chrome 模拟微信UserAgent。 0x0、打开Chrome控制台 打开控制台快捷键在Chrome下Windows系统按下F12&#xff0c;Ma…

2021高考成绩查询数学和物理,2021湖南高考物理这么难真的好吗?全国数学卷简单,谁会是赢家?...

相信很多湖南省高考考生的家长已经感受到了孩子考完物理后的情绪低落了&#xff0c;原因是湖南省今年的高考物理试题比较难。在考完全国统一的数学科目后&#xff0c;普遍反映说数学简单&#xff0c;而物理却非常难。在这种情况下&#xff0c;会影响高考录取吗&#xff1f;什么…