Stripeの定期購読機能をwordpressに組み込みたいのですが、中々上手く行きません。
まず、定期購読ではなく単発支払を試したのですが、これは上手くいきました。
その際に参考としたのが
https://crosspiece.jp/how-to-use-stripe/
↑こちらの記事です。
次に同じ要領で定期購読に挑みました。
↓こちらのリファレンスを7%ほど理解し、サブスクリプション(定期購読)の
https://stripe.com/docs/subscriptions/quickstart
の部分のphpの例を単発で試した時の「checkout.php」のようなものを別に用意してそこにコピペしましたが、御察しのとおり上手くいかず。
HTMLフォームのほうはfunction.phpに追加
php
1<?php /* 2 3 This file is part of a child theme called ALC. 4 Functions in this file will be loaded before the parent theme's functions. 5 For more information, please read https://codex.wordpress.org/Child_Themes. 6 7*/ 8 9// this code loads the parent's stylesheet (leave it in place unless you know what you're doing) 10 11function theme_enqueue_styles() { 12 wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css'); 13 wp_enqueue_style('child-style', get_stylesheet_directory_uri() . '/style.css', array($parent_style)); 14} 15add_action('wp_enqueue_scripts', 'theme_enqueue_styles'); 16 17/* Add your own functions below this line. 18 ======================================== */ 19 20function facktest() { 21 echo ' 22 <form action="https://hogehoge.com/wp-content/themes/ALC/checkout.php" method="POST"> 23 <script src="https://checkout.stripe.com/checkout.js" 24 class="stripe-button" 25 data-key="pk_test_4yIXhjDfhtNf2LHWnMIcVIYH" 26 data-amount="903" 27 data-name="烏丸歌麿" 28 data-description="烏丸少将歌麿" 29 data-image="https://stripe.com/img/documentation/checkout/marketplace.png" 30 data-locale="auto" 31 data-currency="jpy" 32 data-zip-code="false" 33 data-allow-remember-me="false" 34 data-label="購入する"></script> 35 </form>'; 36 } 37add_shortcode('testfack', 'facktest'); 38 39function sittest() { 40 echo ' 41 <form action="https://hogehoge.com/wp-content/themes/ALC/subscriptions.php" method="POST"> 42 <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 43 data-key="pk_test_4yIXhjDfhtNf2LHWnMIcVIYH" 44 data-amount="999" 45 data-name="hogehoge" 46 data-description="Widget" 47 data-image="https://stripe.com/img/documentation/checkout/marketplace.png" 48 data-locale="auto" 49 data-currency="jpy"> 50 data-label="お申し込み"> 51 </script> 52 </form>'; 53 } 54add_shortcode('sitfack', 'sittest'); 55 56function register_my_menus() { 57 register_nav_menus( 58 array( 59 'user-menu' => __( 'ユーザーメニュー' ), 60 'userpage' => __( 'ユーザーページメニュー' ) 61 ) 62 ); 63} 64add_action( 'init', 'register_my_menus' ); 65
※functionに定義したものが下品なのはご容赦ください。
また、こちらが定期購読用のcheckout.php、subscription.phpです。
php
1 2<?php 3require_once( dirname(__FILE__).'/lib/stripe-php-6.4.1/init.php'); 4global $display_name , $user_email; 5 get_currentuserinfo(); 6 7 8// Set your secret key: remember to change this to your live secret key in production 9// See your keys here: https://dashboard.stripe.com/account/apikeys 10\Stripe\Stripe::setApiKey("sk_test_0tEi9Ndqyb8LPs0MLOygC5aA"); 11 12// Token is created using Stripe.js or Checkout! 13// Get the payment token submitted by the form: 14$token = $_POST['stripeToken']; 15$email = $_POST['stripeEmail']; 16 17// フォームから情報を取得: 18try { 19 20$plan = \Stripe\Plan::create([ 21 'product' => {'prod_CUwtDefuf3fCgP' => 'Basic Product'}, 22 'nickname' => '$display_name', 23 'interval' => 'month', 24 'currency' => 'jpy', 25 'amount' => 2800, 26]); 27 28$customer = \Stripe\Customer::create([ 29 'email' => $user_email, 30]); 31 32$subscription = \Stripe\Subscription::create([ 33 'customer' => 'cus_4fdAW5ftNQow1a', 34 'items' => [['plan' => 'plan_CUwvgNxfF8JnCp']], 35]); 36 37}catch (\Stripe\Error\Card $e) { 38 // 決済できなかったときの処理 39 die('決済が完了しませんでした'); 40} 41 42 43 44// 自動返信メール 45mb_language("Japanese"); 46mb_internal_encoding("UTF-8"); 47$title = "自動返信メールタイトル"; 48$content = "ここに自動返信メールの本文を記載。改行したいときは \n でできます。"; 49 50$from_name = "送信者名"; 51$from_addr = "no-reply@hogehoge.com"; 52$from_name_enc = mb_encode_mimeheader($from_name, "ISO-2022-JP"); 53$from = $from_name_enc . "<" . $from_addr . ">"; 54$header = "From: " . $from . "\n"; 55$header = $header . "Reply-To: " . $from; 56 57//to user send mail 58 59if(mb_send_mail($email,$title, $content, $header, "-f" .$from_addr)){ 60 echo "メールを送信しました"; 61} else { 62 echo "メールの送信に失敗しました"; 63}; 64 65 66 67// 管理者宛メール 68$title_me = "管理者宛メールタイトル"; 69$from_me = "info@hogehoge.com"; 70$content_me = "ここに管理者宛メールの本文を記載。改行したいときは \n でできます。"; 71if(mb_send_mail($from_me,$title_me, $content_me, $header, "-f" .$from_addr)){ 72 echo "メールを送信しました"; 73} else { 74 echo "メールの送信に失敗しました"; 75}; 76 77// サンキューページへリダイレクト 78header('Location: https://hogehoge.com/thanks/'); 79exit; 80 81?>
始めの単発の時はStripeのダッシュボードのテスト画面に売上が計上されたのですが、
この定期購読の場合は売上が計上されません。また、定期購読の場合は顧客が一覧に載るはずなのですがそれがされません。
環境
ロリポップ スタンダード
wordpress
始めに参考にした記事にサブスクリプションAPIをぶち込んだ雑なものです。
おかしいところだらけだと思うのでそういった点があれば教えてください。
できれば導入完了まで導いて欲しいです。
回答1件
あなたの回答
tips
プレビュー