You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.2 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: travis
  5. * Date: 2019/12/12
  6. * Time: 6:20
  7. */
  8. namespace api\logic;
  9. use backend\modules\shop\models\ars\Config;
  10. use EasyWeChat\Factory;
  11. use Yii;
  12. use backend\modules\shop\models\ars\Order;
  13. use yii\web\BadRequestHttpException;
  14. use yii\web\NotFoundHttpException;
  15. class WxPaymentLogic extends \yii\base\BaseObject
  16. {
  17. const PAY_TYPE_WEB = 1;
  18. const PAY_TYPE_MINI_PROGRAM = 2;
  19. public $appId;
  20. public $mchId;
  21. public $key;
  22. public $certPath;
  23. public $keyPath;
  24. public $notifyUrl;
  25. public $tradeType;
  26. public $payType;
  27. public $app;
  28. public $order;
  29. public function wxPayment($payType)
  30. {
  31. $this->payType = $payType;
  32. $data = $this->applyPaymentData();
  33. $this->unify($data);
  34. }
  35. /**
  36. * @return array
  37. */
  38. private function applyPaymentData()
  39. {
  40. $orderId = Yii::$app->request->getBodyParam('orderId');/*int 商品id*/
  41. if (empty($orderId)) {
  42. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  43. }
  44. $order = Order::find()
  45. ->where(['id' => $orderId, 'user_id' => Yii::$app->user->getId()])
  46. ->one();
  47. if (empty($order)) {
  48. throw new NotFoundHttpException('未找到该订单');
  49. }
  50. $data = [
  51. 'body' => '订单支付',
  52. 'out_trade_no' => $order->order_sn,
  53. 'total_fee' => round($order->pay_fee * 100),
  54. 'openid' => Yii::$app->user->identity->wx_openid ?? '',
  55. ];
  56. $this->tradeType = 'JSAPI';
  57. $this->order = $order;
  58. return $data;
  59. }
  60. private function getPaymentApp()
  61. {
  62. $this->_config();
  63. $config = [
  64. 'app_id' => $this->appId,
  65. 'mch_id' => $this->mchId,
  66. 'key' => $this->key,
  67. 'cert_path' => $this->certPath,
  68. 'key_path' => $this->keyPath,
  69. 'notify_url' => $this->notifyUrl,
  70. 'trade_type' => $this->tradeType
  71. ];
  72. $this->app = Factory::payment($config);
  73. }
  74. private function _config()
  75. {
  76. $path = Yii::getAlias('@backend');
  77. $config = Config::find()->one();
  78. switch ($this->payType) {
  79. case self::PAY_TYPE_WEB:
  80. $this->appId = trim($config->wx_appId);
  81. $this->mchId = trim($config->wx_mchId);
  82. $this->key = trim($config->wx_key);
  83. $this->certPath = trim($path . $config->wx_certPath);
  84. $this->keyPath = trim($path . $config->wx_keyPath);
  85. break;
  86. case self::PAY_TYPE_MINI_PROGRAM:
  87. $this->appId = trim($config->mini_program_appId);
  88. $this->mchId = trim($config->mini_program_mchId);
  89. $this->key = trim($config->mini_program_key);
  90. $this->certPath = trim($path . $config->mini_program_certPath);
  91. $this->keyPath = trim($path . $config->mini_program_keyPath);
  92. break;
  93. }
  94. $this->notifyUrl = $config->api_domain_name . $this->order->notify_url;
  95. }
  96. private function unify($data)
  97. {
  98. $this->getPaymentApp();
  99. return $this->app->order->unify($data);
  100. }
  101. }