1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2017/11/23
- */
- namespace crmeb\services;
- use Alipay\EasySDK\Kernel\Config;
- use Alipay\EasySDK\Kernel\Factory;
- use Alipay\EasySDK\Payment\Common\Models\AlipayTradeCreateResponse;
- use app\models\store\StoreOrder;
- use app\models\trade\CashTradeOrder;
- use app\models\trade\CashTradeOrderPayment;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- class AliPayService
- {
- public static function getOptions()
- {
- $options = new Config();
- $options->protocol = 'https';
- $options->gatewayHost = 'openapi.alipay.com';
- $options->signType = 'RSA2';
- $options->signType = 'RSA2';
- $options->appId = sys_config('ali_appid', '');
- // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
- $options->merchantPrivateKey = sys_config('ali_merchant_private_key', '');
- $options->alipayCertPath = sys_config('ali_cert_path', '');
- $options->alipayRootCertPath = sys_config('ali_root_cert_path', '');
- $options->merchantCertPath = sys_config('ali_merchant_cert_path', '');
- //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
- // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
- //可设置异步通知接收服务地址(可选)
- $options->notifyUrl = sys_config('site_url') . '/api/alipay/notify';
- // //可设置AES密钥,调用AES加解密相关接口时需要(可选)
- // $options->encryptKey = "<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
- return $options;
- }
- /**
- * @param $orderId
- * @param string $field
- * @return AlipayTradeCreateResponse
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function createTrade($orderId, $field = 'order_id')
- {
- Factory::setOptions(self::getOptions());
- if (is_string($orderId))
- $orderInfo = CashTradeOrderPayment::where($field, $orderId)->find();
- else
- $orderInfo = $orderId;
- if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
- if ($orderInfo['paid']) exception('支付已支付!');
- if ($orderInfo['pay_money'] <= 0) exception('该支付无需支付!');
- $bodyContent = $orderInfo['order_type'] == 1 ? '挂买' : '购买' . CashTradeOrder::get($orderInfo['bind_order']);
- $site_name = sys_config('site_name');
- if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
- try {
- $result = Factory::payment()->common()->create($bodyContent, $orderInfo['order_id'], $orderInfo['pay_money'], $orderInfo['uid']);
- if (!empty($result->code) && $result->code == 10000) {
- return $result;
- } else {
- exception("调用失败,原因:" . $result->msg . "," . $result->sub_msg . PHP_EOL);
- }
- } catch (Exception $e) {
- exception("调用失败," . $e->getMessage() . PHP_EOL);
- }
- }
- }
|