netopia / payment
一组库,为您提供将NETOPIA Payments功能集成到您的网站应用程序和移动应用程序中的手段。
v1.1.2
2024-02-07 09:11 UTC
Requires
- php: >=5.5
- ext-dom: *
- ext-simplexml: *
README
NETOPIA Payments
NETOPIA Payments Composer
安装
从您的项目根目录运行以下命令
composer require netopia/payment
或者
-
将以下内容添加到您的 composer.json 文件中,例如以下示例
"require": { ... "netopia/payment": "^1.1", ... }然后从您的终端运行以下命令
composer install
示例
Laravel中支付请求的一个示例
... 使用 Netopia\Payment\Address; 使用 Netopia\Payment\Invoice; 使用 Netopia\Payment\Split; 使用 Netopia\Payment\Request\Card; ... class ExampleController extends Controller { /** * 所有支付请求都将发送到NETOPIA Payments服务器 * SANDBOX: http://sandboxsecure.mobilpay.ro * LIVE: https://secure.mobilpay.ro */ public $paymentUrl; /** * NETOPIA Payments只与证书合作。每个NETOPIA合作伙伴(商家)都有一张证书。 * 您可以从管理员面板下载证书。 * 位于管理员 -> 商家账户 -> 详细信息 -> 安全设置 * 变量$x509FilePath是您平台中证书的路径 * 例如:/home/certificates/public.cer */ public $x509FilePath; /** * 开票地址 */ public $billingAddress; /** * 发货地址 */ public $shippingAddress; ... public function index() { $this->paymentUrl = 'http://sandboxsecure.mobilpay.ro'; $this->x509FilePath = '/home/certificates/sandbox.XXXX-XXXX-XXXX-XXXX-XXXX.public.cer'; try { $paymentRequest = new Card(); $paymentRequest->signature = 'XXXX-XXXX-XXXX-XXXX-XXXX';//签名 - 由mobilpay.ro为每个商家账户生成 $paymentRequest->orderId = md5(uniqid(rand())); // order_id对于商家账户应该是唯一的 $paymentRequest->confirmUrl = 'https://example.test/card/success'; // 支付过程完成后,mobilPay将重定向客户端的位置,是必需的 $paymentRequest->returnUrl = 'https://example.test/ipn';//mobilPay将发送支付结果的位置,是必需的 /* * 发票信息 */ $paymentRequest->invoice = new Invoice(); $paymentRequest->invoice->currency = 'RON'; $paymentRequest->invoice->amount = '20.00'; $paymentRequest->invoice->tokenId = null; $paymentRequest->invoice->details = "通过Composer库支付"; /* * 开票信息 */ $this->billingAddress = new Address(); $this->billingAddress->type = "person"; // 应为 "person" / "company" $this->billingAddress->firstName = "开票名称"; $this->billingAddress->lastName = "开票姓氏"; $this->billingAddress->address = "伊昂·克里安加大道,号 00"; $this->billingAddress->email = "test@billing.com"; $this->billingAddress->mobilePhone = "0732123456"; $paymentRequest->invoice->setBillingAddress($this->billingAddress); /* * 发货 */ $this->shippingAddress = new Address(); $this->shippingAddress->type = "person"; // 应为 "person" / "company" $this->shippingAddress->firstName = "发货名称"; $this->shippingAddress->lastName = "发货姓氏"; $this->shippingAddress->address = "米哈伊·埃米内斯库大道,号 00"; $this->shippingAddress->email = "test@shipping.com"; $this->shippingAddress->mobilePhone = "0721234567"; $paymentRequest->invoice->setShippingAddress($this->shippingAddress); /** * 参数 * 参数是可选的 */ $paymentRequest->params = [ 'framework_name'=>"Laravel", 'framework_version'=>"9" ]; /** * 分割支付 * 此选项仅适用于已经设置分割支付设置的商家。 * 'id' 是 SELLERA_CCOUNT_ID * 'amount' 是分割的金额 */ $paymentRequest->split = new Split(); $paymentRequest->split->destinations = [ [ 'id'=>'123456', 'amount'=>"2.75" ] ]; /* * 加密 * */ $paymentRequest->encrypt($this->x509FilePath); /** * 将以下数据发送到NETOPIA Payments服务器 * 方法:POST * 参数:env_key, data, cipher, iv * URL:$paymentUrl */ $env_key = $paymentRequest->getEnvKey(); $data = $paymentRequest->getEncData(); $cipher = $paymentRequest->getCipher(); $iv = $paymentRequest->getIv(); }catch (\Exception $e) { return "哎呀,出问题了!"; } } ... }
Laravel中的IPN示例
... use Netopia\Payment\Address; use Netopia\Payment\Invoice; use Netopia\Payment\Request\Card; use Netopia\Payment\Request\Notify; use Netopia\Payment\Request\PaymentAbstract; ... class IpnsController extends Controller { ... public $errorCode; public $errorType; public $errorMessage; public $paymentUrl; public $x509FilePath; public $cipher; public $iv; ... public function index() { ... $this->errorType = PaymentAbstract::CONFIRM_ERROR_TYPE_NONE; $this->errorCode = 0; $this->errorMessage = ''; $this->cipher = 'rc4'; $this->iv = null; .... if(array_key_exists('cipher', $_POST)) { $this->cipher = $_POST['cipher']; if(array_key_exists('iv', $_POST)) { $this->iv = $_POST['iv']; } } $this->paymentUrl = 'http://sandboxsecure.mobilpay.ro'; $this->x509FilePath = '/home/certificates/sandbox.XXXX-XXXX-XXXX-XXXX-XXXXprivate.key'; if (strcasecmp($_SERVER['REQUEST_METHOD'], 'post') == 0){ if(isset($_POST['env_key']) && isset($_POST['data'])){ try { $paymentRequestIpn = PaymentAbstract::factoryFromEncrypted($_POST['env_key'], $_POST['data'], $this->x509FilePath, null, $this->cipher, $this->iv); $rrn = $paymentRequestIpn->objPmNotify->rrn; if ($paymentRequestIpn->objPmNotify->errorCode == 0) { switch($paymentRequestIpn->objPmNotify->action){ case 'confirmed': //更新数据库,设置状态为 "已确认/已捕获" $this->errorMessage = $paymentRequestIpn->objPmNotify->errorMessage; break; case 'confirmed_pending': //更新数据库,设置状态为 "待处理" $this->errorMessage = $paymentRequestIpn->objPmNotify->errorMessage; break; case 'paid_pending': //更新数据库,设置状态为 "待处理" $this->errorMessage = $paymentRequestIpn->objPmNotify->errorMessage; break; case 'paid': //更新数据库,设置状态为 "开启/已授权" $this->errorMessage = $paymentRequestIpn->objPmNotify->errorMessage; break; case 'canceled': //更新数据库,设置状态为 "已取消" $this->errorMessage = $paymentRequestIpn->objPmNotify->errorMessage; break; case 'credit': //更新数据库,设置状态为 "已退款" $this->errorMessage = $paymentRequestIpn->objPmNotify->errorMessage; break; default: $errorType = PaymentAbstract::CONFIRM_ERROR_TYPE_PERMANENT; $this->errorCode = PaymentAbstract::ERROR_CONFIRM_INVALID_ACTION; $this->errorMessage = 'mobilpay_refference_action 参数无效'; } }else{ //更新数据库,设置状态为 "已拒绝" $this->errorMessage = $paymentRequestIpn->objPmNotify->errorMessage; } }catch (\Exception $e) { $this->errorType = PaymentAbstract::CONFIRM_ERROR_TYPE_TEMPORARY; $this->errorCode = $e->getCode(); $this->errorMessage = $e->getMessage(); } }else{ $this->errorType = PaymentAbstract::CONFIRM_ERROR_TYPE_PERMANENT; $this->errorCode = PaymentAbstract::ERROR_CONFIRM_INVALID_POST_PARAMETERS; $this->errorMessage = 'mobilpay.ro 发布了无效参数'; } } else { $this->errorType = PaymentAbstract::CONFIRM_ERROR_TYPE_PERMANENT; $this->errorCode = PaymentAbstract::ERROR_CONFIRM_INVALID_POST_METHOD; $this->errorMessage = '支付确认请求方法无效'; } /** * 与 NETOPIA 支付服务器通信 */ header('Content-type: application/xml'); echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; if($this->errorCode == 0) { echo "<crc>{$this->errorMessage}</crc>"; } else { echo "<crc error_type=\"{$this->errorType}\" error_code=\"{$this->errorCode}\">{$this->errorMessage}</crc>"; } } }
要下订单,必须使用 POST 方法将以下参数发送到我们的服务器
- env_key
- data
- cipher
- iv
视图示例
<form id="paymentForm" action="{{ $paymentData['url'] }}" method="POST"> <input type="hidden" name="env_key" value="{{ $paymentData['env_key'] }}"> <input type="hidden" name="data" value="{{ $paymentData['data'] }}"> <input type="hidden" name="cipher" value="{{ $paymentData['cipher'] }}"> <input type="hidden" name="iv" value="{{ $paymentData['iv'] }}"> </form> <script> document.getElementById('paymentForm').submit(); </script>
注意/建议
-
如果您的平台中存在命名空间问题,您可以通过寻求服务提供商的帮助来解决它。例如,在 Laravel 中,您可以在您的 vendor 中定义一个提供者,并将其放入 composer.json 中以设置命名空间。
-
如果在任何情况下,国家、城市、邮编等与您的应用程序中的地址分离,请将其与地址合并,并为账单/配送地址创建完整地址。