AliPayService.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/23
  6. */
  7. namespace crmeb\services;
  8. use Alipay\EasySDK\Kernel\Config;
  9. use Alipay\EasySDK\Kernel\Factory;
  10. use Alipay\EasySDK\Payment\Common\Models\AlipayTradeCreateResponse;
  11. use app\models\store\StoreOrder;
  12. use app\models\trade\CashTradeOrder;
  13. use app\models\trade\CashTradeOrderPayment;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. use think\Exception;
  18. class AliPayService
  19. {
  20. public static function getOptions()
  21. {
  22. $options = new Config();
  23. $options->protocol = 'https';
  24. $options->gatewayHost = 'openapi.alipay.com';
  25. $options->signType = 'RSA2';
  26. $options->signType = 'RSA2';
  27. $options->appId = sys_config('ali_appid', '');
  28. // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
  29. $options->merchantPrivateKey = sys_config('ali_merchant_private_key', '');
  30. $options->alipayCertPath = sys_config('ali_cert_path', '');
  31. $options->alipayRootCertPath = sys_config('ali_root_cert_path', '');
  32. $options->merchantCertPath = sys_config('ali_merchant_cert_path', '');
  33. //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
  34. // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
  35. //可设置异步通知接收服务地址(可选)
  36. $options->notifyUrl = sys_config('site_url') . '/api/alipay/notify';
  37. // //可设置AES密钥,调用AES加解密相关接口时需要(可选)
  38. // $options->encryptKey = "<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
  39. return $options;
  40. }
  41. /**
  42. * @param $orderId
  43. * @param string $field
  44. * @return AlipayTradeCreateResponse
  45. * @throws DataNotFoundException
  46. * @throws DbException
  47. * @throws ModelNotFoundException
  48. */
  49. public static function createTrade($orderId, $field = 'order_id')
  50. {
  51. Factory::setOptions(self::getOptions());
  52. if (is_string($orderId))
  53. $orderInfo = CashTradeOrderPayment::where($field, $orderId)->find();
  54. else
  55. $orderInfo = $orderId;
  56. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  57. if ($orderInfo['paid']) exception('支付已支付!');
  58. if ($orderInfo['pay_money'] <= 0) exception('该支付无需支付!');
  59. $bodyContent = $orderInfo['order_type'] == 1 ? '挂买' : '购买' . CashTradeOrder::get($orderInfo['bind_order']);
  60. $site_name = sys_config('site_name');
  61. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  62. try {
  63. $result = Factory::payment()->common()->create($bodyContent, $orderInfo['order_id'], $orderInfo['pay_money'], $orderInfo['uid']);
  64. if (!empty($result->code) && $result->code == 10000) {
  65. return $result;
  66. } else {
  67. exception("调用失败,原因:" . $result->msg . "," . $result->sub_msg . PHP_EOL);
  68. }
  69. } catch (Exception $e) {
  70. exception("调用失败," . $e->getMessage() . PHP_EOL);
  71. }
  72. }
  73. }